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

Last change on this file since 8374 was 8374, checked in by Don-vip, 9 years ago

code style - Unnecessary Final Modifier

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