1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.text.MessageFormat;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.Comparator;
|
---|
7 | import java.util.Locale;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.Vector;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Internationalisation support.
|
---|
13 | *
|
---|
14 | * @author Immanuel.Scholz
|
---|
15 | */
|
---|
16 | public class I18n {
|
---|
17 |
|
---|
18 | /* Base name for translation data. Used for detecting available translations */
|
---|
19 | private static final String TR_BASE = "org.openstreetmap.josm.i18n.Translation_";
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Set by MainApplication. Changes here later will probably mess up everything, because
|
---|
23 | * many strings are already loaded.
|
---|
24 | */
|
---|
25 | public static org.xnap.commons.i18n.I18n i18n;
|
---|
26 |
|
---|
27 | public static final String tr(String text, Object... objects) {
|
---|
28 | if (i18n == null)
|
---|
29 | return MessageFormat.format(text, objects);
|
---|
30 | return i18n.tr(text, objects);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public static final String tr(String text) {
|
---|
34 | if (i18n == null)
|
---|
35 | return text;
|
---|
36 | return i18n.tr(text);
|
---|
37 | }
|
---|
38 |
|
---|
39 | public static final String marktr(String text) {
|
---|
40 | return text;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public static final String trn(String text, String pluralText, long n, Object... objects) {
|
---|
44 | if (i18n == null)
|
---|
45 | return n == 1 ? tr(text, objects) : tr(pluralText, objects);
|
---|
46 | return i18n.trn(text, pluralText, n, objects);
|
---|
47 | }
|
---|
48 |
|
---|
49 | public static final String trn(String text, String pluralText, long n) {
|
---|
50 | if (i18n == null)
|
---|
51 | return n == 1 ? tr(text) : tr(pluralText);
|
---|
52 | return i18n.trn(text, pluralText, n);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Get a list of all available JOSM Translations.
|
---|
57 | * @return an array of locale objects.
|
---|
58 | */
|
---|
59 | public static final Locale[] getAvailableTranslations() {
|
---|
60 | Vector<Locale> v = new Vector<Locale>();
|
---|
61 | LinkedList<String>str = new LinkedList<String>();
|
---|
62 | Locale[] l = Locale.getAvailableLocales();
|
---|
63 | for (int i = 0; i < l.length; i++) {
|
---|
64 | String loc = l[i].toString();
|
---|
65 | String cn = TR_BASE + loc;
|
---|
66 | try {
|
---|
67 | Class.forName(cn);
|
---|
68 | v.add(l[i]);
|
---|
69 | str.add(loc);
|
---|
70 | } catch (ClassNotFoundException e) {
|
---|
71 | }
|
---|
72 | }
|
---|
73 | /* hmm, don't know why this is necessary */
|
---|
74 | try {
|
---|
75 | if(!str.contains("nb"))
|
---|
76 | v.add(new Locale("nb"));
|
---|
77 | } catch (Exception e) {}
|
---|
78 | try {
|
---|
79 | if(!str.contains("gl"))
|
---|
80 | v.add(new Locale("gl"));
|
---|
81 | } catch (Exception e) {}
|
---|
82 | l = new Locale[v.size()];
|
---|
83 | l = v.toArray(l);
|
---|
84 | Arrays.sort(l, new Comparator<Locale>() {
|
---|
85 | public int compare(Locale o1, Locale o2) {
|
---|
86 | return o1.toString().compareTo(o2.toString());
|
---|
87 | }
|
---|
88 | });
|
---|
89 | return l;
|
---|
90 | }
|
---|
91 | }
|
---|