| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.tools;
|
|---|
| 3 |
|
|---|
| 4 | import java.util.Locale;
|
|---|
| 5 |
|
|---|
| 6 | public final class LanguageInfo {
|
|---|
| 7 |
|
|---|
| 8 | private LanguageInfo() {
|
|---|
| 9 | // Hide default constructor for utils classes
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | /**
|
|---|
| 13 | * Type of the locale to use
|
|---|
| 14 | * @since 5915
|
|---|
| 15 | */
|
|---|
| 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
|
|---|
| 25 | }
|
|---|
| 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
|
|---|
| 30 | * case (or Xy_AB: for sub languages).
|
|---|
| 31 | *
|
|---|
| 32 | * @param type the type
|
|---|
| 33 | * @return the wiki language prefix or {@code null} for {@link LocaleType#BASELANGUAGE}, when
|
|---|
| 34 | * base language is identical to default or english
|
|---|
| 35 | * @since 5915
|
|---|
| 36 | */
|
|---|
| 37 | public static String getWikiLanguagePrefix(LocaleType type) {
|
|---|
| 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);
|
|---|
| 45 | if ("en".equals(code))
|
|---|
| 46 | return null;
|
|---|
| 47 | } else {
|
|---|
| 48 | return null;
|
|---|
| 49 | }
|
|---|
| 50 | } else if(type == LocaleType.DEFAULTNOTENGLISH && "en".equals(code))
|
|---|
| 51 | return null;
|
|---|
| 52 | return code.substring(0,1).toUpperCase() + code.substring(1) + ":";
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /**
|
|---|
| 56 | * Replies the wiki language prefix for the current locale.
|
|---|
| 57 | *
|
|---|
| 58 | * @return the wiki language prefix
|
|---|
| 59 | * @see Locale#getDefault()
|
|---|
| 60 | * @see #getWikiLanguagePrefix(LocaleType)
|
|---|
| 61 | */
|
|---|
| 62 | public static String getWikiLanguagePrefix() {
|
|---|
| 63 | return getWikiLanguagePrefix(LocaleType.DEFAULT);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Replies the JOSM locale code for the default locale.
|
|---|
| 68 | *
|
|---|
| 69 | * @return the JOSM locale code for the default locale
|
|---|
| 70 | * @see #getJOSMLocaleCode(Locale)
|
|---|
| 71 | */
|
|---|
| 72 | public static String getJOSMLocaleCode() {
|
|---|
| 73 | return getJOSMLocaleCode(Locale.getDefault());
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * Replies the locale code used by JOSM for a given locale.
|
|---|
| 78 | *
|
|---|
| 79 | * In most cases JOSM uses the 2-character ISO 639 language code ({@link Locale#getLanguage()}
|
|---|
| 80 | * to identify the locale of a localized resource, but in some cases it may use the
|
|---|
| 81 | * programmatic name for locales, as replied by {@link Locale#toString()}.
|
|---|
| 82 | *
|
|---|
| 83 | * @param locale the locale. Replies "en" if null.
|
|---|
| 84 | * @return the JOSM code for the given locale
|
|---|
| 85 | */
|
|---|
| 86 | public static String getJOSMLocaleCode(Locale locale) {
|
|---|
| 87 | if (locale == null) return "en";
|
|---|
| 88 | String full = locale.toString();
|
|---|
| 89 | if (full.equals("iw_IL"))
|
|---|
| 90 | return "he";
|
|---|
| 91 | else if (full.equals("in"))
|
|---|
| 92 | return "id";
|
|---|
| 93 | else if (I18n.hasCode(full)) // catch all non-single codes
|
|---|
| 94 | return full;
|
|---|
| 95 |
|
|---|
| 96 | // return single code
|
|---|
| 97 | return locale.getLanguage();
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 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 | *
|
|---|
| 105 | * @param localeName the locale code.
|
|---|
| 106 | * @return the resulting locale
|
|---|
| 107 | */
|
|---|
| 108 | public static Locale getLocale(String localeName) {
|
|---|
| 109 | if (localeName.equals("he")) {
|
|---|
| 110 | localeName = "iw_IL";
|
|---|
| 111 | }
|
|---|
| 112 | else if (localeName.equals("id")) {
|
|---|
| 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 |
|
|---|
| 125 | public static String getLanguageCodeXML() {
|
|---|
| 126 | return getJOSMLocaleCode()+".";
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | public static String getLanguageCodeManifest() {
|
|---|
| 130 | return getJOSMLocaleCode()+"_";
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|