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

Last change on this file since 7897 was 7897, checked in by stoecker, 9 years ago

see #8645 - also enable new language on older systems

  • Property svn:eol-style set to native
File size: 29.0 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.FileInputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.net.URL;
10import java.nio.charset.StandardCharsets;
11import java.text.MessageFormat;
12import java.util.ArrayList;
13import java.util.Arrays;
14import java.util.Collection;
15import java.util.Comparator;
16import java.util.HashMap;
17import java.util.Locale;
18import java.util.Map;
19import java.util.jar.JarInputStream;
20import java.util.zip.ZipEntry;
21
22import javax.swing.JColorChooser;
23import javax.swing.JFileChooser;
24import javax.swing.UIManager;
25
26import org.openstreetmap.gui.jmapviewer.FeatureAdapter.TranslationAdapter;
27import org.openstreetmap.josm.Main;
28import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
29
30/**
31 * Internationalisation support.
32 *
33 * @author Immanuel.Scholz
34 */
35public final class I18n {
36
37 private I18n() {
38 // Hide default constructor for utils classes
39 }
40
41 /**
42 * Enumeration of possible plural modes.
43 * See <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html">CLDR</a>
44 * for a complete list.
45 * @see #pluralEval
46 */
47 private enum PluralMode {
48 /** Plural = Not 1. This is the default for many languages, including English: 1 day, but 0 days or 2 days. */
49 MODE_NOTONE,
50 /** No plural. Mainly for Asian languages (Indonesian, Chinese, Japanese, ...) */
51 MODE_NONE,
52 /** Plural = Greater than 1. For some latin languages (French, Brazilian Portuguese) */
53 MODE_GREATERONE,
54 /* Special mode for
55 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ar">Arabic</a>.*
56 MODE_AR,*/
57 /** Special mode for
58 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#cs">Czech</a>. */
59 MODE_CS,
60 /** Special mode for
61 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#pl">Polish</a>. */
62 MODE_PL,
63 /* Special mode for
64 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ro">Romanian</a>.*
65 MODE_RO,*/
66 /** Special mode for
67 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ru">Russian</a>. */
68 MODE_RU,
69 /** Special mode for
70 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#sk">Slovak</a>. */
71 MODE_SK,
72 /* Special mode for
73 * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#sl">Slovenian</a>.*
74 MODE_SL,*/
75 }
76
77 private static PluralMode pluralMode = PluralMode.MODE_NOTONE; /* english default */
78 private static String loadedCode = "en";
79
80 /* Localization keys for file chooser (and color chooser). */
81 private static final String[] javaInternalMessageKeys = new String[] {
82 /* JFileChooser windows laf */
83 "FileChooser.detailsViewActionLabelText",
84 "FileChooser.detailsViewButtonAccessibleName",
85 "FileChooser.detailsViewButtonToolTipText",
86 "FileChooser.fileAttrHeaderText",
87 "FileChooser.fileDateHeaderText",
88 "FileChooser.fileNameHeaderText",
89 "FileChooser.fileNameLabelText",
90 "FileChooser.fileSizeHeaderText",
91 "FileChooser.fileTypeHeaderText",
92 "FileChooser.filesOfTypeLabelText",
93 "FileChooser.homeFolderAccessibleName",
94 "FileChooser.homeFolderToolTipText",
95 "FileChooser.listViewActionLabelText",
96 "FileChooser.listViewButtonAccessibleName",
97 "FileChooser.listViewButtonToolTipText",
98 "FileChooser.lookInLabelText",
99 "FileChooser.newFolderAccessibleName",
100 "FileChooser.newFolderActionLabelText",
101 "FileChooser.newFolderToolTipText",
102 "FileChooser.refreshActionLabelText",
103 "FileChooser.saveInLabelText",
104 "FileChooser.upFolderAccessibleName",
105 "FileChooser.upFolderToolTipText",
106 "FileChooser.viewMenuLabelText",
107
108 /* JFileChooser gtk laf */
109 "FileChooser.acceptAllFileFilterText",
110 "FileChooser.cancelButtonText",
111 "FileChooser.cancelButtonToolTipText",
112 "FileChooser.deleteFileButtonText",
113 "FileChooser.filesLabelText",
114 "FileChooser.filterLabelText",
115 "FileChooser.foldersLabelText",
116 "FileChooser.newFolderButtonText",
117 "FileChooser.newFolderDialogText",
118 "FileChooser.openButtonText",
119 "FileChooser.openButtonToolTipText",
120 "FileChooser.openDialogTitleText",
121 "FileChooser.pathLabelText",
122 "FileChooser.renameFileButtonText",
123 "FileChooser.renameFileDialogText",
124 "FileChooser.renameFileErrorText",
125 "FileChooser.renameFileErrorTitle",
126 "FileChooser.saveButtonText",
127 "FileChooser.saveButtonToolTipText",
128 "FileChooser.saveDialogTitleText",
129
130 /* JFileChooser motif laf */
131 //"FileChooser.cancelButtonText",
132 //"FileChooser.cancelButtonToolTipText",
133 "FileChooser.enterFileNameLabelText",
134 //"FileChooser.filesLabelText",
135 //"FileChooser.filterLabelText",
136 //"FileChooser.foldersLabelText",
137 "FileChooser.helpButtonText",
138 "FileChooser.helpButtonToolTipText",
139 //"FileChooser.openButtonText",
140 //"FileChooser.openButtonToolTipText",
141 //"FileChooser.openDialogTitleText",
142 //"FileChooser.pathLabelText",
143 //"FileChooser.saveButtonText",
144 //"FileChooser.saveButtonToolTipText",
145 //"FileChooser.saveDialogTitleText",
146 "FileChooser.updateButtonText",
147 "FileChooser.updateButtonToolTipText",
148
149 /* gtk color chooser */
150 "GTKColorChooserPanel.blueText",
151 "GTKColorChooserPanel.colorNameText",
152 "GTKColorChooserPanel.greenText",
153 "GTKColorChooserPanel.hueText",
154 "GTKColorChooserPanel.nameText",
155 "GTKColorChooserPanel.redText",
156 "GTKColorChooserPanel.saturationText",
157 "GTKColorChooserPanel.valueText",
158
159 /* JOptionPane */
160 "OptionPane.okButtonText",
161 "OptionPane.yesButtonText",
162 "OptionPane.noButtonText",
163 "OptionPane.cancelButtonText"
164 };
165 private static Map<String, String> strings = null;
166 private static Map<String, String[]> pstrings = null;
167 private static Map<String, PluralMode> languages = new HashMap<>();
168
169 /**
170 * Translates some text for the current locale.
171 * These strings are collected by a script that runs on the source code files.
172 * After translation, the localizations are distributed with the main program.
173 * <br>
174 * For example, <code>tr("JOSM''s default value is ''{0}''.", val)</code>.
175 * <br>
176 * Use {@link #trn} for distinguishing singular from plural text, i.e.,
177 * do not use {@code tr(size == 1 ? "singular" : "plural")} nor
178 * {@code size == 1 ? tr("singular") : tr("plural")}
179 *
180 * @param text the text to translate.
181 * Must be a string literal. (No constants or local vars.)
182 * Can be broken over multiple lines.
183 * An apostrophe ' must be quoted by another apostrophe.
184 * @param objects the parameters for the string.
185 * Mark occurrences in {@code text} with <code>{0}</code>, <code>{1}</code>, ...
186 * @return the translated string.
187 * @see #trn
188 * @see #trc
189 * @see #trnc
190 */
191 public static final String tr(String text, Object... objects) {
192 if (text == null) return null;
193 return MessageFormat.format(gettext(text, null), objects);
194 }
195
196 /**
197 * Translates some text in a context for the current locale.
198 * There can be different translations for the same text within different contexts.
199 *
200 * @param context string that helps translators to find an appropriate
201 * translation for {@code text}.
202 * @param text the text to translate.
203 * @return the translated string.
204 * @see #tr
205 * @see #trn
206 * @see #trnc
207 */
208 public static final String trc(String context, String text) {
209 if (context == null)
210 return tr(text);
211 if (text == null)
212 return null;
213 return MessageFormat.format(gettext(text, context), (Object)null);
214 }
215
216 public static final String trc_lazy(String context, String text) {
217 if (context == null)
218 return tr(text);
219 if (text == null)
220 return null;
221 return MessageFormat.format(gettext_lazy(text, context), (Object)null);
222 }
223
224 /**
225 * Marks a string for translation (such that a script can harvest
226 * the translatable strings from the source files).
227 *
228 * For example, <code>
229 * String[] options = new String[] {marktr("up"), marktr("down")};
230 * lbl.setText(tr(options[0]));</code>
231 * @param text the string to be marked for translation.
232 * @return {@code text} unmodified.
233 */
234 public static final String marktr(String text) {
235 return text;
236 }
237
238 public static final String marktrc(String context, String text) {
239 return text;
240 }
241
242 /**
243 * Translates some text for the current locale and distinguishes between
244 * {@code singularText} and {@code pluralText} depending on {@code n}.
245 * <br>
246 * For instance, {@code trn("There was an error!", "There were errors!", i)} or
247 * <code>trn("Found {0} error in {1}!", "Found {0} errors in {1}!", i, Integer.toString(i), url)</code>.
248 *
249 * @param singularText the singular text to translate.
250 * Must be a string literal. (No constants or local vars.)
251 * Can be broken over multiple lines.
252 * An apostrophe ' must be quoted by another apostrophe.
253 * @param pluralText the plural 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 n a number to determine whether {@code singularText} or {@code pluralText} is used.
258 * @param objects the parameters for the string.
259 * Mark occurrences in {@code singularText} and {@code pluralText} with <code>{0}</code>, <code>{1}</code>, ...
260 * @return the translated string.
261 * @see #tr
262 * @see #trc
263 * @see #trnc
264 */
265 public static final String trn(String singularText, String pluralText, long n, Object... objects) {
266 return MessageFormat.format(gettextn(singularText, pluralText, null, n), objects);
267 }
268
269 /**
270 * Translates some text in a context for the current locale and distinguishes between
271 * {@code singularText} and {@code pluralText} depending on {@code n}.
272 * There can be different translations for the same text within different contexts.
273 *
274 * @param context string that helps translators to find an appropriate
275 * translation for {@code text}.
276 * @param singularText the singular text to translate.
277 * Must be a string literal. (No constants or local vars.)
278 * Can be broken over multiple lines.
279 * An apostrophe ' must be quoted by another apostrophe.
280 * @param pluralText the plural 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 n a number to determine whether {@code singularText} or {@code pluralText} is used.
285 * @param objects the parameters for the string.
286 * Mark occurrences in {@code singularText} and {@code pluralText} with <code>{0}</code>, <code>{1}</code>, ...
287 * @return the translated string.
288 * @see #tr
289 * @see #trc
290 * @see #trn
291 */
292 public static final String trnc(String context, String singularText, String pluralText, long n, Object... objects) {
293 return MessageFormat.format(gettextn(singularText, pluralText, context, n), objects);
294 }
295
296 private static final String gettext(String text, String ctx, boolean lazy) {
297 int i;
298 if(ctx == null && text.startsWith("_:") && (i = text.indexOf('\n')) >= 0) {
299 ctx = text.substring(2,i-1);
300 text = text.substring(i+1);
301 }
302 if(strings != null) {
303 String trans = strings.get(ctx == null ? text : "_:"+ctx+"\n"+text);
304 if(trans != null)
305 return trans;
306 }
307 if(pstrings != null) {
308 i = pluralEval(1);
309 String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+"\n"+text);
310 if(trans != null && trans.length > i)
311 return trans[i];
312 }
313 return lazy ? gettext(text, null) : text;
314 }
315
316 private static final String gettext(String text, String ctx) {
317 return gettext(text, ctx, false);
318 }
319
320 /* try without context, when context try fails */
321 private static final String gettext_lazy(String text, String ctx) {
322 return gettext(text, ctx, true);
323 }
324
325 private static final String gettextn(String text, String plural, String ctx, long num) {
326 int i;
327 if(ctx == null && text.startsWith("_:") && (i = text.indexOf('\n')) >= 0) {
328 ctx = text.substring(2,i-1);
329 text = text.substring(i+1);
330 }
331 if(pstrings != null) {
332 i = pluralEval(num);
333 String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+"\n"+text);
334 if(trans != null && trans.length > i)
335 return trans[i];
336 }
337
338 return num == 1 ? text : plural;
339 }
340
341 public static String escape(String msg) {
342 if (msg == null) return null;
343 return msg.replace("\'", "\'\'").replace("{", "\'{\'").replace("}", "\'}\'");
344 }
345
346 private static URL getTranslationFile(String lang) {
347 return Main.class.getResource("/data/"+lang+".lang");
348 }
349
350 /**
351 * Get a list of all available JOSM Translations.
352 * @return an array of locale objects.
353 */
354 public static final Locale[] getAvailableTranslations() {
355 Collection<Locale> v = new ArrayList<>(languages.size());
356 if(getTranslationFile("en") != null) {
357 for (String loc : languages.keySet()) {
358 if(getTranslationFile(loc) != null) {
359 v.add(LanguageInfo.getLocale(loc));
360 }
361 }
362 }
363 v.add(Locale.ENGLISH);
364 Locale[] l = new Locale[v.size()];
365 l = v.toArray(l);
366 Arrays.sort(l, new Comparator<Locale>() {
367 @Override
368 public int compare(Locale o1, Locale o2) {
369 return o1.toString().compareTo(o2.toString());
370 }
371 });
372 return l;
373 }
374
375 /**
376 * Determines if a language exists for the given code.
377 * @param code The language code
378 * @return {@code true} if a language exists, {@code false} otherwise
379 */
380 public static boolean hasCode(String code) {
381 return languages.containsKey(code);
382 }
383
384 /**
385 * I18n initialization.
386 */
387 public static void init() {
388 // Enable CLDR locale provider on Java 8 to get additional languages, such as Khmer.
389 // http://docs.oracle.com/javase/8/docs/technotes/guides/intl/enhancements.8.html#cldr
390 // FIXME: This can be removed after we switch to a minimal version of Java that enables CLDR by default
391 // or includes all languages we need in the JRE. See http://openjdk.java.net/jeps/8043554 for Java 9
392 Utils.updateSystemProperty("java.locale.providers", "JRE,CLDR");
393
394 //languages.put("ar", PluralMode.MODE_AR);
395 languages.put("bg", PluralMode.MODE_NOTONE);
396 languages.put("ca", PluralMode.MODE_NOTONE);
397 languages.put("cs", PluralMode.MODE_CS);
398 languages.put("da", PluralMode.MODE_NOTONE);
399 languages.put("de", PluralMode.MODE_NOTONE);
400 languages.put("el", PluralMode.MODE_NOTONE);
401 languages.put("en_AU", PluralMode.MODE_NOTONE);
402 languages.put("en_GB", PluralMode.MODE_NOTONE);
403 languages.put("es", PluralMode.MODE_NOTONE);
404 languages.put("et", PluralMode.MODE_NOTONE);
405 //languages.put("eu", PluralMode.MODE_NOTONE);
406 languages.put("fi", PluralMode.MODE_NOTONE);
407 languages.put("fr", PluralMode.MODE_GREATERONE);
408 languages.put("gl", PluralMode.MODE_NOTONE);
409 //languages.put("he", PluralMode.MODE_NOTONE);
410 languages.put("hu", PluralMode.MODE_NOTONE);
411 languages.put("id", PluralMode.MODE_NONE);
412 //languages.put("is", PluralMode.MODE_NOTONE);
413 languages.put("it", PluralMode.MODE_NOTONE);
414 languages.put("ja", PluralMode.MODE_NONE);
415 //if (Main.isJava8orLater()) {
416 // fully supported only with Java 8 and later code
417 languages.put("km", PluralMode.MODE_NONE);
418 //}
419 //languages.put("nb", PluralMode.MODE_NOTONE);
420 languages.put("nl", PluralMode.MODE_NOTONE);
421 languages.put("pl", PluralMode.MODE_PL);
422 languages.put("pt", PluralMode.MODE_NOTONE);
423 languages.put("pt_BR", PluralMode.MODE_GREATERONE);
424 //languages.put("ro", PluralMode.MODE_RO);
425 languages.put("ru", PluralMode.MODE_RU);
426 languages.put("sk", PluralMode.MODE_SK);
427 //languages.put("sl", PluralMode.MODE_SL);
428 languages.put("sv", PluralMode.MODE_NOTONE);
429 //languages.put("tr", PluralMode.MODE_NONE);
430 languages.put("uk", PluralMode.MODE_RU);
431 languages.put("zh_CN", PluralMode.MODE_NONE);
432 languages.put("zh_TW", PluralMode.MODE_NONE);
433
434 /* try initial language settings, may be changed later again */
435 if(!load(Locale.getDefault().toString())) {
436 Locale.setDefault(Locale.ENGLISH);
437 }
438 }
439
440 public static void addTexts(File source) {
441 if ("en".equals(loadedCode))
442 return;
443 String enfile = "data/en.lang";
444 String langfile = "data/"+loadedCode+".lang";
445 try (
446 FileInputStream fis = new FileInputStream(source);
447 JarInputStream jar = new JarInputStream(fis)
448 ) {
449 ZipEntry e;
450 boolean found = false;
451 while (!found && (e = jar.getNextEntry()) != null) {
452 String name = e.getName();
453 if(name.equals(enfile))
454 found = true;
455 }
456 if (found) {
457 try (
458 FileInputStream fisTrans = new FileInputStream(source);
459 JarInputStream jarTrans = new JarInputStream(fisTrans)
460 ) {
461 found = false;
462 while(!found && (e = jarTrans.getNextEntry()) != null) {
463 String name = e.getName();
464 if (name.equals(langfile))
465 found = true;
466 }
467 if (found)
468 load(jar, jarTrans, true);
469 }
470 }
471 } catch (IOException e) {
472 // Ignore
473 }
474 }
475
476 private static boolean load(String l) {
477 if ("en".equals(l) || "en_US".equals(l)) {
478 strings = null;
479 pstrings = null;
480 loadedCode = "en";
481 pluralMode = PluralMode.MODE_NOTONE;
482 return true;
483 }
484 URL en = getTranslationFile("en");
485 if (en == null)
486 return false;
487 URL tr = getTranslationFile(l);
488 if (tr == null || !languages.containsKey(l)) {
489 int i = l.indexOf('_');
490 if (i > 0) {
491 l = l.substring(0, i);
492 }
493 tr = getTranslationFile(l);
494 if (tr == null || !languages.containsKey(l))
495 return false;
496 }
497 try (
498 InputStream enStream = en.openStream();
499 InputStream trStream = tr.openStream()
500 ) {
501 if (load(enStream, trStream, false)) {
502 pluralMode = languages.get(l);
503 loadedCode = l;
504 return true;
505 }
506 } catch (IOException e) {
507 // Ignore exception
508 }
509 return false;
510 }
511
512 private static boolean load(InputStream en, InputStream tr, boolean add) {
513 Map<String, String> s;
514 Map<String, String[]> p;
515 if (add) {
516 s = strings;
517 p = pstrings;
518 } else {
519 s = new HashMap<>();
520 p = new HashMap<>();
521 }
522 /* file format:
523 Files are always a group. English file and translated file must provide identical datasets.
524
525 for all single strings:
526 {
527 unsigned short (2 byte) stringlength
528 - length 0 indicates missing translation
529 - length 0xFFFE indicates translation equal to original, but otherwise is equal to length 0
530 string
531 }
532 unsigned short (2 byte) 0xFFFF (marks end of single strings)
533 for all multi strings:
534 {
535 unsigned char (1 byte) stringcount
536 - count 0 indicates missing translations
537 - count 0xFE indicates translations equal to original, but otherwise is equal to length 0
538 for stringcount
539 unsigned short (2 byte) stringlength
540 string
541 }
542 */
543 try {
544 InputStream ens = new BufferedInputStream(en);
545 InputStream trs = new BufferedInputStream(tr);
546 byte[] enlen = new byte[2];
547 byte[] trlen = new byte[2];
548 boolean multimode = false;
549 byte[] str = new byte[4096];
550 for(;;) {
551 if(multimode) {
552 int ennum = ens.read();
553 int trnum = trs.read();
554 if(trnum == 0xFE) /* marks identical string, handle equally to non-translated */
555 trnum = 0;
556 if((ennum == -1 && trnum != -1) || (ennum != -1 && trnum == -1)) /* files do not match */
557 return false;
558 if(ennum == -1) {
559 break;
560 }
561 String[] enstrings = new String[ennum];
562 String[] trstrings = new String[trnum];
563 for(int i = 0; i < ennum; ++i) {
564 int val = ens.read(enlen);
565 if(val != 2) /* file corrupt */
566 return false;
567 val = (enlen[0] < 0 ? 256+enlen[0]:enlen[0])*256+(enlen[1] < 0 ? 256+enlen[1]:enlen[1]);
568 if(val > str.length) {
569 str = new byte[val];
570 }
571 int rval = ens.read(str, 0, val);
572 if(rval != val) /* file corrupt */
573 return false;
574 enstrings[i] = new String(str, 0, val, StandardCharsets.UTF_8);
575 }
576 for(int i = 0; i < trnum; ++i) {
577 int val = trs.read(trlen);
578 if(val != 2) /* file corrupt */
579 return false;
580 val = (trlen[0] < 0 ? 256+trlen[0]:trlen[0])*256+(trlen[1] < 0 ? 256+trlen[1]:trlen[1]);
581 if(val > str.length) {
582 str = new byte[val];
583 }
584 int rval = trs.read(str, 0, val);
585 if(rval != val) /* file corrupt */
586 return false;
587 trstrings[i] = new String(str, 0, val, StandardCharsets.UTF_8);
588 }
589 if(trnum > 0 && !p.containsKey(enstrings[0])) {
590 p.put(enstrings[0], trstrings);
591 }
592 } else {
593 int enval = ens.read(enlen);
594 int trval = trs.read(trlen);
595 if(enval != trval) /* files do not match */
596 return false;
597 if(enval == -1) {
598 break;
599 }
600 if(enval != 2) /* files corrupt */
601 return false;
602 enval = (enlen[0] < 0 ? 256+enlen[0]:enlen[0])*256+(enlen[1] < 0 ? 256+enlen[1]:enlen[1]);
603 trval = (trlen[0] < 0 ? 256+trlen[0]:trlen[0])*256+(trlen[1] < 0 ? 256+trlen[1]:trlen[1]);
604 if(trval == 0xFFFE) /* marks identical string, handle equally to non-translated */
605 trval = 0;
606 if(enval == 0xFFFF) {
607 multimode = true;
608 if(trval != 0xFFFF) /* files do not match */
609 return false;
610 } else {
611 if (enval > str.length) {
612 str = new byte[enval];
613 }
614 if (trval > str.length) {
615 str = new byte[trval];
616 }
617 int val = ens.read(str, 0, enval);
618 if(val != enval) /* file corrupt */
619 return false;
620 String enstr = new String(str, 0, enval, StandardCharsets.UTF_8);
621 if (trval != 0) {
622 val = trs.read(str, 0, trval);
623 if(val != trval) /* file corrupt */
624 return false;
625 String trstr = new String(str, 0, trval, StandardCharsets.UTF_8);
626 if(!s.containsKey(enstr))
627 s.put(enstr, trstr);
628 }
629 }
630 }
631 }
632 } catch (IOException e) {
633 return false;
634 }
635 if (!s.isEmpty()) {
636 strings = s;
637 pstrings = p;
638 return true;
639 }
640 return false;
641 }
642
643 /**
644 * Sets the default locale (see {@link Locale#setDefault(Locale)} to the local
645 * given by <code>localName</code>.
646 *
647 * Ignored if localeName is null. If the locale with name <code>localName</code>
648 * isn't found the default local is set to <tt>en</tt> (english).
649 *
650 * @param localeName the locale name. Ignored if null.
651 */
652 public static void set(String localeName){
653 if (localeName != null) {
654 Locale l = LanguageInfo.getLocale(localeName);
655 if (load(LanguageInfo.getJOSMLocaleCode(l))) {
656 Locale.setDefault(l);
657 } else {
658 if (!"en".equals(l.getLanguage())) {
659 Main.info(tr("Unable to find translation for the locale {0}. Reverting to {1}.",
660 l.getDisplayName(), Locale.getDefault().getDisplayName()));
661 } else {
662 strings = null;
663 pstrings = null;
664 }
665 }
666 }
667 }
668
669 /**
670 * Localizations for file chooser dialog.
671 * For some locales (e.g. de, fr) translations are provided
672 * by Java, but not for others (e.g. ru, uk).
673 */
674 public static void translateJavaInternalMessages() {
675 Locale l = Locale.getDefault();
676
677 AbstractFileChooser.setDefaultLocale(l);
678 JFileChooser.setDefaultLocale(l);
679 JColorChooser.setDefaultLocale(l);
680 for (String key : javaInternalMessageKeys) {
681 String us = UIManager.getString(key, Locale.US);
682 String loc = UIManager.getString(key, l);
683 // only provide custom translation if it is not already localized by Java
684 if (us != null && us.equals(loc)) {
685 UIManager.put(key, tr(us));
686 }
687 }
688 }
689
690 private static int pluralEval(long n) {
691 switch(pluralMode) {
692 case MODE_NOTONE: /* bg, da, de, el, en, en_GB, es, et, eu, fi, gl, is, it, iw_IL, nb, nl, sv */
693 return ((n != 1) ? 1 : 0);
694 case MODE_NONE: /* id, ja, km, tr, zh_CN, zh_TW */
695 return 0;
696 case MODE_GREATERONE: /* fr, pt_BR */
697 return ((n > 1) ? 1 : 0);
698 case MODE_CS:
699 return ((n == 1) ? 0 : (((n >= 2) && (n <= 4)) ? 1 : 2));
700 //case MODE_AR:
701 // return ((n == 0) ? 0 : ((n == 1) ? 1 : ((n == 2) ? 2 : ((((n % 100) >= 3)
702 // && ((n % 100) <= 10)) ? 3 : ((((n % 100) >= 11) && ((n % 100) <= 99)) ? 4 : 5)))));
703 case MODE_PL:
704 return ((n == 1) ? 0 : (((((n % 10) >= 2) && ((n % 10) <= 4))
705 && (((n % 100) < 10) || ((n % 100) >= 20))) ? 1 : 2));
706 //case MODE_RO:
707 // return ((n == 1) ? 0 : ((((n % 100) > 19) || (((n % 100) == 0) && (n != 0))) ? 2 : 1));
708 case MODE_RU:
709 return ((((n % 10) == 1) && ((n % 100) != 11)) ? 0 : (((((n % 10) >= 2)
710 && ((n % 10) <= 4)) && (((n % 100) < 10) || ((n % 100) >= 20))) ? 1 : 2));
711 case MODE_SK:
712 return ((n == 1) ? 1 : (((n >= 2) && (n <= 4)) ? 2 : 0));
713 //case MODE_SL:
714 // return (((n % 100) == 1) ? 1 : (((n % 100) == 2) ? 2 : ((((n % 100) == 3)
715 // || ((n % 100) == 4)) ? 3 : 0)));
716 }
717 return 0;
718 }
719
720 public static TranslationAdapter getTranslationAdapter() {
721 return new TranslationAdapter() {
722 @Override
723 public String tr(String text, Object... objects) {
724 return I18n.tr(text, objects);
725 }
726 };
727 }
728}
Note: See TracBrowser for help on using the repository browser.