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