| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.tools;
|
|---|
| 3 |
|
|---|
| 4 | import java.util.ArrayList;
|
|---|
| 5 | import java.util.List;
|
|---|
| 6 | import java.util.Locale;
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * This is a utility class that provides information about locales and allows to convert locale codes.
|
|---|
| 10 | */
|
|---|
| 11 | public final class LanguageInfo {
|
|---|
| 12 |
|
|---|
| 13 | private LanguageInfo() {
|
|---|
| 14 | // Hide default constructor for utils classes
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Type of the locale to use
|
|---|
| 19 | * @since 5915
|
|---|
| 20 | */
|
|---|
| 21 | public enum LocaleType {
|
|---|
| 22 | /** The current default language */
|
|---|
| 23 | DEFAULT,
|
|---|
| 24 | /** The current default language, but not english */
|
|---|
| 25 | DEFAULTNOTENGLISH,
|
|---|
| 26 | /** The base language (i.e. pt for pt_BR) */
|
|---|
| 27 | BASELANGUAGE,
|
|---|
| 28 | /** The standard english texts */
|
|---|
| 29 | ENGLISH,
|
|---|
| 30 | /** The locale prefix on the OSM wiki */
|
|---|
| 31 | OSM_WIKI,
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Replies the wiki language prefix for the given locale. The wiki language
|
|---|
| 36 | * prefix has the form 'Xy:' where 'Xy' is a ISO 639 language code in title
|
|---|
| 37 | * case (or Xy_AB: for sub languages).
|
|---|
| 38 | *
|
|---|
| 39 | * @param type the type
|
|---|
| 40 | * @return the wiki language prefix or {@code null} for {@link LocaleType#BASELANGUAGE}, when
|
|---|
| 41 | * base language is identical to default or english
|
|---|
| 42 | * @since 5915
|
|---|
| 43 | */
|
|---|
| 44 | public static String getWikiLanguagePrefix(LocaleType type) {
|
|---|
| 45 | return getWikiLanguagePrefix(Locale.getDefault(), type);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | static String getWikiLanguagePrefix(Locale locale, LocaleType type) {
|
|---|
| 49 | if (type == LocaleType.ENGLISH) {
|
|---|
| 50 | return "";
|
|---|
| 51 | } else if (type == LocaleType.OSM_WIKI && Locale.ENGLISH.getLanguage().equals(locale.getLanguage())) {
|
|---|
| 52 | return "";
|
|---|
| 53 | } else if (type == LocaleType.OSM_WIKI && Locale.SIMPLIFIED_CHINESE.equals(locale)) {
|
|---|
| 54 | return "Zh-hans:";
|
|---|
| 55 | } else if (type == LocaleType.OSM_WIKI && Locale.TRADITIONAL_CHINESE.equals(locale)) {
|
|---|
| 56 | return "Zh-hant:";
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | String code = getJOSMLocaleCode(locale);
|
|---|
| 60 |
|
|---|
| 61 | if (type == LocaleType.OSM_WIKI) {
|
|---|
| 62 | if (code.matches("[^_@]+[_@][^_]+")) {
|
|---|
| 63 | code = code.substring(0, 2);
|
|---|
| 64 | if ("en".equals(code)) {
|
|---|
| 65 | return "";
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | switch (code) {
|
|---|
| 69 | case "nb": /* OSM-Wiki has "no", but no "nb" */
|
|---|
| 70 | return "No:";
|
|---|
| 71 | case "sr@latin": /* OSM-Wiki has "Sr-latn" and not Sr-latin */
|
|---|
| 72 | return "Sr-latn:";
|
|---|
| 73 | case "de":
|
|---|
| 74 | case "es":
|
|---|
| 75 | case "fr":
|
|---|
| 76 | case "it":
|
|---|
| 77 | case "nl":
|
|---|
| 78 | case "ru":
|
|---|
| 79 | case "ja":
|
|---|
| 80 | return code.toUpperCase(Locale.ENGLISH) + ":";
|
|---|
| 81 | default:
|
|---|
| 82 | return code.substring(0, 1).toUpperCase(Locale.ENGLISH) + code.substring(1) + ":";
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | if (type == LocaleType.BASELANGUAGE) {
|
|---|
| 87 | if (code.matches("[^_]+_[^_]+")) {
|
|---|
| 88 | code = code.substring(0, 2);
|
|---|
| 89 | if ("en".equals(code))
|
|---|
| 90 | return null;
|
|---|
| 91 | } else {
|
|---|
| 92 | return null;
|
|---|
| 93 | }
|
|---|
| 94 | } else if (type == LocaleType.DEFAULTNOTENGLISH && "en".equals(code)) {
|
|---|
| 95 | return null;
|
|---|
| 96 | } else if (code.matches(".+@.+")) {
|
|---|
| 97 | return code.substring(0, 1).toUpperCase(Locale.ENGLISH)
|
|---|
| 98 | + code.charAt(1)
|
|---|
| 99 | + '-'
|
|---|
| 100 | + code.substring(3, 4).toUpperCase(Locale.ENGLISH)
|
|---|
| 101 | + code.substring(4)
|
|---|
| 102 | + ':';
|
|---|
| 103 | }
|
|---|
| 104 | return code.substring(0, 1).toUpperCase(Locale.ENGLISH) + code.substring(1) + ':';
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | * Replies the wiki language prefix for the current locale.
|
|---|
| 109 | *
|
|---|
| 110 | * @return the wiki language prefix
|
|---|
| 111 | * @see Locale#getDefault()
|
|---|
| 112 | * @see #getWikiLanguagePrefix(LocaleType)
|
|---|
| 113 | */
|
|---|
| 114 | public static String getWikiLanguagePrefix() {
|
|---|
| 115 | return getWikiLanguagePrefix(LocaleType.DEFAULT);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /**
|
|---|
| 119 | * Replies the JOSM locale code for the default locale.
|
|---|
| 120 | *
|
|---|
| 121 | * @return the JOSM locale code for the default locale
|
|---|
| 122 | * @see #getJOSMLocaleCode(Locale)
|
|---|
| 123 | */
|
|---|
| 124 | public static String getJOSMLocaleCode() {
|
|---|
| 125 | return getJOSMLocaleCode(Locale.getDefault());
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Replies the locale code used by JOSM for a given locale.
|
|---|
| 130 | * <p>
|
|---|
| 131 | * In most cases JOSM uses the 2-character ISO 639 language code ({@link Locale#getLanguage()}
|
|---|
| 132 | * to identify the locale of a localized resource, but in some cases it may use the
|
|---|
| 133 | * programmatic name for locales, as replied by {@link Locale#toString()}.
|
|---|
| 134 | * <p>
|
|---|
| 135 | * For unknown country codes and variants this function already does fallback to
|
|---|
| 136 | * internally known translations.
|
|---|
| 137 | *
|
|---|
| 138 | * @param locale the locale. Replies "en" if null.
|
|---|
| 139 | * @return the JOSM code for the given locale
|
|---|
| 140 | */
|
|---|
| 141 | public static String getJOSMLocaleCode(Locale locale) {
|
|---|
| 142 | if (locale == null) return "en";
|
|---|
| 143 | for (String full : getLanguageCodes(locale)) {
|
|---|
| 144 | if ("iw_IL".equals(full))
|
|---|
| 145 | return "he";
|
|---|
| 146 | else if ("in".equals(full))
|
|---|
| 147 | return "id";
|
|---|
| 148 | else if (I18n.hasCode(full)) // catch all non-single codes
|
|---|
| 149 | return full;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | // return single code as fallback
|
|---|
| 153 | return locale.getLanguage();
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * Replies the OSM locale codes for the default locale.
|
|---|
| 158 | *
|
|---|
| 159 | * @param prefix a prefix like {@code name:}.
|
|---|
| 160 | * @return the OSM locale codes for the default locale
|
|---|
| 161 | * @see #getOSMLocaleCodes(String, Locale)
|
|---|
| 162 | * @since 19045
|
|---|
| 163 | */
|
|---|
| 164 | public static String[] getOSMLocaleCodes(String prefix) {
|
|---|
| 165 | return getOSMLocaleCodes(prefix, Locale.getDefault());
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * Replies the locale codes used by OSM for a given locale.
|
|---|
| 170 | * <p>
|
|---|
| 171 | * In most cases OSM uses the 2-character ISO 639 language code ({@link Locale#getLanguage()}
|
|---|
| 172 | * to identify the locale of a localized resource, but in some cases it may use the
|
|---|
| 173 | * programmatic name for locales, as replied by {@link Locale#toString()}.
|
|---|
| 174 | * <p>
|
|---|
| 175 | * For unknown country codes and variants this function already does fallback to
|
|---|
| 176 | * internally known translations.
|
|---|
| 177 | *
|
|---|
| 178 | * @param prefix a prefix like {@code name:}.
|
|---|
| 179 | * @param locale the locale. Replies "en" if null.
|
|---|
| 180 | * @return the OSM codes for the given locale
|
|---|
| 181 | * @since 19045
|
|---|
| 182 | */
|
|---|
| 183 | public static String[] getOSMLocaleCodes(String prefix, Locale locale) {
|
|---|
| 184 | if (prefix == null) {
|
|---|
| 185 | prefix = "";
|
|---|
| 186 | }
|
|---|
| 187 | String main = getJOSMLocaleCode(locale);
|
|---|
| 188 | switch (main) {
|
|---|
| 189 | case "zh_CN":
|
|---|
| 190 | return new String[]{prefix+"zh-Hans-CN", prefix+"zh-Hans", prefix+"zh"};
|
|---|
| 191 | case "zh_TW":
|
|---|
| 192 | return new String[]{prefix+"zh-Hant-TW", prefix+"zh-Hant", prefix+"zh"};
|
|---|
| 193 | default:
|
|---|
| 194 | ArrayList<String> r = new ArrayList<>();
|
|---|
| 195 | for (String s : LanguageInfo.getLanguageCodes(null)) {
|
|---|
| 196 | r.add(prefix + s);
|
|---|
| 197 | }
|
|---|
| 198 | return r.toArray(String[]::new);
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /**
|
|---|
| 203 | * Replies the locale code used by Java for a given locale.
|
|---|
| 204 | * <p>
|
|---|
| 205 | * In most cases JOSM and Java uses the same codes, but for some exceptions this is needed.
|
|---|
| 206 | *
|
|---|
| 207 | * @param localeName the locale. Replies "en" if null.
|
|---|
| 208 | * @return the Java code for the given locale
|
|---|
| 209 | * @since 8232
|
|---|
| 210 | */
|
|---|
| 211 | public static String getJavaLocaleCode(String localeName) {
|
|---|
| 212 | if (localeName == null) return "en";
|
|---|
| 213 | switch (localeName) {
|
|---|
| 214 | case "ca@valencia":
|
|---|
| 215 | return "ca__valencia";
|
|---|
| 216 | case "sr@latin":
|
|---|
| 217 | return "sr__latin";
|
|---|
| 218 | case "he":
|
|---|
| 219 | return "iw_IL";
|
|---|
| 220 | case "id":
|
|---|
| 221 | return "in";
|
|---|
| 222 | default:
|
|---|
| 223 | return localeName;
|
|---|
| 224 | }
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /**
|
|---|
| 228 | * Replies the display string used by JOSM for a given locale.
|
|---|
| 229 | * <p>
|
|---|
| 230 | * In most cases returns text replied by {@link Locale#getDisplayName()}, for some
|
|---|
| 231 | * locales an override is used (i.e. when unsupported by Java).
|
|---|
| 232 | *
|
|---|
| 233 | * @param locale the locale. Replies "en" if null.
|
|---|
| 234 | * @return the display string for the given locale
|
|---|
| 235 | * @since 8232
|
|---|
| 236 | */
|
|---|
| 237 | public static String getDisplayName(Locale locale) {
|
|---|
| 238 | String currentCountry = Locale.getDefault().getCountry();
|
|---|
| 239 | String localeCountry = locale.getCountry();
|
|---|
| 240 | // Don't display locale country if country has been forced to current country at JOSM startup
|
|---|
| 241 | if (currentCountry.equals(localeCountry) && !I18n.hasCode(getLanguageCodes(locale).get(0))) {
|
|---|
| 242 | return new Locale(locale.getLanguage(), "", locale.getVariant()).getDisplayName();
|
|---|
| 243 | }
|
|---|
| 244 | return locale.getDisplayName();
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | /**
|
|---|
| 248 | * Replies the locale used by Java for a given language code.
|
|---|
| 249 | * <p>
|
|---|
| 250 | * Accepts JOSM and Java codes as input.
|
|---|
| 251 | *
|
|---|
| 252 | * @param localeName the locale code.
|
|---|
| 253 | * @return the resulting locale
|
|---|
| 254 | */
|
|---|
| 255 | public static Locale getLocale(String localeName) {
|
|---|
| 256 | return getLocale(localeName, false);
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /**
|
|---|
| 260 | * Replies the locale used by Java for a given language code.
|
|---|
| 261 | * <p>
|
|---|
| 262 | * Accepts JOSM, Java and POSIX codes as input.
|
|---|
| 263 | *
|
|---|
| 264 | * @param localeName the locale code.
|
|---|
| 265 | * @param useDefaultCountry if {@code true}, the current locale country will be used if no country is specified
|
|---|
| 266 | * @return the resulting locale
|
|---|
| 267 | * @since 15547
|
|---|
| 268 | */
|
|---|
| 269 | public static Locale getLocale(String localeName, boolean useDefaultCountry) {
|
|---|
| 270 | final int encoding = localeName.indexOf('.');
|
|---|
| 271 | if (encoding > 0) {
|
|---|
| 272 | localeName = localeName.substring(0, encoding);
|
|---|
| 273 | }
|
|---|
| 274 | int country = localeName.indexOf('_');
|
|---|
| 275 | int variant = localeName.indexOf('@');
|
|---|
| 276 | if (variant < 0 && country >= 0)
|
|---|
| 277 | variant = localeName.indexOf('_', country+1);
|
|---|
| 278 | Locale l;
|
|---|
| 279 | if (variant > 0 && country > 0) {
|
|---|
| 280 | l = new Locale(localeName.substring(0, country), localeName.substring(country+1, variant), localeName.substring(variant + 1));
|
|---|
| 281 | } else if (variant > 0) {
|
|---|
| 282 | l = new Locale(localeName.substring(0, variant), "", localeName.substring(variant + 1));
|
|---|
| 283 | } else if (country > 0) {
|
|---|
| 284 | l = new Locale(localeName.substring(0, country), localeName.substring(country + 1));
|
|---|
| 285 | } else {
|
|---|
| 286 | l = new Locale(localeName, useDefaultCountry ? Locale.getDefault().getCountry() : "");
|
|---|
| 287 | }
|
|---|
| 288 | return l;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | /**
|
|---|
| 292 | * Check if a new language is better than a previous existing. Can be used in classes where
|
|---|
| 293 | * multiple user supplied language marked strings appear and the best one is searched. Following
|
|---|
| 294 | * priorities: current language, english, any other
|
|---|
| 295 | *
|
|---|
| 296 | * @param oldLanguage the language code of the existing string
|
|---|
| 297 | * @param newLanguage the language code of the new string
|
|---|
| 298 | * @return true if new one is better
|
|---|
| 299 | * @since 8091
|
|---|
| 300 | */
|
|---|
| 301 | public static boolean isBetterLanguage(String oldLanguage, String newLanguage) {
|
|---|
| 302 | if (oldLanguage == null)
|
|---|
| 303 | return true;
|
|---|
| 304 | String want = getJOSMLocaleCode();
|
|---|
| 305 | return want.equals(newLanguage) || (!want.equals(oldLanguage) && newLanguage.startsWith("en"));
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| 309 | * Replies the language prefix for use in XML elements (with a dot appended).
|
|---|
| 310 | *
|
|---|
| 311 | * @return the XML language prefix
|
|---|
| 312 | * @see #getJOSMLocaleCode()
|
|---|
| 313 | */
|
|---|
| 314 | public static String getLanguageCodeXML() {
|
|---|
| 315 | String code = getJOSMLocaleCode();
|
|---|
| 316 | code = code.replace('@', '-');
|
|---|
| 317 | return code+'.';
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | /**
|
|---|
| 321 | * Replies the language prefix for use in manifests (with an underscore appended).
|
|---|
| 322 | *
|
|---|
| 323 | * @return the manifest language prefix
|
|---|
| 324 | * @see #getJOSMLocaleCode()
|
|---|
| 325 | */
|
|---|
| 326 | public static String getLanguageCodeManifest() {
|
|---|
| 327 | String code = getJOSMLocaleCode();
|
|---|
| 328 | code = code.replace('@', '-');
|
|---|
| 329 | return code+'_';
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | /**
|
|---|
| 333 | * Replies a list of language codes for local names. Prefixes range from very specific
|
|---|
| 334 | * to more generic.
|
|---|
| 335 | * <ul>
|
|---|
| 336 | * <li>lang_COUNTRY@variant of the current locale</li>
|
|---|
| 337 | * <li>lang@variant of the current locale</li>
|
|---|
| 338 | * <li>lang_COUNTRY of the current locale</li>
|
|---|
| 339 | * <li>lang of the current locale</li>
|
|---|
| 340 | * </ul>
|
|---|
| 341 | *
|
|---|
| 342 | * @param l the locale to use, <code>null</code> for default locale
|
|---|
| 343 | * @return list of codes
|
|---|
| 344 | * @since 8283
|
|---|
| 345 | */
|
|---|
| 346 | public static List<String> getLanguageCodes(Locale l) {
|
|---|
| 347 | List<String> list = new ArrayList<>(4);
|
|---|
| 348 | if (l == null)
|
|---|
| 349 | l = Locale.getDefault();
|
|---|
| 350 | String lang = l.getLanguage();
|
|---|
| 351 | String c = l.getCountry();
|
|---|
| 352 | String v = l.getVariant();
|
|---|
| 353 | if (c.isEmpty())
|
|---|
| 354 | c = null;
|
|---|
| 355 | if (!Utils.isEmpty(v)) {
|
|---|
| 356 | if (c != null)
|
|---|
| 357 | list.add(lang+'_'+c+'@'+v);
|
|---|
| 358 | list.add(lang+'@'+v);
|
|---|
| 359 | }
|
|---|
| 360 | if (c != null)
|
|---|
| 361 | list.add(lang+'_'+c);
|
|---|
| 362 | list.add(lang);
|
|---|
| 363 | return list;
|
|---|
| 364 | }
|
|---|
| 365 | }
|
|---|