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

Last change on this file since 14660 was 14660, checked in by stoecker, 5 years ago

see #17173 - proper prefixes for OSM wiki

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