[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1755] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[8283] | 4 | import java.util.Collection;
|
---|
| 5 | import java.util.LinkedList;
|
---|
[5275] | 6 | import java.util.Locale;
|
---|
| 7 |
|
---|
[6362] | 8 | public final class LanguageInfo {
|
---|
[7509] | 9 |
|
---|
[6360] | 10 | private LanguageInfo() {
|
---|
| 11 | // Hide default constructor for utils classes
|
---|
| 12 | }
|
---|
[7509] | 13 |
|
---|
[5916] | 14 | /**
|
---|
| 15 | * Type of the locale to use
|
---|
| 16 | * @since 5915
|
---|
| 17 | */
|
---|
[5915] | 18 | public enum LocaleType {
|
---|
| 19 | /** The current default language */
|
---|
| 20 | DEFAULT,
|
---|
| 21 | /** The current default language, but not english */
|
---|
| 22 | DEFAULTNOTENGLISH,
|
---|
| 23 | /** The base language (i.e. pt for pt_BR) */
|
---|
| 24 | BASELANGUAGE,
|
---|
| 25 | /** The standard english texts */
|
---|
| 26 | ENGLISH
|
---|
[6142] | 27 | }
|
---|
[2308] | 28 |
|
---|
| 29 | /**
|
---|
| 30 | * Replies the wiki language prefix for the given locale. The wiki language
|
---|
| 31 | * prefix has the form 'Xy:' where 'Xy' is a ISO 639 language code in title
|
---|
[5915] | 32 | * case (or Xy_AB: for sub languages).
|
---|
[2512] | 33 | *
|
---|
[6070] | 34 | * @param type the type
|
---|
[5915] | 35 | * @return the wiki language prefix or {@code null} for {@link LocaleType#BASELANGUAGE}, when
|
---|
| 36 | * base language is identical to default or english
|
---|
[5916] | 37 | * @since 5915
|
---|
[2308] | 38 | */
|
---|
[6889] | 39 | public static String getWikiLanguagePrefix(LocaleType type) {
|
---|
[5915] | 40 | if(type == LocaleType.ENGLISH)
|
---|
| 41 | return "";
|
---|
| 42 |
|
---|
| 43 | String code = getJOSMLocaleCode();
|
---|
| 44 | if(type == LocaleType.BASELANGUAGE) {
|
---|
| 45 | if(code.matches("[^_]+_[^_]+")) {
|
---|
| 46 | code = code.substring(0,2);
|
---|
[6223] | 47 | if ("en".equals(code))
|
---|
[5915] | 48 | return null;
|
---|
| 49 | } else {
|
---|
| 50 | return null;
|
---|
| 51 | }
|
---|
[8227] | 52 | } else if(type == LocaleType.DEFAULTNOTENGLISH && "en".equals(code)) {
|
---|
[5915] | 53 | return null;
|
---|
[8227] | 54 | } else if(code.matches(".+@.+")) {
|
---|
[8404] | 55 | return code.substring(0,1).toUpperCase(Locale.ENGLISH) + code.substring(1,2)
|
---|
| 56 | + "-" + code.substring(3,4).toUpperCase(Locale.ENGLISH) + code.substring(4) + ":";
|
---|
[8227] | 57 | }
|
---|
[8404] | 58 | return code.substring(0,1).toUpperCase(Locale.ENGLISH) + code.substring(1) + ":";
|
---|
[1755] | 59 | }
|
---|
[2308] | 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Replies the wiki language prefix for the current locale.
|
---|
[2512] | 63 | *
|
---|
[2308] | 64 | * @return the wiki language prefix
|
---|
| 65 | * @see Locale#getDefault()
|
---|
[5915] | 66 | * @see #getWikiLanguagePrefix(LocaleType)
|
---|
[2308] | 67 | */
|
---|
[6889] | 68 | public static String getWikiLanguagePrefix() {
|
---|
[5915] | 69 | return getWikiLanguagePrefix(LocaleType.DEFAULT);
|
---|
[2308] | 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Replies the JOSM locale code for the default locale.
|
---|
[2512] | 74 | *
|
---|
[2308] | 75 | * @return the JOSM locale code for the default locale
|
---|
| 76 | * @see #getJOSMLocaleCode(Locale)
|
---|
| 77 | */
|
---|
[6889] | 78 | public static String getJOSMLocaleCode() {
|
---|
[2308] | 79 | return getJOSMLocaleCode(Locale.getDefault());
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
[3418] | 83 | * Replies the locale code used by JOSM for a given locale.
|
---|
[2512] | 84 | *
|
---|
[5266] | 85 | * In most cases JOSM uses the 2-character ISO 639 language code ({@link Locale#getLanguage()}
|
---|
[2308] | 86 | * to identify the locale of a localized resource, but in some cases it may use the
|
---|
[5266] | 87 | * programmatic name for locales, as replied by {@link Locale#toString()}.
|
---|
[8404] | 88 | *
|
---|
[8284] | 89 | * For unknown country codes and variants this function already does fallback to
|
---|
[8283] | 90 | * internally known translations.
|
---|
[2512] | 91 | *
|
---|
[2308] | 92 | * @param locale the locale. Replies "en" if null.
|
---|
| 93 | * @return the JOSM code for the given locale
|
---|
| 94 | */
|
---|
[6889] | 95 | public static String getJOSMLocaleCode(Locale locale) {
|
---|
[2308] | 96 | if (locale == null) return "en";
|
---|
[8283] | 97 | for(String full : getLanguageCodes(locale)) {
|
---|
| 98 | if ("iw_IL".equals(full))
|
---|
| 99 | return "he";
|
---|
| 100 | else if ("in".equals(full))
|
---|
| 101 | return "id";
|
---|
| 102 | else if (I18n.hasCode(full)) // catch all non-single codes
|
---|
| 103 | return full;
|
---|
| 104 | }
|
---|
[2308] | 105 |
|
---|
[8283] | 106 | // return single code as fallback
|
---|
[2308] | 107 | return locale.getLanguage();
|
---|
[1755] | 108 | }
|
---|
[2308] | 109 |
|
---|
[4211] | 110 | /**
|
---|
| 111 | * Replies the locale code used by Java for a given locale.
|
---|
| 112 | *
|
---|
[8233] | 113 | * In most cases JOSM and Java uses the same codes, but for some exceptions this is needed.
|
---|
| 114 | *
|
---|
[8232] | 115 | * @param locale the locale. Replies "en" if null.
|
---|
| 116 | * @return the Java code for the given locale
|
---|
| 117 | * @since 8232
|
---|
| 118 | */
|
---|
| 119 | public static String getJavaLocaleCode(String localeName) {
|
---|
| 120 | if (localeName == null) return "en";
|
---|
| 121 | if ("ca@valencia".equals(localeName)) {
|
---|
| 122 | localeName = "ca__valencia";
|
---|
| 123 | } else if ("he".equals(localeName)) {
|
---|
| 124 | localeName = "iw_IL";
|
---|
| 125 | } else if ("id".equals(localeName)) {
|
---|
| 126 | localeName = "in";
|
---|
| 127 | }
|
---|
| 128 | return localeName;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * Replies the display string used by JOSM for a given locale.
|
---|
| 133 | *
|
---|
| 134 | * In most cases returns text replied by {@link Locale#getDisplayName()}, for some
|
---|
| 135 | * locales an override is used (i.e. when unsupported by Java).
|
---|
| 136 | *
|
---|
| 137 | * @param locale the locale. Replies "en" if null.
|
---|
| 138 | * @return the display string for the given locale
|
---|
| 139 | * @since 8232
|
---|
| 140 | */
|
---|
| 141 | public static String getDisplayName(Locale locale) {
|
---|
[8241] | 142 | /*String full = locale.toString();
|
---|
[8232] | 143 | if ("ca__valencia".equals(full))
|
---|
[8241] | 144 | return t_r_c("language", "Valencian");*/
|
---|
[8232] | 145 |
|
---|
| 146 | return locale.getDisplayName();
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /**
|
---|
[8233] | 150 | * Replies the locale used by Java for a given language code.
|
---|
[8232] | 151 | *
|
---|
[8233] | 152 | * Accepts JOSM and Java codes as input.
|
---|
[4211] | 153 | *
|
---|
[5275] | 154 | * @param localeName the locale code.
|
---|
[4211] | 155 | * @return the resulting locale
|
---|
| 156 | */
|
---|
[6889] | 157 | public static Locale getLocale(String localeName) {
|
---|
[8283] | 158 | int country = localeName.indexOf("_");
|
---|
| 159 | int variant = localeName.indexOf("@");
|
---|
| 160 | if (variant < 0 && country >= 0)
|
---|
| 161 | variant = localeName.indexOf("_", country+1);
|
---|
[4211] | 162 | Locale l;
|
---|
[8283] | 163 | if (variant > 0 && country > 0) {
|
---|
| 164 | l = new Locale(localeName.substring(0, country), localeName.substring(country+1, variant), localeName.substring(variant + 1));
|
---|
| 165 | } else if (variant > 0) {
|
---|
| 166 | l = new Locale(localeName.substring(0, variant), "", localeName.substring(variant + 1));
|
---|
| 167 | } else if (country > 0) {
|
---|
| 168 | l = new Locale(localeName.substring(0, country), localeName.substring(country + 1));
|
---|
[4211] | 169 | } else {
|
---|
| 170 | l = new Locale(localeName);
|
---|
| 171 | }
|
---|
| 172 | return l;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[8091] | 175 | /**
|
---|
| 176 | * Check if a new language is better than a previous existing. Can be used in classes where
|
---|
| 177 | * multiple user supplied language marked strings appear and the best one is searched. Following
|
---|
| 178 | * priorities: current language, english, any other
|
---|
| 179 | *
|
---|
| 180 | * @param oldLanguage the language code of the existing string
|
---|
| 181 | * @param newLanguage the language code of the new string
|
---|
| 182 | * @return true if new one is better
|
---|
| 183 | * @since 8091
|
---|
| 184 | */
|
---|
| 185 | public static boolean isBetterLanguage(String oldLanguage, String newLanguage) {
|
---|
| 186 | if (oldLanguage == null)
|
---|
| 187 | return true;
|
---|
| 188 | String want = getJOSMLocaleCode();
|
---|
| 189 | return want.equals(newLanguage) || (!want.equals(oldLanguage) && newLanguage.startsWith("en"));
|
---|
| 190 | }
|
---|
[8404] | 191 |
|
---|
[8091] | 192 | /**
|
---|
| 193 | * Replies the language prefix for use in XML elements (with a dot appended).
|
---|
| 194 | *
|
---|
| 195 | * @return the XML language prefix
|
---|
| 196 | * @see #getJOSMLocaleCode()
|
---|
| 197 | */
|
---|
[6889] | 198 | public static String getLanguageCodeXML() {
|
---|
[8227] | 199 | String code = getJOSMLocaleCode();
|
---|
| 200 | code = code.replace("@", "-");
|
---|
| 201 | return code+".";
|
---|
[1755] | 202 | }
|
---|
[7509] | 203 |
|
---|
[8091] | 204 | /**
|
---|
| 205 | * Replies the language prefix for use in manifests (with an underscore appended).
|
---|
| 206 | *
|
---|
| 207 | * @return the manifest language prefix
|
---|
| 208 | * @see #getJOSMLocaleCode()
|
---|
| 209 | */
|
---|
[6889] | 210 | public static String getLanguageCodeManifest() {
|
---|
[8227] | 211 | String code = getJOSMLocaleCode();
|
---|
| 212 | code = code.replace("@", "-");
|
---|
| 213 | return code+"_";
|
---|
[1755] | 214 | }
|
---|
[8283] | 215 |
|
---|
| 216 | /**
|
---|
| 217 | * Replies a list of language codes for local names. Prefixes range from very specific
|
---|
| 218 | * to more generic.
|
---|
| 219 | * <ul>
|
---|
| 220 | * <li>lang_COUNTRY@variant of the current locale</li>
|
---|
| 221 | * <li>lang@variant of the current locale</li>
|
---|
| 222 | * <li>lang_COUNTRY of the current locale</li>
|
---|
| 223 | * <li>lang of the current locale</li>
|
---|
| 224 | * </ul>
|
---|
| 225 | *
|
---|
| 226 | * @param locale the locale to use, <code>null</code> for default locale
|
---|
| 227 | * @since 8283
|
---|
| 228 | * @return list of codes
|
---|
| 229 | */
|
---|
| 230 | public static Collection<String> getLanguageCodes(Locale l) {
|
---|
| 231 | Collection<String> list = new LinkedList<String>();
|
---|
| 232 | if(l == null)
|
---|
| 233 | l = Locale.getDefault();
|
---|
| 234 | String lang = l.getLanguage();
|
---|
| 235 | String c = l.getCountry();
|
---|
| 236 | String v = l.getVariant();
|
---|
| 237 | if(c.isEmpty())
|
---|
| 238 | c = null;
|
---|
| 239 | if(v != null && !v.isEmpty()) {
|
---|
| 240 | if(c != null)
|
---|
| 241 | list.add(lang+"_"+c+"@"+v);
|
---|
| 242 | list.add(lang+"@"+v);
|
---|
| 243 | }
|
---|
| 244 | if(c != null)
|
---|
| 245 | list.add(lang+"_"+c);
|
---|
| 246 | list.add(lang);
|
---|
| 247 | return list;
|
---|
| 248 | }
|
---|
[1755] | 249 | }
|
---|