1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Locale;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * This is a utility class that provides information about locales and allows to convert locale codes.
|
---|
10 | */
|
---|
11 | public 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 | }
|
---|
68 | if ("nb".equals(code)) { /* OSM-Wiki has "no", but no "nb" */
|
---|
69 | return "No:";
|
---|
70 | } else if ("sr@latin".equals(code)) { /* OSM-Wiki has "Sr-latn" and not Sr-latin */
|
---|
71 | return "Sr-latn:";
|
---|
72 | } else if ("de".equals(code) || "es".equals(code) || "fr".equals(code)
|
---|
73 | || "it".equals(code) || "nl".equals(code) || "ru".equals(code)
|
---|
74 | || "ja".equals(code)) {
|
---|
75 | return code.toUpperCase(Locale.ENGLISH) + ":";
|
---|
76 | } else {
|
---|
77 | return code.substring(0, 1).toUpperCase(Locale.ENGLISH) + code.substring(1) + ":";
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (type == LocaleType.BASELANGUAGE) {
|
---|
82 | if (code.matches("[^_]+_[^_]+")) {
|
---|
83 | code = code.substring(0, 2);
|
---|
84 | if ("en".equals(code))
|
---|
85 | return null;
|
---|
86 | } else {
|
---|
87 | return null;
|
---|
88 | }
|
---|
89 | } else if (type == LocaleType.DEFAULTNOTENGLISH && "en".equals(code)) {
|
---|
90 | return null;
|
---|
91 | } else if (code.matches(".+@.+")) {
|
---|
92 | return code.substring(0, 1).toUpperCase(Locale.ENGLISH)
|
---|
93 | + code.substring(1, 2)
|
---|
94 | + '-'
|
---|
95 | + code.substring(3, 4).toUpperCase(Locale.ENGLISH)
|
---|
96 | + code.substring(4)
|
---|
97 | + ':';
|
---|
98 | }
|
---|
99 | return code.substring(0, 1).toUpperCase(Locale.ENGLISH) + code.substring(1) + ':';
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Replies the wiki language prefix for the current locale.
|
---|
104 | *
|
---|
105 | * @return the wiki language prefix
|
---|
106 | * @see Locale#getDefault()
|
---|
107 | * @see #getWikiLanguagePrefix(LocaleType)
|
---|
108 | */
|
---|
109 | public static String getWikiLanguagePrefix() {
|
---|
110 | return getWikiLanguagePrefix(LocaleType.DEFAULT);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Replies the JOSM locale code for the default locale.
|
---|
115 | *
|
---|
116 | * @return the JOSM locale code for the default locale
|
---|
117 | * @see #getJOSMLocaleCode(Locale)
|
---|
118 | */
|
---|
119 | public static String getJOSMLocaleCode() {
|
---|
120 | return getJOSMLocaleCode(Locale.getDefault());
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Replies the locale code used by JOSM for a given locale.
|
---|
125 | *
|
---|
126 | * In most cases JOSM uses the 2-character ISO 639 language code ({@link Locale#getLanguage()}
|
---|
127 | * to identify the locale of a localized resource, but in some cases it may use the
|
---|
128 | * programmatic name for locales, as replied by {@link Locale#toString()}.
|
---|
129 | *
|
---|
130 | * For unknown country codes and variants this function already does fallback to
|
---|
131 | * internally known translations.
|
---|
132 | *
|
---|
133 | * @param locale the locale. Replies "en" if null.
|
---|
134 | * @return the JOSM code for the given locale
|
---|
135 | */
|
---|
136 | public static String getJOSMLocaleCode(Locale locale) {
|
---|
137 | if (locale == null) return "en";
|
---|
138 | for (String full : getLanguageCodes(locale)) {
|
---|
139 | if ("iw_IL".equals(full))
|
---|
140 | return "he";
|
---|
141 | else if ("in".equals(full))
|
---|
142 | return "id";
|
---|
143 | else if (I18n.hasCode(full)) // catch all non-single codes
|
---|
144 | return full;
|
---|
145 | }
|
---|
146 |
|
---|
147 | // return single code as fallback
|
---|
148 | return locale.getLanguage();
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Replies the locale code used by Java for a given locale.
|
---|
153 | *
|
---|
154 | * In most cases JOSM and Java uses the same codes, but for some exceptions this is needed.
|
---|
155 | *
|
---|
156 | * @param localeName the locale. Replies "en" if null.
|
---|
157 | * @return the Java code for the given locale
|
---|
158 | * @since 8232
|
---|
159 | */
|
---|
160 | public static String getJavaLocaleCode(String localeName) {
|
---|
161 | if (localeName == null) return "en";
|
---|
162 | if ("ca@valencia".equals(localeName)) {
|
---|
163 | localeName = "ca__valencia";
|
---|
164 | } else if ("sr@latin".equals(localeName)) {
|
---|
165 | localeName = "sr__latin";
|
---|
166 | } else if ("he".equals(localeName)) {
|
---|
167 | localeName = "iw_IL";
|
---|
168 | } else if ("id".equals(localeName)) {
|
---|
169 | localeName = "in";
|
---|
170 | }
|
---|
171 | return localeName;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Replies the display string used by JOSM for a given locale.
|
---|
176 | *
|
---|
177 | * In most cases returns text replied by {@link Locale#getDisplayName()}, for some
|
---|
178 | * locales an override is used (i.e. when unsupported by Java).
|
---|
179 | *
|
---|
180 | * @param locale the locale. Replies "en" if null.
|
---|
181 | * @return the display string for the given locale
|
---|
182 | * @since 8232
|
---|
183 | */
|
---|
184 | public static String getDisplayName(Locale locale) {
|
---|
185 | String currentCountry = Locale.getDefault().getCountry();
|
---|
186 | String localeCountry = locale.getCountry();
|
---|
187 | // Don't display locale country if country has been forced to current country at JOSM startup
|
---|
188 | if (currentCountry.equals(localeCountry) && !I18n.hasCode(getLanguageCodes(locale).get(0))) {
|
---|
189 | return new Locale(locale.getLanguage(), "", locale.getVariant()).getDisplayName();
|
---|
190 | }
|
---|
191 | return locale.getDisplayName();
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Replies the locale used by Java for a given language code.
|
---|
196 | *
|
---|
197 | * Accepts JOSM and Java codes as input.
|
---|
198 | *
|
---|
199 | * @param localeName the locale code.
|
---|
200 | * @return the resulting locale
|
---|
201 | */
|
---|
202 | public static Locale getLocale(String localeName) {
|
---|
203 | return getLocale(localeName, false);
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Replies the locale used by Java for a given language code.
|
---|
208 | *
|
---|
209 | * Accepts JOSM, Java and POSIX codes as input.
|
---|
210 | *
|
---|
211 | * @param localeName the locale code.
|
---|
212 | * @param useDefaultCountry if {@code true}, the current locale country will be used if no country is specified
|
---|
213 | * @return the resulting locale
|
---|
214 | * @since 15547
|
---|
215 | */
|
---|
216 | public static Locale getLocale(String localeName, boolean useDefaultCountry) {
|
---|
217 | final int encoding = localeName.indexOf('.');
|
---|
218 | if (encoding > 0) {
|
---|
219 | localeName = localeName.substring(0, encoding);
|
---|
220 | }
|
---|
221 | int country = localeName.indexOf('_');
|
---|
222 | int variant = localeName.indexOf('@');
|
---|
223 | if (variant < 0 && country >= 0)
|
---|
224 | variant = localeName.indexOf('_', country+1);
|
---|
225 | Locale l;
|
---|
226 | if (variant > 0 && country > 0) {
|
---|
227 | l = new Locale(localeName.substring(0, country), localeName.substring(country+1, variant), localeName.substring(variant + 1));
|
---|
228 | } else if (variant > 0) {
|
---|
229 | l = new Locale(localeName.substring(0, variant), "", localeName.substring(variant + 1));
|
---|
230 | } else if (country > 0) {
|
---|
231 | l = new Locale(localeName.substring(0, country), localeName.substring(country + 1));
|
---|
232 | } else {
|
---|
233 | l = new Locale(localeName, useDefaultCountry ? Locale.getDefault().getCountry() : "");
|
---|
234 | }
|
---|
235 | return l;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Check if a new language is better than a previous existing. Can be used in classes where
|
---|
240 | * multiple user supplied language marked strings appear and the best one is searched. Following
|
---|
241 | * priorities: current language, english, any other
|
---|
242 | *
|
---|
243 | * @param oldLanguage the language code of the existing string
|
---|
244 | * @param newLanguage the language code of the new string
|
---|
245 | * @return true if new one is better
|
---|
246 | * @since 8091
|
---|
247 | */
|
---|
248 | public static boolean isBetterLanguage(String oldLanguage, String newLanguage) {
|
---|
249 | if (oldLanguage == null)
|
---|
250 | return true;
|
---|
251 | String want = getJOSMLocaleCode();
|
---|
252 | return want.equals(newLanguage) || (!want.equals(oldLanguage) && newLanguage.startsWith("en"));
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Replies the language prefix for use in XML elements (with a dot appended).
|
---|
257 | *
|
---|
258 | * @return the XML language prefix
|
---|
259 | * @see #getJOSMLocaleCode()
|
---|
260 | */
|
---|
261 | public static String getLanguageCodeXML() {
|
---|
262 | String code = getJOSMLocaleCode();
|
---|
263 | code = code.replace('@', '-');
|
---|
264 | return code+'.';
|
---|
265 | }
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Replies the language prefix for use in manifests (with an underscore appended).
|
---|
269 | *
|
---|
270 | * @return the manifest language prefix
|
---|
271 | * @see #getJOSMLocaleCode()
|
---|
272 | */
|
---|
273 | public static String getLanguageCodeManifest() {
|
---|
274 | String code = getJOSMLocaleCode();
|
---|
275 | code = code.replace('@', '-');
|
---|
276 | return code+'_';
|
---|
277 | }
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Replies a list of language codes for local names. Prefixes range from very specific
|
---|
281 | * to more generic.
|
---|
282 | * <ul>
|
---|
283 | * <li>lang_COUNTRY@variant of the current locale</li>
|
---|
284 | * <li>lang@variant of the current locale</li>
|
---|
285 | * <li>lang_COUNTRY of the current locale</li>
|
---|
286 | * <li>lang of the current locale</li>
|
---|
287 | * </ul>
|
---|
288 | *
|
---|
289 | * @param l the locale to use, <code>null</code> for default locale
|
---|
290 | * @return list of codes
|
---|
291 | * @since 8283
|
---|
292 | */
|
---|
293 | public static List<String> getLanguageCodes(Locale l) {
|
---|
294 | List<String> list = new ArrayList<>(4);
|
---|
295 | if (l == null)
|
---|
296 | l = Locale.getDefault();
|
---|
297 | String lang = l.getLanguage();
|
---|
298 | String c = l.getCountry();
|
---|
299 | String v = l.getVariant();
|
---|
300 | if (c.isEmpty())
|
---|
301 | c = null;
|
---|
302 | if (v != null && !v.isEmpty()) {
|
---|
303 | if (c != null)
|
---|
304 | list.add(lang+'_'+c+'@'+v);
|
---|
305 | list.add(lang+'@'+v);
|
---|
306 | }
|
---|
307 | if (c != null)
|
---|
308 | list.add(lang+'_'+c);
|
---|
309 | list.add(lang);
|
---|
310 | return list;
|
---|
311 | }
|
---|
312 | }
|
---|