| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others |
|---|
| 2 | package org.openstreetmap.josm.tools; |
|---|
| 3 | |
|---|
| 4 | import java.util.Locale; |
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 6 | |
|---|
| 7 | public class LanguageInfo { |
|---|
| 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. |
|---|
| 13 | * |
|---|
| 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 ""; |
|---|
| 21 | } else if (code.equals("zh_TW") || code.equals("zh_CN")) { |
|---|
| 22 | /* do nothing */ |
|---|
| 23 | } else if (code.matches("[^_]+_[^_]+")) { |
|---|
| 24 | code = code.substring(0,2); |
|---|
| 25 | if (code.equals("en")) return ""; |
|---|
| 26 | } else { |
|---|
| 27 | System.err.println(tr("Warning: failed to derive wiki language prefix from JOSM locale code ''{0}''. Using default code ''en''.", code)); |
|---|
| 28 | return ""; |
|---|
| 29 | } |
|---|
| 30 | return code.substring(0,1).toUpperCase() + code.substring(1) + ":"; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Replies the wiki language prefix for the current locale. |
|---|
| 35 | * |
|---|
| 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. |
|---|
| 46 | * |
|---|
| 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 | /** |
|---|
| 55 | * Replies the locale code used by JOSM for a given locale. |
|---|
| 56 | * |
|---|
| 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()}. |
|---|
| 60 | * |
|---|
| 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(); |
|---|
| 67 | if (full.equals("iw_IL")) |
|---|
| 68 | return "he"; |
|---|
| 69 | else if (full.equals("in")) |
|---|
| 70 | return "id"; |
|---|
| 71 | else if (I18n.hasCode(full)) /* catch all non-single codes */ |
|---|
| 72 | return full; |
|---|
| 73 | |
|---|
| 74 | /* return single code */ |
|---|
| 75 | return locale.getLanguage(); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 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 | |
|---|
| 103 | static public String getLanguageCodeXML() |
|---|
| 104 | { |
|---|
| 105 | return getJOSMLocaleCode()+"."; |
|---|
| 106 | } |
|---|
| 107 | static public String getLanguageCodeManifest() |
|---|
| 108 | { |
|---|
| 109 | return getJOSMLocaleCode()+"_"; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|