source: josm/trunk/src/org/openstreetmap/josm/tools/I18n.java@ 16439

Last change on this file since 16439 was 16439, checked in by stoecker, 4 years ago

Add Persian language, see #19147, readd Icelandic, i18n update

  • Property svn:eol-style set to native
File size: 31.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.BufferedInputStream;
5import java.io.File;
6import java.io.IOException;
7import java.io.InputStream;
8import java.lang.annotation.Retention;
9import java.lang.annotation.RetentionPolicy;
10import java.net.URL;
11import java.nio.charset.StandardCharsets;
12import java.nio.file.InvalidPathException;
13import java.text.MessageFormat;
14import java.util.ArrayList;
15import java.util.Arrays;
16import java.util.Collection;
17import java.util.Comparator;
18import java.util.HashMap;
19import java.util.Locale;
20import java.util.Map;
21import java.util.regex.Matcher;
22import java.util.regex.Pattern;
23import java.util.zip.ZipEntry;
24import java.util.zip.ZipFile;
25
26import org.openstreetmap.josm.data.osm.TagMap;
27
28/**
29 * Internationalisation support.
30 *
31 * @author Immanuel.Scholz
32 */
33public final class I18n {
34
35 private static final String CORE_TRANS_DIRECTORY = "/data/";
36 private static final String PLUGIN_TRANS_DIRECTORY = "data/";
37
38 /**
39 * This annotates strings which do not permit a clean i18n. This is mostly due to strings
40 * containing two nouns which can occur in singular or plural form.
41 * <br>
42 * No behaviour is associated with this annotation.
43 */
44 @Retention(RetentionPolicy.SOURCE)
45 public @interface QuirkyPluralString {
46 }
47
48 private I18n() {
49 // Hide default constructor for utils classes
50 }
51
52 /**
53 * Enumeration of possible plural modes. It allows us to identify and implement logical conditions of
54 * plural forms defined on <a href="https://help.launchpad.net/Translations/PluralForms">Launchpad</a>.
55 * See <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html">CLDR</a>
56 * for another complete list.
57 * @see #pluralEval
58 */
59 private enum PluralMode {
60 /** Plural = Not 1. This is the default for many languages, including English: 1 day, but 0 days or 2 days. */
61 MODE_NOTONE,
62 /** No plural. Mainly for Asian languages (Indonesian, Chinese, Japanese, ...) */
63 MODE_NONE,
64 /** Plural = Greater than 1. For some latin languages (French, Brazilian Portuguese) */
65 MODE_GREATERONE,
66 /* Special mode for
67 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ar">Arabic</a>.*/
68 MODE_AR,
69 /** Special mode for
70 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#cs">Czech</a>. */
71 MODE_CS,
72 /** Special mode for
73 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#pl">Polish</a>. */
74 MODE_PL,
75 /* Special mode for
76 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ro">Romanian</a>.*
77 MODE_RO,*/
78 /** Special mode for
79 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#lt">Lithuanian</a>. */
80 MODE_LT,
81 /** Special mode for
82 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ru">Russian</a>. */
83 MODE_RU,
84 /** Special mode for
85 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#sk">Slovak</a>. */
86 MODE_SK,
87 /* Special mode for
88 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#sl">Slovenian</a>.*
89 MODE_SL,*/
90 }
91
92 private static volatile PluralMode pluralMode = PluralMode.MODE_NOTONE; /* english default */
93 private static volatile String loadedCode = "en";
94
95 /** Map (english/locale) of singular strings **/
96 private static volatile Map<String, String> strings;
97 /** Map (english/locale) of plural strings **/
98 private static volatile Map<String, String[]> pstrings;
99 private static Locale originalLocale = Locale.getDefault();
100 private static Map<String, PluralMode> languages = new HashMap<>();
101 // NOTE: check also WikiLanguage handling in LanguageInfo.java when adding new languages
102 static {
103 languages.put("ar", PluralMode.MODE_AR);
104 languages.put("ast", PluralMode.MODE_NOTONE);
105 languages.put("bg", PluralMode.MODE_NOTONE);
106 languages.put("be", PluralMode.MODE_RU);
107 languages.put("ca", PluralMode.MODE_NOTONE);
108 languages.put("ca@valencia", PluralMode.MODE_NOTONE);
109 languages.put("cs", PluralMode.MODE_CS);
110 languages.put("da", PluralMode.MODE_NOTONE);
111 languages.put("de", PluralMode.MODE_NOTONE);
112 languages.put("el", PluralMode.MODE_NOTONE);
113 languages.put("en_AU", PluralMode.MODE_NOTONE);
114 //languages.put("en_CA", PluralMode.MODE_NOTONE);
115 languages.put("en_GB", PluralMode.MODE_NOTONE);
116 languages.put("es", PluralMode.MODE_NOTONE);
117 languages.put("et", PluralMode.MODE_NOTONE);
118 //languages.put("eu", PluralMode.MODE_NOTONE);
119 languages.put("fa", PluralMode.MODE_NONE);
120 languages.put("fi", PluralMode.MODE_NOTONE);
121 languages.put("fr", PluralMode.MODE_GREATERONE);
122 languages.put("gl", PluralMode.MODE_NOTONE);
123 //languages.put("he", PluralMode.MODE_NOTONE);
124 languages.put("hu", PluralMode.MODE_NOTONE);
125 languages.put("id", PluralMode.MODE_NONE);
126 languages.put("is", PluralMode.MODE_NOTONE);
127 languages.put("it", PluralMode.MODE_NOTONE);
128 languages.put("ja", PluralMode.MODE_NONE);
129 languages.put("ko", PluralMode.MODE_NONE);
130 languages.put("km", PluralMode.MODE_NONE);
131 languages.put("lt", PluralMode.MODE_LT);
132 languages.put("mr", PluralMode.MODE_NOTONE);
133 languages.put("nb", PluralMode.MODE_NOTONE);
134 languages.put("nl", PluralMode.MODE_NOTONE);
135 languages.put("pl", PluralMode.MODE_PL);
136 languages.put("pt", PluralMode.MODE_NOTONE);
137 languages.put("pt_BR", PluralMode.MODE_GREATERONE);
138 //languages.put("ro", PluralMode.MODE_RO);
139 languages.put("ru", PluralMode.MODE_RU);
140 languages.put("sk", PluralMode.MODE_SK);
141 //languages.put("sl", PluralMode.MODE_SL);
142 languages.put("sv", PluralMode.MODE_NOTONE);
143 //languages.put("tr", PluralMode.MODE_NONE);
144 languages.put("uk", PluralMode.MODE_RU);
145 languages.put("vi", PluralMode.MODE_NONE);
146 languages.put("zh_CN", PluralMode.MODE_NONE);
147 languages.put("zh_TW", PluralMode.MODE_NONE);
148 }
149
150 private static final String HIRAGANA = "hira";
151 private static final String KATAKANA = "kana";
152 private static final String LATIN = "latn";
153 private static final String PINYIN = "pinyin";
154 private static final String ROMAJI = "rm";
155
156 // Matches ISO-639 two and three letters language codes + scripts
157 private static final Pattern LANGUAGE_NAMES = Pattern.compile(
158 "name:(\\p{Lower}{2,3})(?:[-_](?i:(" + String.join("|", HIRAGANA, KATAKANA, LATIN, PINYIN, ROMAJI) + ")))?");
159
160 private static String format(String text, Object... objects) {
161 if (objects.length == 0 && !text.contains("'")) {
162 return text;
163 }
164 try {
165 return MessageFormat.format(text, objects);
166 } catch (InvalidPathException e) {
167 System.err.println("!!! Unable to format '" + text + "': " + e.getMessage());
168 e.printStackTrace();
169 return null;
170 }
171 }
172
173 /**
174 * Translates some text for the current locale.
175 * These strings are collected by a script that runs on the source code files.
176 * After translation, the localizations are distributed with the main program.
177 * <br>
178 * For example, <code>tr("JOSM''s default value is ''{0}''.", val)</code>.
179 * <br>
180 * Use {@link #trn} for distinguishing singular from plural text, i.e.,
181 * do not use {@code tr(size == 1 ? "singular" : "plural")} nor
182 * {@code size == 1 ? tr("singular") : tr("plural")}
183 *
184 * @param text the text to translate.
185 * Must be a string literal. (No constants or local vars.)
186 * Can be broken over multiple lines.
187 * An apostrophe ' must be quoted by another apostrophe.
188 * @param objects the parameters for the string.
189 * Mark occurrences in {@code text} with <code>{0}</code>, <code>{1}</code>, ...
190 * @return the translated string.
191 * @see #trn
192 * @see #trc
193 * @see #trnc
194 */
195 public static String tr(String text, Object... objects) {
196 if (text == null) return null;
197 return format(gettext(text, null), objects);
198 }
199
200 /**
201 * Translates some text in a context for the current locale.
202 * There can be different translations for the same text within different contexts.
203 *
204 * @param context string that helps translators to find an appropriate
205 * translation for {@code text}.
206 * @param text the text to translate.
207 * @return the translated string.
208 * @see #tr
209 * @see #trn
210 * @see #trnc
211 */
212 public static String trc(String context, String text) {
213 if (context == null)
214 return tr(text);
215 if (text == null)
216 return null;
217 return format(gettext(text, context), (Object) null);
218 }
219
220 public static String trcLazy(String context, String text) {
221 if (context == null)
222 return tr(text);
223 if (text == null)
224 return null;
225 return format(gettextLazy(text, context), (Object) null);
226 }
227
228 /**
229 * Marks a string for translation (such that a script can harvest
230 * the translatable strings from the source files).
231 *
232 * For example, <code>
233 * String[] options = new String[] {marktr("up"), marktr("down")};
234 * lbl.setText(tr(options[0]));</code>
235 * @param text the string to be marked for translation.
236 * @return {@code text} unmodified.
237 */
238 public static String marktr(String text) {
239 return text;
240 }
241
242 public static String marktrc(String context, String text) {
243 return text;
244 }
245
246 /**
247 * Translates some text for the current locale and distinguishes between
248 * {@code singularText} and {@code pluralText} depending on {@code n}.
249 * <br>
250 * For instance, {@code trn("There was an error!", "There were errors!", i)} or
251 * <code>trn("Found {0} error in {1}!", "Found {0} errors in {1}!", i, Integer.toString(i), url)</code>.
252 *
253 * @param singularText the singular text to translate.
254 * Must be a string literal. (No constants or local vars.)
255 * Can be broken over multiple lines.
256 * An apostrophe ' must be quoted by another apostrophe.
257 * @param pluralText the plural text to translate.
258 * Must be a string literal. (No constants or local vars.)
259 * Can be broken over multiple lines.
260 * An apostrophe ' must be quoted by another apostrophe.
261 * @param n a number to determine whether {@code singularText} or {@code pluralText} is used.
262 * @param objects the parameters for the string.
263 * Mark occurrences in {@code singularText} and {@code pluralText} with <code>{0}</code>, <code>{1}</code>, ...
264 * @return the translated string.
265 * @see #tr
266 * @see #trc
267 * @see #trnc
268 */
269 public static String trn(String singularText, String pluralText, long n, Object... objects) {
270 return format(gettextn(singularText, pluralText, null, n), objects);
271 }
272
273 /**
274 * Translates some text in a context for the current locale and distinguishes between
275 * {@code singularText} and {@code pluralText} depending on {@code n}.
276 * There can be different translations for the same text within different contexts.
277 *
278 * @param context string that helps translators to find an appropriate
279 * translation for {@code text}.
280 * @param singularText the singular text to translate.
281 * Must be a string literal. (No constants or local vars.)
282 * Can be broken over multiple lines.
283 * An apostrophe ' must be quoted by another apostrophe.
284 * @param pluralText the plural text to translate.
285 * Must be a string literal. (No constants or local vars.)
286 * Can be broken over multiple lines.
287 * An apostrophe ' must be quoted by another apostrophe.
288 * @param n a number to determine whether {@code singularText} or {@code pluralText} is used.
289 * @param objects the parameters for the string.
290 * Mark occurrences in {@code singularText} and {@code pluralText} with <code>{0}</code>, <code>{1}</code>, ...
291 * @return the translated string.
292 * @see #tr
293 * @see #trc
294 * @see #trn
295 */
296 public static String trnc(String context, String singularText, String pluralText, long n, Object... objects) {
297 return format(gettextn(singularText, pluralText, context, n), objects);
298 }
299
300 private static String gettext(String text, String ctx, boolean lazy) {
301 int i;
302 if (ctx == null && text.startsWith("_:") && (i = text.indexOf('\n')) >= 0) {
303 ctx = text.substring(2, i-1);
304 text = text.substring(i+1);
305 }
306 if (strings != null) {
307 String trans = strings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
308 if (trans != null)
309 return trans;
310 }
311 if (pstrings != null) {
312 i = pluralEval(1);
313 String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
314 if (trans != null && trans.length > i)
315 return trans[i];
316 }
317 return lazy ? gettext(text, null) : text;
318 }
319
320 private static String gettext(String text, String ctx) {
321 return gettext(text, ctx, false);
322 }
323
324 /* try without context, when context try fails */
325 private static String gettextLazy(String text, String ctx) {
326 return gettext(text, ctx, true);
327 }
328
329 private static String gettextn(String text, String plural, String ctx, long num) {
330 int i;
331 if (ctx == null && text.startsWith("_:") && (i = text.indexOf('\n')) >= 0) {
332 ctx = text.substring(2, i-1);
333 text = text.substring(i+1);
334 }
335 if (pstrings != null) {
336 i = pluralEval(num);
337 String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
338 if (trans != null && trans.length > i)
339 return trans[i];
340 }
341
342 return num == 1 ? text : plural;
343 }
344
345 /**
346 * Escapes the special i18n characters <code>'{}</code> with quotes.
347 * @param msg unescaped string
348 * @return escaped string
349 * @since 4477
350 */
351 public static String escape(String msg) {
352 if (msg == null) return null;
353 return msg.replace("\'", "\'\'").replace("{", "\'{\'").replace("}", "\'}\'");
354 }
355
356 private static URL getTranslationFile(String lang) {
357 return I18n.class.getResource(CORE_TRANS_DIRECTORY + lang.replace('@', '-') + ".lang");
358 }
359
360 /**
361 * Get a list of all available JOSM Translations.
362 * @return an array of locale objects.
363 */
364 public static Locale[] getAvailableTranslations() {
365 Collection<Locale> v = new ArrayList<>(languages.size());
366 if (getTranslationFile("en") != null) {
367 for (String loc : languages.keySet()) {
368 if (getTranslationFile(loc) != null) {
369 v.add(LanguageInfo.getLocale(loc));
370 }
371 }
372 }
373 v.add(Locale.ENGLISH);
374 Locale[] l = new Locale[v.size()];
375 l = v.toArray(l);
376 Arrays.sort(l, Comparator.comparing(Locale::toString));
377 return l;
378 }
379
380 /**
381 * Determines if a language exists for the given code.
382 * @param code The language code
383 * @return {@code true} if a language exists, {@code false} otherwise
384 */
385 public static boolean hasCode(String code) {
386 return languages.containsKey(code);
387 }
388
389 static String setupJavaLocaleProviders() {
390 // Look up SPI providers first (for JosmDecimalFormatSymbolsProvider).
391 // Enable CLDR locale provider on Java 8 to get additional languages, such as Khmer.
392 // https://docs.oracle.com/javase/8/docs/technotes/guides/intl/enhancements.8.html#cldr
393 // FIXME: This must be updated after we switch to Java 9.
394 // See https://docs.oracle.com/javase/9/docs/api/java/util/spi/LocaleServiceProvider.html
395 try {
396 try {
397 // First check we're able to open a stream to our own SPI file
398 // Java will fail on Windows if the jar file is in a folder with a space character!
399 I18n.class.getResourceAsStream("/META-INF/services/java.text.spi.DecimalFormatSymbolsProvider").close();
400 // Don't call Utils.updateSystemProperty to avoid spurious log at startup
401 return System.setProperty("java.locale.providers", "SPI,JRE,CLDR");
402 } catch (RuntimeException | IOException e) {
403 // Don't call Logging class, it may not be fully initialized yet
404 System.err.println("Unable to set SPI locale provider: " + e.getMessage());
405 }
406 } catch (SecurityException e) {
407 // Don't call Logging class, it may not be fully initialized yet
408 System.err.println("Unable to set locale providers: " + e.getMessage());
409 }
410 return System.setProperty("java.locale.providers", "JRE,CLDR");
411 }
412
413 /**
414 * I18n initialization.
415 */
416 public static void init() {
417 setupJavaLocaleProviders();
418
419 /* try initial language settings, may be changed later again */
420 if (!load(LanguageInfo.getJOSMLocaleCode())) {
421 Locale.setDefault(new Locale("en", Locale.getDefault().getCountry()));
422 }
423 }
424
425 /**
426 * I18n initialization for plugins.
427 * @param source file path/name of the JAR or Zip file containing translation strings
428 * @since 4159
429 */
430 public static void addTexts(File source) {
431 if ("en".equals(loadedCode))
432 return;
433 final ZipEntry enfile = new ZipEntry(PLUGIN_TRANS_DIRECTORY + "en.lang");
434 final ZipEntry langfile = new ZipEntry(PLUGIN_TRANS_DIRECTORY + loadedCode + ".lang");
435 try (
436 ZipFile zipFile = new ZipFile(source, StandardCharsets.UTF_8);
437 InputStream orig = zipFile.getInputStream(enfile);
438 InputStream trans = zipFile.getInputStream(langfile)
439 ) {
440 if (orig != null && trans != null)
441 load(orig, trans, true);
442 } catch (IOException | InvalidPathException e) {
443 Logging.trace(e);
444 }
445 }
446
447 private static boolean load(String l) {
448 if ("en".equals(l) || "en_US".equals(l)) {
449 strings = null;
450 pstrings = null;
451 loadedCode = "en";
452 pluralMode = PluralMode.MODE_NOTONE;
453 return true;
454 }
455 URL en = getTranslationFile("en");
456 if (en == null)
457 return false;
458 URL tr = getTranslationFile(l);
459 if (tr == null || !languages.containsKey(l)) {
460 return false;
461 }
462 try (
463 InputStream enStream = Utils.openStream(en);
464 InputStream trStream = Utils.openStream(tr)
465 ) {
466 if (load(enStream, trStream, false)) {
467 pluralMode = languages.get(l);
468 loadedCode = l;
469 return true;
470 }
471 } catch (IOException e) {
472 // Ignore exception
473 Logging.trace(e);
474 }
475 return false;
476 }
477
478 private static boolean load(InputStream en, InputStream tr, boolean add) {
479 Map<String, String> s;
480 Map<String, String[]> p;
481 if (add) {
482 s = strings;
483 p = pstrings;
484 } else {
485 s = new HashMap<>();
486 p = new HashMap<>();
487 }
488 /* file format:
489 Files are always a group. English file and translated file must provide identical datasets.
490
491 for all single strings:
492 {
493 unsigned short (2 byte) stringlength
494 - length 0 indicates missing translation
495 - length 0xFFFE indicates translation equal to original, but otherwise is equal to length 0
496 string
497 }
498 unsigned short (2 byte) 0xFFFF (marks end of single strings)
499 for all multi strings:
500 {
501 unsigned char (1 byte) stringcount
502 - count 0 indicates missing translations
503 - count 0xFE indicates translations equal to original, but otherwise is equal to length 0
504 for stringcount
505 unsigned short (2 byte) stringlength
506 string
507 }
508 */
509 try {
510 InputStream ens = new BufferedInputStream(en);
511 InputStream trs = new BufferedInputStream(tr);
512 byte[] enlen = new byte[2];
513 byte[] trlen = new byte[2];
514 boolean multimode = false;
515 byte[] str = new byte[4096];
516 for (;;) {
517 if (multimode) {
518 int ennum = ens.read();
519 int trnum = trs.read();
520 if (trnum == 0xFE) /* marks identical string, handle equally to non-translated */
521 trnum = 0;
522 if ((ennum == -1 && trnum != -1) || (ennum != -1 && trnum == -1)) /* files do not match */
523 return false;
524 if (ennum == -1) {
525 break;
526 }
527 String[] enstrings = new String[ennum];
528 for (int i = 0; i < ennum; ++i) {
529 int val = ens.read(enlen);
530 if (val != 2) /* file corrupt */
531 return false;
532 val = (enlen[0] < 0 ? 256+enlen[0] : enlen[0])*256+(enlen[1] < 0 ? 256+enlen[1] : enlen[1]);
533 if (val > str.length) {
534 str = new byte[val];
535 }
536 int rval = ens.read(str, 0, val);
537 if (rval != val) /* file corrupt */
538 return false;
539 enstrings[i] = new String(str, 0, val, StandardCharsets.UTF_8);
540 }
541 String[] trstrings = new String[trnum];
542 for (int i = 0; i < trnum; ++i) {
543 int val = trs.read(trlen);
544 if (val != 2) /* file corrupt */
545 return false;
546 val = (trlen[0] < 0 ? 256+trlen[0] : trlen[0])*256+(trlen[1] < 0 ? 256+trlen[1] : trlen[1]);
547 if (val > str.length) {
548 str = new byte[val];
549 }
550 int rval = trs.read(str, 0, val);
551 if (rval != val) /* file corrupt */
552 return false;
553 trstrings[i] = new String(str, 0, val, StandardCharsets.UTF_8);
554 }
555 if (trnum > 0 && !p.containsKey(enstrings[0])) {
556 p.put(enstrings[0], trstrings);
557 }
558 } else {
559 int enval = ens.read(enlen);
560 int trval = trs.read(trlen);
561 if (enval != trval) /* files do not match */
562 return false;
563 if (enval == -1) {
564 break;
565 }
566 if (enval != 2) /* files corrupt */
567 return false;
568 enval = (enlen[0] < 0 ? 256+enlen[0] : enlen[0])*256+(enlen[1] < 0 ? 256+enlen[1] : enlen[1]);
569 trval = (trlen[0] < 0 ? 256+trlen[0] : trlen[0])*256+(trlen[1] < 0 ? 256+trlen[1] : trlen[1]);
570 if (trval == 0xFFFE) /* marks identical string, handle equally to non-translated */
571 trval = 0;
572 if (enval == 0xFFFF) {
573 multimode = true;
574 if (trval != 0xFFFF) /* files do not match */
575 return false;
576 } else {
577 if (enval > str.length) {
578 str = new byte[enval];
579 }
580 if (trval > str.length) {
581 str = new byte[trval];
582 }
583 int val = ens.read(str, 0, enval);
584 if (val != enval) /* file corrupt */
585 return false;
586 String enstr = new String(str, 0, enval, StandardCharsets.UTF_8);
587 if (trval != 0) {
588 val = trs.read(str, 0, trval);
589 if (val != trval) /* file corrupt */
590 return false;
591 String trstr = new String(str, 0, trval, StandardCharsets.UTF_8);
592 if (!s.containsKey(enstr))
593 s.put(enstr, trstr);
594 }
595 }
596 }
597 }
598 } catch (IOException e) {
599 Logging.trace(e);
600 return false;
601 }
602 if (!s.isEmpty()) {
603 strings = s;
604 pstrings = p;
605 return true;
606 }
607 return false;
608 }
609
610 /**
611 * Sets the default locale (see {@link Locale#setDefault(Locale)} to the local
612 * given by <code>localName</code>.
613 *
614 * Ignored if localeName is null. If the locale with name <code>localName</code>
615 * isn't found the default local is set to <code>en</code> (english).
616 *
617 * @param localeName the locale name. Ignored if null.
618 */
619 public static void set(String localeName) {
620 if (localeName != null) {
621 Locale l = LanguageInfo.getLocale(localeName, true);
622 if (load(LanguageInfo.getJOSMLocaleCode(l))) {
623 Locale.setDefault(l);
624 } else {
625 if (!"en".equals(l.getLanguage())) {
626 Logging.info(tr("Unable to find translation for the locale {0}. Reverting to {1}.",
627 LanguageInfo.getDisplayName(l), LanguageInfo.getDisplayName(Locale.getDefault())));
628 } else {
629 strings = null;
630 pstrings = null;
631 }
632 }
633 }
634 }
635
636 /**
637 * Updates the default locale : overrides the numbering system, if defined in internal boudnaries.xml for the current language/country.
638 * @since 16109
639 */
640 public static void initializeNumberingFormat() {
641 Locale l = Locale.getDefault();
642 TagMap tags = Territories.getCustomTags(l.getCountry());
643 if (tags != null) {
644 String numberingSystem = tags.get("ldml:nu:" + l.getLanguage());
645 if (numberingSystem != null && !numberingSystem.equals(l.getExtension(Locale.UNICODE_LOCALE_EXTENSION))) {
646 Locale.setDefault(new Locale.Builder()
647 .setLanguage(l.getLanguage())
648 .setRegion(l.getCountry())
649 .setVariant(l.getVariant())
650 .setExtension(Locale.UNICODE_LOCALE_EXTENSION, numberingSystem)
651 .build());
652 }
653 }
654 }
655
656 private static int pluralEval(long n) {
657 switch(pluralMode) {
658 case MODE_NOTONE: /* bg, da, de, el, en, en_AU, en_CA, en_GB, es, et, eu, fi, gl, is, it, iw_IL, mr, nb, nl, sv */
659 return (n != 1) ? 1 : 0;
660 case MODE_NONE: /* id, vi, ja, km, tr, zh_CN, zh_TW */
661 return 0;
662 case MODE_GREATERONE: /* fr, pt_BR */
663 return (n > 1) ? 1 : 0;
664 case MODE_CS:
665 return (n == 1) ? 0 : (((n >= 2) && (n <= 4)) ? 1 : 2);
666 case MODE_AR:
667 return ((n == 0) ? 0 : ((n == 1) ? 1 : ((n == 2) ? 2 : ((((n % 100) >= 3)
668 && ((n % 100) <= 10)) ? 3 : ((((n % 100) >= 11) && ((n % 100) <= 99)) ? 4 : 5)))));
669 case MODE_PL:
670 return (n == 1) ? 0 : (((((n % 10) >= 2) && ((n % 10) <= 4))
671 && (((n % 100) < 10) || ((n % 100) >= 20))) ? 1 : 2);
672 //case MODE_RO:
673 // return ((n == 1) ? 0 : ((((n % 100) > 19) || (((n % 100) == 0) && (n != 0))) ? 2 : 1));
674 case MODE_LT:
675 return ((n % 10) == 1) && ((n % 100) != 11) ? 0 : (((n % 10) >= 2)
676 && (((n % 100) < 10) || ((n % 100) >= 20)) ? 1 : 2);
677 case MODE_RU:
678 return (((n % 10) == 1) && ((n % 100) != 11)) ? 0 : (((((n % 10) >= 2)
679 && ((n % 10) <= 4)) && (((n % 100) < 10) || ((n % 100) >= 20))) ? 1 : 2);
680 case MODE_SK:
681 return (n == 1) ? 1 : (((n >= 2) && (n <= 4)) ? 2 : 0);
682 //case MODE_SL:
683 // return (((n % 100) == 1) ? 1 : (((n % 100) == 2) ? 2 : ((((n % 100) == 3)
684 // || ((n % 100) == 4)) ? 3 : 0)));
685 }
686 return 0;
687 }
688
689 /**
690 * Returns the map of singular translations.
691 * @return the map of singular translations.
692 * @since 13761
693 */
694 public static Map<String, String> getSingularTranslations() {
695 return new HashMap<>(strings);
696 }
697
698 /**
699 * Returns the map of plural translations.
700 * @return the map of plural translations.
701 * @since 13761
702 */
703 public static Map<String, String[]> getPluralTranslations() {
704 return new HashMap<>(pstrings);
705 }
706
707 /**
708 * Returns the original default locale found when the JVM started.
709 * Used to guess real language/country of current user disregarding language chosen in JOSM preferences.
710 * @return the original default locale found when the JVM started
711 * @since 14013
712 */
713 public static Locale getOriginalLocale() {
714 return originalLocale;
715 }
716
717 /**
718 * Returns the localized name of the given script. Only scripts used in the OSM database are known.
719 * @param script Writing system
720 * @return the localized name of the given script, or null
721 * @since 15501
722 */
723 public static String getLocalizedScript(String script) {
724 if (script != null) {
725 switch (script.toLowerCase(Locale.ENGLISH)) {
726 case HIRAGANA:
727 return /* I18n: a Japanese syllabary */ tr("Hiragana");
728 case KATAKANA:
729 return /* I18n: a Japanese syllabary */ tr("Katakana");
730 case LATIN:
731 return /* I18n: usage of latin letters/script for usually non-latin languages */ tr("Latin");
732 case PINYIN:
733 return /* I18n: official romanization system for Standard Chinese */ tr("Pinyin");
734 case ROMAJI:
735 return /* I18n: a Japanese syllabary (latin script) */ tr("Rōmaji");
736 default:
737 Logging.warn("Unsupported script: {0}", script);
738 }
739 }
740 return null;
741 }
742
743 /**
744 * Returns the localized name of the given language and optional script.
745 * @param language Language
746 * @return the pair of localized name + known state of the given language, or null
747 * @since 15501
748 */
749 public static Pair<String, Boolean> getLocalizedLanguageName(String language) {
750 Matcher m = LANGUAGE_NAMES.matcher(language);
751 if (m.matches()) {
752 String code = m.group(1);
753 String label = new Locale(code).getDisplayLanguage();
754 boolean knownNameKey = !code.equals(label);
755 String script = getLocalizedScript(m.group(2));
756 if (script != null) {
757 label += " (" + script + ")";
758 }
759 return new Pair<>(label, knownNameKey);
760 }
761 return null;
762 }
763}
Note: See TracBrowser for help on using the repository browser.