[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1755] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[5275] | 4 | import java.util.Locale;
|
---|
| 5 |
|
---|
[6362] | 6 | public final class LanguageInfo {
|
---|
[6360] | 7 |
|
---|
| 8 | private LanguageInfo() {
|
---|
| 9 | // Hide default constructor for utils classes
|
---|
| 10 | }
|
---|
| 11 |
|
---|
[5916] | 12 | /**
|
---|
| 13 | * Type of the locale to use
|
---|
| 14 | * @since 5915
|
---|
| 15 | */
|
---|
[5915] | 16 | public enum LocaleType {
|
---|
| 17 | /** The current default language */
|
---|
| 18 | DEFAULT,
|
---|
| 19 | /** The current default language, but not english */
|
---|
| 20 | DEFAULTNOTENGLISH,
|
---|
| 21 | /** The base language (i.e. pt for pt_BR) */
|
---|
| 22 | BASELANGUAGE,
|
---|
| 23 | /** The standard english texts */
|
---|
| 24 | ENGLISH
|
---|
[6142] | 25 | }
|
---|
[2308] | 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Replies the wiki language prefix for the given locale. The wiki language
|
---|
| 29 | * prefix has the form 'Xy:' where 'Xy' is a ISO 639 language code in title
|
---|
[5915] | 30 | * case (or Xy_AB: for sub languages).
|
---|
[2512] | 31 | *
|
---|
[6070] | 32 | * @param type the type
|
---|
[5915] | 33 | * @return the wiki language prefix or {@code null} for {@link LocaleType#BASELANGUAGE}, when
|
---|
| 34 | * base language is identical to default or english
|
---|
[5916] | 35 | * @since 5915
|
---|
[2308] | 36 | */
|
---|
[6889] | 37 | public static String getWikiLanguagePrefix(LocaleType type) {
|
---|
[5915] | 38 | if(type == LocaleType.ENGLISH)
|
---|
| 39 | return "";
|
---|
| 40 |
|
---|
| 41 | String code = getJOSMLocaleCode();
|
---|
| 42 | if(type == LocaleType.BASELANGUAGE) {
|
---|
| 43 | if(code.matches("[^_]+_[^_]+")) {
|
---|
| 44 | code = code.substring(0,2);
|
---|
[6223] | 45 | if ("en".equals(code))
|
---|
[5915] | 46 | return null;
|
---|
| 47 | } else {
|
---|
| 48 | return null;
|
---|
| 49 | }
|
---|
[6223] | 50 | } else if(type == LocaleType.DEFAULTNOTENGLISH && "en".equals(code))
|
---|
[5915] | 51 | return null;
|
---|
[2308] | 52 | return code.substring(0,1).toUpperCase() + code.substring(1) + ":";
|
---|
[1755] | 53 | }
|
---|
[2308] | 54 |
|
---|
| 55 | /**
|
---|
| 56 | * Replies the wiki language prefix for the current locale.
|
---|
[2512] | 57 | *
|
---|
[2308] | 58 | * @return the wiki language prefix
|
---|
| 59 | * @see Locale#getDefault()
|
---|
[5915] | 60 | * @see #getWikiLanguagePrefix(LocaleType)
|
---|
[2308] | 61 | */
|
---|
[6889] | 62 | public static String getWikiLanguagePrefix() {
|
---|
[5915] | 63 | return getWikiLanguagePrefix(LocaleType.DEFAULT);
|
---|
[2308] | 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * Replies the JOSM locale code for the default locale.
|
---|
[2512] | 68 | *
|
---|
[2308] | 69 | * @return the JOSM locale code for the default locale
|
---|
| 70 | * @see #getJOSMLocaleCode(Locale)
|
---|
| 71 | */
|
---|
[6889] | 72 | public static String getJOSMLocaleCode() {
|
---|
[2308] | 73 | return getJOSMLocaleCode(Locale.getDefault());
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
[3418] | 77 | * Replies the locale code used by JOSM for a given locale.
|
---|
[2512] | 78 | *
|
---|
[5266] | 79 | * In most cases JOSM uses the 2-character ISO 639 language code ({@link Locale#getLanguage()}
|
---|
[2308] | 80 | * to identify the locale of a localized resource, but in some cases it may use the
|
---|
[5266] | 81 | * programmatic name for locales, as replied by {@link Locale#toString()}.
|
---|
[2512] | 82 | *
|
---|
[2308] | 83 | * @param locale the locale. Replies "en" if null.
|
---|
| 84 | * @return the JOSM code for the given locale
|
---|
| 85 | */
|
---|
[6889] | 86 | public static String getJOSMLocaleCode(Locale locale) {
|
---|
[2308] | 87 | if (locale == null) return "en";
|
---|
| 88 | String full = locale.toString();
|
---|
[7012] | 89 | if ("iw_IL".equals(full))
|
---|
[1755] | 90 | return "he";
|
---|
[7012] | 91 | else if ("in".equals(full))
|
---|
[4211] | 92 | return "id";
|
---|
[6360] | 93 | else if (I18n.hasCode(full)) // catch all non-single codes
|
---|
[1755] | 94 | return full;
|
---|
[2308] | 95 |
|
---|
[6360] | 96 | // return single code
|
---|
[2308] | 97 | return locale.getLanguage();
|
---|
[1755] | 98 | }
|
---|
[2308] | 99 |
|
---|
[4211] | 100 | /**
|
---|
| 101 | * Replies the locale code used by Java for a given locale.
|
---|
| 102 | *
|
---|
| 103 | * In most cases JOSM and Java uses the same codes, but for some exceptions this is needed.
|
---|
| 104 | *
|
---|
[5275] | 105 | * @param localeName the locale code.
|
---|
[4211] | 106 | * @return the resulting locale
|
---|
| 107 | */
|
---|
[6889] | 108 | public static Locale getLocale(String localeName) {
|
---|
[7012] | 109 | if ("he".equals(localeName)) {
|
---|
[4211] | 110 | localeName = "iw_IL";
|
---|
| 111 | }
|
---|
[7012] | 112 | else if ("id".equals(localeName)) {
|
---|
[4211] | 113 | localeName = "in";
|
---|
| 114 | }
|
---|
| 115 | Locale l;
|
---|
| 116 | int i = localeName.indexOf('_');
|
---|
| 117 | if (i > 0) {
|
---|
| 118 | l = new Locale(localeName.substring(0, i), localeName.substring(i + 1));
|
---|
| 119 | } else {
|
---|
| 120 | l = new Locale(localeName);
|
---|
| 121 | }
|
---|
| 122 | return l;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[6889] | 125 | public static String getLanguageCodeXML() {
|
---|
[2308] | 126 | return getJOSMLocaleCode()+".";
|
---|
[1755] | 127 | }
|
---|
[6360] | 128 |
|
---|
[6889] | 129 | public static String getLanguageCodeManifest() {
|
---|
[2308] | 130 | return getJOSMLocaleCode()+"_";
|
---|
[1755] | 131 | }
|
---|
| 132 | }
|
---|