[1755] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
| 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
| 4 | import java.util.Locale;
|
---|
[2308] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[1755] | 6 |
|
---|
| 7 | public class LanguageInfo {
|
---|
[2308] | 8 |
|
---|
| 9 | /**
|
---|
| 10 | * Replies the wiki language prefix for the given locale. The wiki language
|
---|
| 11 | * prefix has the form 'Xy:' where 'Xy' is a ISO 639 language code in title
|
---|
| 12 | * case.
|
---|
[2512] | 13 | *
|
---|
[2308] | 14 | * @param locale the locale
|
---|
| 15 | * @return the wiki language prefix
|
---|
| 16 | */
|
---|
| 17 | static public String getWikiLanguagePrefix(Locale locale) {
|
---|
| 18 | String code = getJOSMLocaleCode(locale);
|
---|
| 19 | if (code.length() == 2) {
|
---|
| 20 | if (code.equals("en")) return "";
|
---|
[3418] | 21 | } else if (code.equals("zh_TW") || code.equals("zh_CN")) {
|
---|
| 22 | /* do nothing */
|
---|
[2308] | 23 | } else if (code.matches("[^_]+_[^_]+")) {
|
---|
| 24 | code = code.substring(0,2);
|
---|
[3589] | 25 | if (code.equals("en")) return "";
|
---|
[2308] | 26 | } else {
|
---|
| 27 | System.err.println(tr("Warning: failed to derive wiki language prefix from JOSM locale code ''{0}''. Using default code ''en''.", code));
|
---|
[1755] | 28 | return "";
|
---|
[2308] | 29 | }
|
---|
| 30 | return code.substring(0,1).toUpperCase() + code.substring(1) + ":";
|
---|
[1755] | 31 | }
|
---|
[2308] | 32 |
|
---|
| 33 | /**
|
---|
| 34 | * Replies the wiki language prefix for the current locale.
|
---|
[2512] | 35 | *
|
---|
[2308] | 36 | * @return the wiki language prefix
|
---|
| 37 | * @see Locale#getDefault()
|
---|
| 38 | * @see #getWikiLanguagePrefix(Locale)
|
---|
| 39 | */
|
---|
| 40 | static public String getWikiLanguagePrefix() {
|
---|
| 41 | return getWikiLanguagePrefix(Locale.getDefault());
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Replies the JOSM locale code for the default locale.
|
---|
[2512] | 46 | *
|
---|
[2308] | 47 | * @return the JOSM locale code for the default locale
|
---|
| 48 | * @see #getJOSMLocaleCode(Locale)
|
---|
| 49 | */
|
---|
| 50 | static public String getJOSMLocaleCode() {
|
---|
| 51 | return getJOSMLocaleCode(Locale.getDefault());
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | /**
|
---|
[3418] | 55 | * Replies the locale code used by JOSM for a given locale.
|
---|
[2512] | 56 | *
|
---|
[2308] | 57 | * In most cases JOSM uses the 2-character ISO 639 language code ({@see Locale#getLanguage()}
|
---|
| 58 | * to identify the locale of a localized resource, but in some cases it may use the
|
---|
| 59 | * programmatic name for locales, as replied by {@see Locale#toString()}.
|
---|
[2512] | 60 | *
|
---|
[2308] | 61 | * @param locale the locale. Replies "en" if null.
|
---|
| 62 | * @return the JOSM code for the given locale
|
---|
| 63 | */
|
---|
| 64 | static public String getJOSMLocaleCode(Locale locale) {
|
---|
| 65 | if (locale == null) return "en";
|
---|
| 66 | String full = locale.toString();
|
---|
[1755] | 67 | if (full.equals("iw_IL"))
|
---|
| 68 | return "he";
|
---|
[4211] | 69 | else if (full.equals("in"))
|
---|
| 70 | return "id";
|
---|
[4212] | 71 | else if (I18n.hasCode(full)) /* catch all non-single codes */
|
---|
[1755] | 72 | return full;
|
---|
[2308] | 73 |
|
---|
[4212] | 74 | /* return single code */
|
---|
[2308] | 75 | return locale.getLanguage();
|
---|
[1755] | 76 | }
|
---|
[2308] | 77 |
|
---|
[4211] | 78 | /**
|
---|
| 79 | * Replies the locale code used by Java for a given locale.
|
---|
| 80 | *
|
---|
| 81 | * In most cases JOSM and Java uses the same codes, but for some exceptions this is needed.
|
---|
| 82 | *
|
---|
| 83 | * @param code the locale code.
|
---|
| 84 | * @return the resulting locale
|
---|
| 85 | */
|
---|
| 86 | static public Locale getLocale(String localeName) {
|
---|
| 87 | if (localeName.equals("he")) {
|
---|
| 88 | localeName = "iw_IL";
|
---|
| 89 | }
|
---|
| 90 | else if (localeName.equals("id")) {
|
---|
| 91 | localeName = "in";
|
---|
| 92 | }
|
---|
| 93 | Locale l;
|
---|
| 94 | int i = localeName.indexOf('_');
|
---|
| 95 | if (i > 0) {
|
---|
| 96 | l = new Locale(localeName.substring(0, i), localeName.substring(i + 1));
|
---|
| 97 | } else {
|
---|
| 98 | l = new Locale(localeName);
|
---|
| 99 | }
|
---|
| 100 | return l;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[1755] | 103 | static public String getLanguageCodeXML()
|
---|
| 104 | {
|
---|
[2308] | 105 | return getJOSMLocaleCode()+".";
|
---|
[1755] | 106 | }
|
---|
| 107 | static public String getLanguageCodeManifest()
|
---|
| 108 | {
|
---|
[2308] | 109 | return getJOSMLocaleCode()+"_";
|
---|
[1755] | 110 | }
|
---|
| 111 | }
|
---|