001 // License: GPL. Copyright 2007 by Immanuel Scholz and others
002 package org.openstreetmap.josm.tools;
003
004 import static org.openstreetmap.josm.tools.I18n.tr;
005
006 import java.util.Locale;
007
008 public class LanguageInfo {
009
010 /**
011 * Replies the wiki language prefix for the given locale. The wiki language
012 * prefix has the form 'Xy:' where 'Xy' is a ISO 639 language code in title
013 * case.
014 *
015 * @param locale the locale
016 * @return the wiki language prefix
017 */
018 static public String getWikiLanguagePrefix(Locale locale) {
019 String code = getJOSMLocaleCode(locale);
020 if (code.length() == 2) {
021 if (code.equals("en")) return "";
022 } else if (code.equals("zh_TW") || code.equals("zh_CN")) {
023 /* do nothing */
024 } else if (code.matches("[^_]+_[^_]+")) {
025 code = code.substring(0,2);
026 if (code.equals("en")) return "";
027 } else {
028 System.err.println(tr("Warning: failed to derive wiki language prefix from JOSM locale code ''{0}''. Using default code ''en''.", code));
029 return "";
030 }
031 return code.substring(0,1).toUpperCase() + code.substring(1) + ":";
032 }
033
034 /**
035 * Replies the wiki language prefix for the current locale.
036 *
037 * @return the wiki language prefix
038 * @see Locale#getDefault()
039 * @see #getWikiLanguagePrefix(Locale)
040 */
041 static public String getWikiLanguagePrefix() {
042 return getWikiLanguagePrefix(Locale.getDefault());
043 }
044
045 /**
046 * Replies the JOSM locale code for the default locale.
047 *
048 * @return the JOSM locale code for the default locale
049 * @see #getJOSMLocaleCode(Locale)
050 */
051 static public String getJOSMLocaleCode() {
052 return getJOSMLocaleCode(Locale.getDefault());
053 }
054
055 /**
056 * Replies the locale code used by JOSM for a given locale.
057 *
058 * In most cases JOSM uses the 2-character ISO 639 language code ({@link Locale#getLanguage()}
059 * to identify the locale of a localized resource, but in some cases it may use the
060 * programmatic name for locales, as replied by {@link Locale#toString()}.
061 *
062 * @param locale the locale. Replies "en" if null.
063 * @return the JOSM code for the given locale
064 */
065 static public String getJOSMLocaleCode(Locale locale) {
066 if (locale == null) return "en";
067 String full = locale.toString();
068 if (full.equals("iw_IL"))
069 return "he";
070 else if (full.equals("in"))
071 return "id";
072 else if (I18n.hasCode(full)) /* catch all non-single codes */
073 return full;
074
075 /* return single code */
076 return locale.getLanguage();
077 }
078
079 /**
080 * Replies the locale code used by Java for a given locale.
081 *
082 * In most cases JOSM and Java uses the same codes, but for some exceptions this is needed.
083 *
084 * @param localeName the locale code.
085 * @return the resulting locale
086 */
087 static public Locale getLocale(String localeName) {
088 if (localeName.equals("he")) {
089 localeName = "iw_IL";
090 }
091 else if (localeName.equals("id")) {
092 localeName = "in";
093 }
094 Locale l;
095 int i = localeName.indexOf('_');
096 if (i > 0) {
097 l = new Locale(localeName.substring(0, i), localeName.substring(i + 1));
098 } else {
099 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 }