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