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