source: josm/trunk/src/org/openstreetmap/josm/tools/LanguageInfo.java@ 8332

Last change on this file since 8332 was 8284, checked in by stoecker, 11 years ago

typo

  • Property svn:eol-style set to native
File size: 8.4 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[1755]2package org.openstreetmap.josm.tools;
3
[8232]4import static org.openstreetmap.josm.tools.I18n.trc;
5
[8283]6import java.util.Collection;
7import java.util.LinkedList;
[5275]8import java.util.Locale;
9
[6362]10public final class LanguageInfo {
[7509]11
[6360]12 private LanguageInfo() {
13 // Hide default constructor for utils classes
14 }
[7509]15
[5916]16 /**
17 * Type of the locale to use
18 * @since 5915
19 */
[5915]20 public enum LocaleType {
21 /** The current default language */
22 DEFAULT,
23 /** The current default language, but not english */
24 DEFAULTNOTENGLISH,
25 /** The base language (i.e. pt for pt_BR) */
26 BASELANGUAGE,
27 /** The standard english texts */
28 ENGLISH
[6142]29 }
[2308]30
31 /**
32 * Replies the wiki language prefix for the given locale. The wiki language
33 * prefix has the form 'Xy:' where 'Xy' is a ISO 639 language code in title
[5915]34 * case (or Xy_AB: for sub languages).
[2512]35 *
[6070]36 * @param type the type
[5915]37 * @return the wiki language prefix or {@code null} for {@link LocaleType#BASELANGUAGE}, when
38 * base language is identical to default or english
[5916]39 * @since 5915
[2308]40 */
[6889]41 public static String getWikiLanguagePrefix(LocaleType type) {
[5915]42 if(type == LocaleType.ENGLISH)
43 return "";
44
45 String code = getJOSMLocaleCode();
46 if(type == LocaleType.BASELANGUAGE) {
47 if(code.matches("[^_]+_[^_]+")) {
48 code = code.substring(0,2);
[6223]49 if ("en".equals(code))
[5915]50 return null;
51 } else {
52 return null;
53 }
[8227]54 } else if(type == LocaleType.DEFAULTNOTENGLISH && "en".equals(code)) {
[5915]55 return null;
[8227]56 } else if(code.matches(".+@.+")) {
57 return code.substring(0,1).toUpperCase() + code.substring(1,2)
58 + "-" + code.substring(3,4).toUpperCase() + code.substring(4) + ":";
59 }
[2308]60 return code.substring(0,1).toUpperCase() + code.substring(1) + ":";
[1755]61 }
[2308]62
63 /**
64 * Replies the wiki language prefix for the current locale.
[2512]65 *
[2308]66 * @return the wiki language prefix
67 * @see Locale#getDefault()
[5915]68 * @see #getWikiLanguagePrefix(LocaleType)
[2308]69 */
[6889]70 public static String getWikiLanguagePrefix() {
[5915]71 return getWikiLanguagePrefix(LocaleType.DEFAULT);
[2308]72 }
73
74 /**
75 * Replies the JOSM locale code for the default locale.
[2512]76 *
[2308]77 * @return the JOSM locale code for the default locale
78 * @see #getJOSMLocaleCode(Locale)
79 */
[6889]80 public static String getJOSMLocaleCode() {
[2308]81 return getJOSMLocaleCode(Locale.getDefault());
82 }
83
84 /**
[3418]85 * Replies the locale code used by JOSM for a given locale.
[2512]86 *
[5266]87 * In most cases JOSM uses the 2-character ISO 639 language code ({@link Locale#getLanguage()}
[2308]88 * to identify the locale of a localized resource, but in some cases it may use the
[5266]89 * programmatic name for locales, as replied by {@link Locale#toString()}.
[8283]90 *
[8284]91 * For unknown country codes and variants this function already does fallback to
[8283]92 * internally known translations.
[2512]93 *
[2308]94 * @param locale the locale. Replies "en" if null.
95 * @return the JOSM code for the given locale
96 */
[6889]97 public static String getJOSMLocaleCode(Locale locale) {
[2308]98 if (locale == null) return "en";
[8283]99 for(String full : getLanguageCodes(locale)) {
100 if ("iw_IL".equals(full))
101 return "he";
102 else if ("in".equals(full))
103 return "id";
104 else if (I18n.hasCode(full)) // catch all non-single codes
105 return full;
106 }
[2308]107
[8283]108 // return single code as fallback
[2308]109 return locale.getLanguage();
[1755]110 }
[2308]111
[4211]112 /**
113 * Replies the locale code used by Java for a given locale.
114 *
[8233]115 * In most cases JOSM and Java uses the same codes, but for some exceptions this is needed.
116 *
[8232]117 * @param locale the locale. Replies "en" if null.
118 * @return the Java code for the given locale
119 * @since 8232
120 */
121 public static String getJavaLocaleCode(String localeName) {
122 if (localeName == null) return "en";
123 if ("ca@valencia".equals(localeName)) {
124 localeName = "ca__valencia";
125 } else if ("he".equals(localeName)) {
126 localeName = "iw_IL";
127 } else if ("id".equals(localeName)) {
128 localeName = "in";
129 }
130 return localeName;
131 }
132
133 /**
134 * Replies the display string used by JOSM for a given locale.
135 *
136 * In most cases returns text replied by {@link Locale#getDisplayName()}, for some
137 * locales an override is used (i.e. when unsupported by Java).
138 *
139 * @param locale the locale. Replies "en" if null.
140 * @return the display string for the given locale
141 * @since 8232
142 */
143 public static String getDisplayName(Locale locale) {
[8241]144 /*String full = locale.toString();
[8232]145 if ("ca__valencia".equals(full))
[8241]146 return t_r_c("language", "Valencian");*/
[8232]147
148 return locale.getDisplayName();
149 }
150
151 /**
[8233]152 * Replies the locale used by Java for a given language code.
[8232]153 *
[8233]154 * Accepts JOSM and Java codes as input.
[4211]155 *
[5275]156 * @param localeName the locale code.
[4211]157 * @return the resulting locale
158 */
[6889]159 public static Locale getLocale(String localeName) {
[8283]160 int country = localeName.indexOf("_");
161 int variant = localeName.indexOf("@");
162 if (variant < 0 && country >= 0)
163 variant = localeName.indexOf("_", country+1);
[4211]164 Locale l;
[8283]165 if (variant > 0 && country > 0) {
166 l = new Locale(localeName.substring(0, country), localeName.substring(country+1, variant), localeName.substring(variant + 1));
167 } else if (variant > 0) {
168 l = new Locale(localeName.substring(0, variant), "", localeName.substring(variant + 1));
169 } else if (country > 0) {
170 l = new Locale(localeName.substring(0, country), localeName.substring(country + 1));
[4211]171 } else {
172 l = new Locale(localeName);
173 }
174 return l;
175 }
176
[8091]177 /**
178 * Check if a new language is better than a previous existing. Can be used in classes where
179 * multiple user supplied language marked strings appear and the best one is searched. Following
180 * priorities: current language, english, any other
181 *
182 * @param oldLanguage the language code of the existing string
183 * @param newLanguage the language code of the new string
184 * @return true if new one is better
185 * @since 8091
186 */
187 public static boolean isBetterLanguage(String oldLanguage, String newLanguage) {
188 if (oldLanguage == null)
189 return true;
190 String want = getJOSMLocaleCode();
191 return want.equals(newLanguage) || (!want.equals(oldLanguage) && newLanguage.startsWith("en"));
192 }
193
194 /**
195 * Replies the language prefix for use in XML elements (with a dot appended).
196 *
197 * @return the XML language prefix
198 * @see #getJOSMLocaleCode()
199 */
[6889]200 public static String getLanguageCodeXML() {
[8227]201 String code = getJOSMLocaleCode();
202 code = code.replace("@", "-");
203 return code+".";
[1755]204 }
[7509]205
[8091]206 /**
207 * Replies the language prefix for use in manifests (with an underscore appended).
208 *
209 * @return the manifest language prefix
210 * @see #getJOSMLocaleCode()
211 */
[6889]212 public static String getLanguageCodeManifest() {
[8227]213 String code = getJOSMLocaleCode();
214 code = code.replace("@", "-");
215 return code+"_";
[1755]216 }
[8283]217
218 /**
219 * Replies a list of language codes for local names. Prefixes range from very specific
220 * to more generic.
221 * <ul>
222 * <li>lang_COUNTRY@variant of the current locale</li>
223 * <li>lang@variant of the current locale</li>
224 * <li>lang_COUNTRY of the current locale</li>
225 * <li>lang of the current locale</li>
226 * </ul>
227 *
228 * @param locale the locale to use, <code>null</code> for default locale
229 * @since 8283
230 * @return list of codes
231 */
232 public static Collection<String> getLanguageCodes(Locale l) {
233 Collection<String> list = new LinkedList<String>();
234 if(l == null)
235 l = Locale.getDefault();
236 String lang = l.getLanguage();
237 String c = l.getCountry();
238 String v = l.getVariant();
239 if(c.isEmpty())
240 c = null;
241 if(v != null && !v.isEmpty()) {
242 if(c != null)
243 list.add(lang+"_"+c+"@"+v);
244 list.add(lang+"@"+v);
245 }
246 if(c != null)
247 list.add(lang+"_"+c);
248 list.add(lang);
249 return list;
250 }
[1755]251}
Note: See TracBrowser for help on using the repository browser.