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.LinkedList;
|
---|
8 | import java.util.Locale;
|
---|
9 | import java.util.MissingResourceException;
|
---|
10 | import java.util.Vector;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
13 | import org.xnap.commons.i18n.I18nFactory;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Internationalisation support.
|
---|
17 | *
|
---|
18 | * @author Immanuel.Scholz
|
---|
19 | */
|
---|
20 | public class I18n {
|
---|
21 |
|
---|
22 | /* Base name for translation data. Used for detecting available translations */
|
---|
23 | private static final String TR_BASE = "org.openstreetmap.josm.i18n.Translation_";
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Set by MainApplication. Changes here later will probably mess up everything, because
|
---|
27 | * many strings are already loaded.
|
---|
28 | */
|
---|
29 | public static org.xnap.commons.i18n.I18n i18n;
|
---|
30 |
|
---|
31 | public static final String tr(String text, Object... objects) {
|
---|
32 | if (i18n == null)
|
---|
33 | return MessageFormat.format(filter(text), objects);
|
---|
34 | return filter(i18n.tr(text, objects));
|
---|
35 | }
|
---|
36 |
|
---|
37 | public static final String tr(String text) {
|
---|
38 | if (i18n == null)
|
---|
39 | return filter(text);
|
---|
40 | return filter(i18n.tr(text));
|
---|
41 | }
|
---|
42 |
|
---|
43 | public static final String trc(String ctx, String text) {
|
---|
44 | if (i18n == null)
|
---|
45 | return text;
|
---|
46 | return i18n.trc(ctx, text);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /* NOTE: marktr does NOT support context strings - use marktrc instead */
|
---|
50 | public static final String marktr(String text) {
|
---|
51 | return text;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public static final String marktrc(String context, String text) {
|
---|
55 | return text;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public static final String trn(String text, String pluralText, long n, Object... objects) {
|
---|
59 | if (i18n == null)
|
---|
60 | return n == 1 ? tr(text, objects) : tr(pluralText, objects);
|
---|
61 | return filter(i18n.trn(text, pluralText, n, objects));
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static final String trn(String text, String pluralText, long n) {
|
---|
65 | if (i18n == null)
|
---|
66 | return n == 1 ? tr(text) : tr(pluralText);
|
---|
67 | return filter(i18n.trn(text, pluralText, n));
|
---|
68 | }
|
---|
69 |
|
---|
70 | public static final String trnc(String ctx, String text, String pluralText, long n, Object... objects) {
|
---|
71 | if (i18n == null)
|
---|
72 | return n == 1 ? tr(text, objects) : tr(pluralText, objects);
|
---|
73 | return i18n.trnc(ctx, text, pluralText, n, objects);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static final String trnc(String ctx, String text, String pluralText, long n) {
|
---|
77 | if (i18n == null)
|
---|
78 | return n == 1 ? tr(text) : tr(pluralText);
|
---|
79 | return i18n.trnc(ctx, text, pluralText, n);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static final String filter(String text)
|
---|
83 | {
|
---|
84 | int i;
|
---|
85 | if(text.startsWith("_:") && (i = text.indexOf("\n")) >= 0)
|
---|
86 | return text.substring(i+1);
|
---|
87 | return text;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Get a list of all available JOSM Translations.
|
---|
92 | * @return an array of locale objects.
|
---|
93 | */
|
---|
94 | public static final Locale[] getAvailableTranslations() {
|
---|
95 | Vector<Locale> v = new Vector<Locale>();
|
---|
96 | LinkedList<String>str = new LinkedList<String>();
|
---|
97 | Locale[] l = Locale.getAvailableLocales();
|
---|
98 | for (int i = 0; i < l.length; i++) {
|
---|
99 | String loc = l[i].toString();
|
---|
100 | String cn = TR_BASE + loc;
|
---|
101 | try {
|
---|
102 | Class.forName(cn);
|
---|
103 | v.add(l[i]);
|
---|
104 | str.add(loc);
|
---|
105 | } catch (ClassNotFoundException e) {
|
---|
106 | }
|
---|
107 | }
|
---|
108 | /* hmm, don't know why this is necessary */
|
---|
109 | try {
|
---|
110 | if(!str.contains("nb"))
|
---|
111 | v.add(new Locale("nb"));
|
---|
112 | } catch (Exception e) {}
|
---|
113 | try {
|
---|
114 | if(!str.contains("gl"))
|
---|
115 | v.add(new Locale("gl"));
|
---|
116 | } catch (Exception e) {}
|
---|
117 | l = new Locale[v.size()];
|
---|
118 | l = v.toArray(l);
|
---|
119 | Arrays.sort(l, new Comparator<Locale>() {
|
---|
120 | public int compare(Locale o1, Locale o2) {
|
---|
121 | return o1.toString().compareTo(o2.toString());
|
---|
122 | }
|
---|
123 | });
|
---|
124 | return l;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static void init()
|
---|
128 | {
|
---|
129 | /* try initial language settings, may be changed later again */
|
---|
130 | try { i18n = I18nFactory.getI18n(MainApplication.class); }
|
---|
131 | catch (MissingResourceException ex) { Locale.setDefault(Locale.ENGLISH);}
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Sets the default locale (see {@see Locale#setDefault(Locale)} to the local
|
---|
136 | * given by <code>localName</code>.
|
---|
137 | *
|
---|
138 | * Ignored if localName is null. If the locale with name <code>localName</code>
|
---|
139 | * isn't found the default local is set to <tt>en</tt> (english).
|
---|
140 | *
|
---|
141 | * @param localeName the locale name. Ignored if null.
|
---|
142 | */
|
---|
143 | public static void set(String localeName){
|
---|
144 | if (localeName != null) {
|
---|
145 | Locale l;
|
---|
146 | Locale d = Locale.getDefault();
|
---|
147 | if (localeName.equals("he")) localeName = "iw_IL";
|
---|
148 | int i = localeName.indexOf('_');
|
---|
149 | if (i > 0) {
|
---|
150 | l = new Locale(localeName.substring(0, i), localeName.substring(i + 1));
|
---|
151 | } else {
|
---|
152 | l = new Locale(localeName);
|
---|
153 | }
|
---|
154 | try {
|
---|
155 | Locale.setDefault(l);
|
---|
156 | i18n = I18nFactory.getI18n(MainApplication.class);
|
---|
157 | } catch (MissingResourceException ex) {
|
---|
158 | if (!l.getLanguage().equals("en")) {
|
---|
159 | System.out.println(tr("Unable to find translation for the locale {0}. Reverting to {1}.",
|
---|
160 | l.getDisplayName(), d.getDisplayName()));
|
---|
161 | Locale.setDefault(d);
|
---|
162 | } else {
|
---|
163 | i18n = null;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|