1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.io.BufferedInputStream;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileInputStream;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.lang.annotation.Retention;
|
---|
10 | import java.lang.annotation.RetentionPolicy;
|
---|
11 | import java.net.URL;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.text.MessageFormat;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.Arrays;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.Comparator;
|
---|
18 | import java.util.HashMap;
|
---|
19 | import java.util.Locale;
|
---|
20 | import java.util.Map;
|
---|
21 | import java.util.jar.JarInputStream;
|
---|
22 | import java.util.zip.ZipEntry;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Internationalisation support.
|
---|
26 | *
|
---|
27 | * @author Immanuel.Scholz
|
---|
28 | */
|
---|
29 | public final class I18n {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * This annotates strings which do not permit a clean i18n. This is mostly due to strings
|
---|
33 | * containing two nouns which can occur in singular or plural form.
|
---|
34 | * <br>
|
---|
35 | * No behaviour is associated with this annotation.
|
---|
36 | */
|
---|
37 | @Retention(RetentionPolicy.SOURCE)
|
---|
38 | public @interface QuirkyPluralString {
|
---|
39 | }
|
---|
40 |
|
---|
41 | private I18n() {
|
---|
42 | // Hide default constructor for utils classes
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Enumeration of possible plural modes. It allows us to identify and implement logical conditions of
|
---|
47 | * plural forms defined on <a href="https://help.launchpad.net/Translations/PluralForms">Launchpad</a>.
|
---|
48 | * See <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html">CLDR</a>
|
---|
49 | * for another complete list.
|
---|
50 | * @see #pluralEval
|
---|
51 | */
|
---|
52 | private enum PluralMode {
|
---|
53 | /** Plural = Not 1. This is the default for many languages, including English: 1 day, but 0 days or 2 days. */
|
---|
54 | MODE_NOTONE,
|
---|
55 | /** No plural. Mainly for Asian languages (Indonesian, Chinese, Japanese, ...) */
|
---|
56 | MODE_NONE,
|
---|
57 | /** Plural = Greater than 1. For some latin languages (French, Brazilian Portuguese) */
|
---|
58 | MODE_GREATERONE,
|
---|
59 | /* Special mode for
|
---|
60 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ar">Arabic</a>.*
|
---|
61 | MODE_AR,*/
|
---|
62 | /** Special mode for
|
---|
63 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#cs">Czech</a>. */
|
---|
64 | MODE_CS,
|
---|
65 | /** Special mode for
|
---|
66 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#pl">Polish</a>. */
|
---|
67 | MODE_PL,
|
---|
68 | /* Special mode for
|
---|
69 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ro">Romanian</a>.*
|
---|
70 | MODE_RO,*/
|
---|
71 | /** Special mode for
|
---|
72 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#lt">Lithuanian</a>. */
|
---|
73 | MODE_LT,
|
---|
74 | /** Special mode for
|
---|
75 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ru">Russian</a>. */
|
---|
76 | MODE_RU,
|
---|
77 | /** Special mode for
|
---|
78 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#sk">Slovak</a>. */
|
---|
79 | MODE_SK,
|
---|
80 | /* Special mode for
|
---|
81 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#sl">Slovenian</a>.*
|
---|
82 | MODE_SL,*/
|
---|
83 | }
|
---|
84 |
|
---|
85 | private static volatile PluralMode pluralMode = PluralMode.MODE_NOTONE; /* english default */
|
---|
86 | private static volatile String loadedCode = "en";
|
---|
87 |
|
---|
88 |
|
---|
89 | private static volatile Map<String, String> strings;
|
---|
90 | private static volatile Map<String, String[]> pstrings;
|
---|
91 | private static Map<String, PluralMode> languages = new HashMap<>();
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Translates some text for the current locale.
|
---|
95 | * These strings are collected by a script that runs on the source code files.
|
---|
96 | * After translation, the localizations are distributed with the main program.
|
---|
97 | * <br>
|
---|
98 | * For example, <code>tr("JOSM''s default value is ''{0}''.", val)</code>.
|
---|
99 | * <br>
|
---|
100 | * Use {@link #trn} for distinguishing singular from plural text, i.e.,
|
---|
101 | * do not use {@code tr(size == 1 ? "singular" : "plural")} nor
|
---|
102 | * {@code size == 1 ? tr("singular") : tr("plural")}
|
---|
103 | *
|
---|
104 | * @param text the text to translate.
|
---|
105 | * Must be a string literal. (No constants or local vars.)
|
---|
106 | * Can be broken over multiple lines.
|
---|
107 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
108 | * @param objects the parameters for the string.
|
---|
109 | * Mark occurrences in {@code text} with <code>{0}</code>, <code>{1}</code>, ...
|
---|
110 | * @return the translated string.
|
---|
111 | * @see #trn
|
---|
112 | * @see #trc
|
---|
113 | * @see #trnc
|
---|
114 | */
|
---|
115 | public static String tr(String text, Object... objects) {
|
---|
116 | if (text == null) return null;
|
---|
117 | return MessageFormat.format(gettext(text, null), objects);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Translates some text in a context for the current locale.
|
---|
122 | * There can be different translations for the same text within different contexts.
|
---|
123 | *
|
---|
124 | * @param context string that helps translators to find an appropriate
|
---|
125 | * translation for {@code text}.
|
---|
126 | * @param text the text to translate.
|
---|
127 | * @return the translated string.
|
---|
128 | * @see #tr
|
---|
129 | * @see #trn
|
---|
130 | * @see #trnc
|
---|
131 | */
|
---|
132 | public static String trc(String context, String text) {
|
---|
133 | if (context == null)
|
---|
134 | return tr(text);
|
---|
135 | if (text == null)
|
---|
136 | return null;
|
---|
137 | return MessageFormat.format(gettext(text, context), (Object) null);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public static String trcLazy(String context, String text) {
|
---|
141 | if (context == null)
|
---|
142 | return tr(text);
|
---|
143 | if (text == null)
|
---|
144 | return null;
|
---|
145 | return MessageFormat.format(gettextLazy(text, context), (Object) null);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Marks a string for translation (such that a script can harvest
|
---|
150 | * the translatable strings from the source files).
|
---|
151 | *
|
---|
152 | * For example, <code>
|
---|
153 | * String[] options = new String[] {marktr("up"), marktr("down")};
|
---|
154 | * lbl.setText(tr(options[0]));</code>
|
---|
155 | * @param text the string to be marked for translation.
|
---|
156 | * @return {@code text} unmodified.
|
---|
157 | */
|
---|
158 | public static String marktr(String text) {
|
---|
159 | return text;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public static String marktrc(String context, String text) {
|
---|
163 | return text;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Translates some text for the current locale and distinguishes between
|
---|
168 | * {@code singularText} and {@code pluralText} depending on {@code n}.
|
---|
169 | * <br>
|
---|
170 | * For instance, {@code trn("There was an error!", "There were errors!", i)} or
|
---|
171 | * <code>trn("Found {0} error in {1}!", "Found {0} errors in {1}!", i, Integer.toString(i), url)</code>.
|
---|
172 | *
|
---|
173 | * @param singularText the singular text to translate.
|
---|
174 | * Must be a string literal. (No constants or local vars.)
|
---|
175 | * Can be broken over multiple lines.
|
---|
176 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
177 | * @param pluralText the plural text to translate.
|
---|
178 | * Must be a string literal. (No constants or local vars.)
|
---|
179 | * Can be broken over multiple lines.
|
---|
180 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
181 | * @param n a number to determine whether {@code singularText} or {@code pluralText} is used.
|
---|
182 | * @param objects the parameters for the string.
|
---|
183 | * Mark occurrences in {@code singularText} and {@code pluralText} with <code>{0}</code>, <code>{1}</code>, ...
|
---|
184 | * @return the translated string.
|
---|
185 | * @see #tr
|
---|
186 | * @see #trc
|
---|
187 | * @see #trnc
|
---|
188 | */
|
---|
189 | public static String trn(String singularText, String pluralText, long n, Object... objects) {
|
---|
190 | return MessageFormat.format(gettextn(singularText, pluralText, null, n), objects);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Translates some text in a context for the current locale and distinguishes between
|
---|
195 | * {@code singularText} and {@code pluralText} depending on {@code n}.
|
---|
196 | * There can be different translations for the same text within different contexts.
|
---|
197 | *
|
---|
198 | * @param context string that helps translators to find an appropriate
|
---|
199 | * translation for {@code text}.
|
---|
200 | * @param singularText the singular text to translate.
|
---|
201 | * Must be a string literal. (No constants or local vars.)
|
---|
202 | * Can be broken over multiple lines.
|
---|
203 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
204 | * @param pluralText the plural text to translate.
|
---|
205 | * Must be a string literal. (No constants or local vars.)
|
---|
206 | * Can be broken over multiple lines.
|
---|
207 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
208 | * @param n a number to determine whether {@code singularText} or {@code pluralText} is used.
|
---|
209 | * @param objects the parameters for the string.
|
---|
210 | * Mark occurrences in {@code singularText} and {@code pluralText} with <code>{0}</code>, <code>{1}</code>, ...
|
---|
211 | * @return the translated string.
|
---|
212 | * @see #tr
|
---|
213 | * @see #trc
|
---|
214 | * @see #trn
|
---|
215 | */
|
---|
216 | public static String trnc(String context, String singularText, String pluralText, long n, Object... objects) {
|
---|
217 | return MessageFormat.format(gettextn(singularText, pluralText, context, n), objects);
|
---|
218 | }
|
---|
219 |
|
---|
220 | private static String gettext(String text, String ctx, boolean lazy) {
|
---|
221 | int i;
|
---|
222 | if (ctx == null && text.startsWith("_:") && (i = text.indexOf('\n')) >= 0) {
|
---|
223 | ctx = text.substring(2, i-1);
|
---|
224 | text = text.substring(i+1);
|
---|
225 | }
|
---|
226 | if (strings != null) {
|
---|
227 | String trans = strings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
|
---|
228 | if (trans != null)
|
---|
229 | return trans;
|
---|
230 | }
|
---|
231 | if (pstrings != null) {
|
---|
232 | i = pluralEval(1);
|
---|
233 | String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
|
---|
234 | if (trans != null && trans.length > i)
|
---|
235 | return trans[i];
|
---|
236 | }
|
---|
237 | return lazy ? gettext(text, null) : text;
|
---|
238 | }
|
---|
239 |
|
---|
240 | private static String gettext(String text, String ctx) {
|
---|
241 | return gettext(text, ctx, false);
|
---|
242 | }
|
---|
243 |
|
---|
244 | /* try without context, when context try fails */
|
---|
245 | private static String gettextLazy(String text, String ctx) {
|
---|
246 | return gettext(text, ctx, true);
|
---|
247 | }
|
---|
248 |
|
---|
249 | private static String gettextn(String text, String plural, String ctx, long num) {
|
---|
250 | int i;
|
---|
251 | if (ctx == null && text.startsWith("_:") && (i = text.indexOf('\n')) >= 0) {
|
---|
252 | ctx = text.substring(2, i-1);
|
---|
253 | text = text.substring(i+1);
|
---|
254 | }
|
---|
255 | if (pstrings != null) {
|
---|
256 | i = pluralEval(num);
|
---|
257 | String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
|
---|
258 | if (trans != null && trans.length > i)
|
---|
259 | return trans[i];
|
---|
260 | }
|
---|
261 |
|
---|
262 | return num == 1 ? text : plural;
|
---|
263 | }
|
---|
264 |
|
---|
265 | public static String escape(String msg) {
|
---|
266 | if (msg == null) return null;
|
---|
267 | return msg.replace("\'", "\'\'").replace("{", "\'{\'").replace("}", "\'}\'");
|
---|
268 | }
|
---|
269 |
|
---|
270 | private static URL getTranslationFile(String lang) {
|
---|
271 | return I18n.class.getResource("/data/"+lang.replace('@', '-')+".lang");
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Get a list of all available JOSM Translations.
|
---|
276 | * @return an array of locale objects.
|
---|
277 | */
|
---|
278 | public static Locale[] getAvailableTranslations() {
|
---|
279 | Collection<Locale> v = new ArrayList<>(languages.size());
|
---|
280 | if (getTranslationFile("en") != null) {
|
---|
281 | for (String loc : languages.keySet()) {
|
---|
282 | if (getTranslationFile(loc) != null) {
|
---|
283 | v.add(LanguageInfo.getLocale(loc));
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|
287 | v.add(Locale.ENGLISH);
|
---|
288 | Locale[] l = new Locale[v.size()];
|
---|
289 | l = v.toArray(l);
|
---|
290 | Arrays.sort(l, Comparator.comparing(Locale::toString));
|
---|
291 | return l;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Determines if a language exists for the given code.
|
---|
296 | * @param code The language code
|
---|
297 | * @return {@code true} if a language exists, {@code false} otherwise
|
---|
298 | */
|
---|
299 | public static boolean hasCode(String code) {
|
---|
300 | return languages.containsKey(code);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * I18n initialization.
|
---|
305 | */
|
---|
306 | public static void init() {
|
---|
307 | // Enable CLDR locale provider on Java 8 to get additional languages, such as Khmer.
|
---|
308 | // http://docs.oracle.com/javase/8/docs/technotes/guides/intl/enhancements.8.html#cldr
|
---|
309 | // FIXME: This can be removed after we switch to a minimal version of Java that enables CLDR by default
|
---|
310 | // or includes all languages we need in the JRE. See http://openjdk.java.net/jeps/252 for Java 9
|
---|
311 | System.setProperty("java.locale.providers", "JRE,CLDR"); // Don't call Utils.updateSystemProperty to avoid spurious log at startup
|
---|
312 |
|
---|
313 | //languages.put("ar", PluralMode.MODE_AR);
|
---|
314 | languages.put("ast", PluralMode.MODE_NOTONE);
|
---|
315 | languages.put("bg", PluralMode.MODE_NOTONE);
|
---|
316 | languages.put("be", PluralMode.MODE_RU);
|
---|
317 | languages.put("ca", PluralMode.MODE_NOTONE);
|
---|
318 | languages.put("ca@valencia", PluralMode.MODE_NOTONE);
|
---|
319 | languages.put("cs", PluralMode.MODE_CS);
|
---|
320 | languages.put("da", PluralMode.MODE_NOTONE);
|
---|
321 | languages.put("de", PluralMode.MODE_NOTONE);
|
---|
322 | languages.put("el", PluralMode.MODE_NOTONE);
|
---|
323 | languages.put("en_AU", PluralMode.MODE_NOTONE);
|
---|
324 | languages.put("en_GB", PluralMode.MODE_NOTONE);
|
---|
325 | languages.put("es", PluralMode.MODE_NOTONE);
|
---|
326 | languages.put("et", PluralMode.MODE_NOTONE);
|
---|
327 | //languages.put("eu", PluralMode.MODE_NOTONE);
|
---|
328 | languages.put("fi", PluralMode.MODE_NOTONE);
|
---|
329 | languages.put("fr", PluralMode.MODE_GREATERONE);
|
---|
330 | languages.put("gl", PluralMode.MODE_NOTONE);
|
---|
331 | //languages.put("he", PluralMode.MODE_NOTONE);
|
---|
332 | languages.put("hu", PluralMode.MODE_NOTONE);
|
---|
333 | languages.put("id", PluralMode.MODE_NONE);
|
---|
334 | //languages.put("is", PluralMode.MODE_NOTONE);
|
---|
335 | languages.put("it", PluralMode.MODE_NOTONE);
|
---|
336 | languages.put("ja", PluralMode.MODE_NONE);
|
---|
337 | // fully supported only with Java 8 and later (needs CLDR)
|
---|
338 | languages.put("km", PluralMode.MODE_NONE);
|
---|
339 | languages.put("lt", PluralMode.MODE_LT);
|
---|
340 | languages.put("nb", PluralMode.MODE_NOTONE);
|
---|
341 | languages.put("nl", PluralMode.MODE_NOTONE);
|
---|
342 | languages.put("pl", PluralMode.MODE_PL);
|
---|
343 | languages.put("pt", PluralMode.MODE_NOTONE);
|
---|
344 | languages.put("pt_BR", PluralMode.MODE_GREATERONE);
|
---|
345 | //languages.put("ro", PluralMode.MODE_RO);
|
---|
346 | languages.put("ru", PluralMode.MODE_RU);
|
---|
347 | languages.put("sk", PluralMode.MODE_SK);
|
---|
348 | //languages.put("sl", PluralMode.MODE_SL);
|
---|
349 | languages.put("sv", PluralMode.MODE_NOTONE);
|
---|
350 | //languages.put("tr", PluralMode.MODE_NONE);
|
---|
351 | languages.put("uk", PluralMode.MODE_RU);
|
---|
352 | languages.put("vi", PluralMode.MODE_NONE);
|
---|
353 | languages.put("zh_CN", PluralMode.MODE_NONE);
|
---|
354 | languages.put("zh_TW", PluralMode.MODE_NONE);
|
---|
355 |
|
---|
356 | /* try initial language settings, may be changed later again */
|
---|
357 | if (!load(LanguageInfo.getJOSMLocaleCode())) {
|
---|
358 | Locale.setDefault(Locale.ENGLISH);
|
---|
359 | }
|
---|
360 | }
|
---|
361 |
|
---|
362 | public static void addTexts(File source) {
|
---|
363 | if ("en".equals(loadedCode))
|
---|
364 | return;
|
---|
365 | final String enfile = "data/en.lang";
|
---|
366 | final String langfile = "data/"+loadedCode+".lang";
|
---|
367 | try (
|
---|
368 | FileInputStream fis = new FileInputStream(source);
|
---|
369 | JarInputStream jar = new JarInputStream(fis)
|
---|
370 | ) {
|
---|
371 | ZipEntry e;
|
---|
372 | boolean found = false;
|
---|
373 | while (!found && (e = jar.getNextEntry()) != null) {
|
---|
374 | String name = e.getName();
|
---|
375 | if (enfile.equals(name))
|
---|
376 | found = true;
|
---|
377 | }
|
---|
378 | if (found) {
|
---|
379 | try (
|
---|
380 | FileInputStream fisTrans = new FileInputStream(source);
|
---|
381 | JarInputStream jarTrans = new JarInputStream(fisTrans)
|
---|
382 | ) {
|
---|
383 | found = false;
|
---|
384 | while (!found && (e = jarTrans.getNextEntry()) != null) {
|
---|
385 | String name = e.getName();
|
---|
386 | if (name.equals(langfile))
|
---|
387 | found = true;
|
---|
388 | }
|
---|
389 | if (found)
|
---|
390 | load(jar, jarTrans, true);
|
---|
391 | }
|
---|
392 | }
|
---|
393 | } catch (IOException e) {
|
---|
394 | // Ignore
|
---|
395 | Logging.trace(e);
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | private static boolean load(String l) {
|
---|
400 | if ("en".equals(l) || "en_US".equals(l)) {
|
---|
401 | strings = null;
|
---|
402 | pstrings = null;
|
---|
403 | loadedCode = "en";
|
---|
404 | pluralMode = PluralMode.MODE_NOTONE;
|
---|
405 | return true;
|
---|
406 | }
|
---|
407 | URL en = getTranslationFile("en");
|
---|
408 | if (en == null)
|
---|
409 | return false;
|
---|
410 | URL tr = getTranslationFile(l);
|
---|
411 | if (tr == null || !languages.containsKey(l)) {
|
---|
412 | return false;
|
---|
413 | }
|
---|
414 | try (
|
---|
415 | InputStream enStream = en.openStream();
|
---|
416 | InputStream trStream = tr.openStream()
|
---|
417 | ) {
|
---|
418 | if (load(enStream, trStream, false)) {
|
---|
419 | pluralMode = languages.get(l);
|
---|
420 | loadedCode = l;
|
---|
421 | return true;
|
---|
422 | }
|
---|
423 | } catch (IOException e) {
|
---|
424 | // Ignore exception
|
---|
425 | Logging.trace(e);
|
---|
426 | }
|
---|
427 | return false;
|
---|
428 | }
|
---|
429 |
|
---|
430 | private static boolean load(InputStream en, InputStream tr, boolean add) {
|
---|
431 | Map<String, String> s;
|
---|
432 | Map<String, String[]> p;
|
---|
433 | if (add) {
|
---|
434 | s = strings;
|
---|
435 | p = pstrings;
|
---|
436 | } else {
|
---|
437 | s = new HashMap<>();
|
---|
438 | p = new HashMap<>();
|
---|
439 | }
|
---|
440 | /* file format:
|
---|
441 | Files are always a group. English file and translated file must provide identical datasets.
|
---|
442 |
|
---|
443 | for all single strings:
|
---|
444 | {
|
---|
445 | unsigned short (2 byte) stringlength
|
---|
446 | - length 0 indicates missing translation
|
---|
447 | - length 0xFFFE indicates translation equal to original, but otherwise is equal to length 0
|
---|
448 | string
|
---|
449 | }
|
---|
450 | unsigned short (2 byte) 0xFFFF (marks end of single strings)
|
---|
451 | for all multi strings:
|
---|
452 | {
|
---|
453 | unsigned char (1 byte) stringcount
|
---|
454 | - count 0 indicates missing translations
|
---|
455 | - count 0xFE indicates translations equal to original, but otherwise is equal to length 0
|
---|
456 | for stringcount
|
---|
457 | unsigned short (2 byte) stringlength
|
---|
458 | string
|
---|
459 | }
|
---|
460 | */
|
---|
461 | try {
|
---|
462 | InputStream ens = new BufferedInputStream(en);
|
---|
463 | InputStream trs = new BufferedInputStream(tr);
|
---|
464 | byte[] enlen = new byte[2];
|
---|
465 | byte[] trlen = new byte[2];
|
---|
466 | boolean multimode = false;
|
---|
467 | byte[] str = new byte[4096];
|
---|
468 | for (;;) {
|
---|
469 | if (multimode) {
|
---|
470 | int ennum = ens.read();
|
---|
471 | int trnum = trs.read();
|
---|
472 | if (trnum == 0xFE) /* marks identical string, handle equally to non-translated */
|
---|
473 | trnum = 0;
|
---|
474 | if ((ennum == -1 && trnum != -1) || (ennum != -1 && trnum == -1)) /* files do not match */
|
---|
475 | return false;
|
---|
476 | if (ennum == -1) {
|
---|
477 | break;
|
---|
478 | }
|
---|
479 | String[] enstrings = new String[ennum];
|
---|
480 | for (int i = 0; i < ennum; ++i) {
|
---|
481 | int val = ens.read(enlen);
|
---|
482 | if (val != 2) /* file corrupt */
|
---|
483 | return false;
|
---|
484 | val = (enlen[0] < 0 ? 256+enlen[0] : enlen[0])*256+(enlen[1] < 0 ? 256+enlen[1] : enlen[1]);
|
---|
485 | if (val > str.length) {
|
---|
486 | str = new byte[val];
|
---|
487 | }
|
---|
488 | int rval = ens.read(str, 0, val);
|
---|
489 | if (rval != val) /* file corrupt */
|
---|
490 | return false;
|
---|
491 | enstrings[i] = new String(str, 0, val, StandardCharsets.UTF_8);
|
---|
492 | }
|
---|
493 | String[] trstrings = new String[trnum];
|
---|
494 | for (int i = 0; i < trnum; ++i) {
|
---|
495 | int val = trs.read(trlen);
|
---|
496 | if (val != 2) /* file corrupt */
|
---|
497 | return false;
|
---|
498 | val = (trlen[0] < 0 ? 256+trlen[0] : trlen[0])*256+(trlen[1] < 0 ? 256+trlen[1] : trlen[1]);
|
---|
499 | if (val > str.length) {
|
---|
500 | str = new byte[val];
|
---|
501 | }
|
---|
502 | int rval = trs.read(str, 0, val);
|
---|
503 | if (rval != val) /* file corrupt */
|
---|
504 | return false;
|
---|
505 | trstrings[i] = new String(str, 0, val, StandardCharsets.UTF_8);
|
---|
506 | }
|
---|
507 | if (trnum > 0 && !p.containsKey(enstrings[0])) {
|
---|
508 | p.put(enstrings[0], trstrings);
|
---|
509 | }
|
---|
510 | } else {
|
---|
511 | int enval = ens.read(enlen);
|
---|
512 | int trval = trs.read(trlen);
|
---|
513 | if (enval != trval) /* files do not match */
|
---|
514 | return false;
|
---|
515 | if (enval == -1) {
|
---|
516 | break;
|
---|
517 | }
|
---|
518 | if (enval != 2) /* files corrupt */
|
---|
519 | return false;
|
---|
520 | enval = (enlen[0] < 0 ? 256+enlen[0] : enlen[0])*256+(enlen[1] < 0 ? 256+enlen[1] : enlen[1]);
|
---|
521 | trval = (trlen[0] < 0 ? 256+trlen[0] : trlen[0])*256+(trlen[1] < 0 ? 256+trlen[1] : trlen[1]);
|
---|
522 | if (trval == 0xFFFE) /* marks identical string, handle equally to non-translated */
|
---|
523 | trval = 0;
|
---|
524 | if (enval == 0xFFFF) {
|
---|
525 | multimode = true;
|
---|
526 | if (trval != 0xFFFF) /* files do not match */
|
---|
527 | return false;
|
---|
528 | } else {
|
---|
529 | if (enval > str.length) {
|
---|
530 | str = new byte[enval];
|
---|
531 | }
|
---|
532 | if (trval > str.length) {
|
---|
533 | str = new byte[trval];
|
---|
534 | }
|
---|
535 | int val = ens.read(str, 0, enval);
|
---|
536 | if (val != enval) /* file corrupt */
|
---|
537 | return false;
|
---|
538 | String enstr = new String(str, 0, enval, StandardCharsets.UTF_8);
|
---|
539 | if (trval != 0) {
|
---|
540 | val = trs.read(str, 0, trval);
|
---|
541 | if (val != trval) /* file corrupt */
|
---|
542 | return false;
|
---|
543 | String trstr = new String(str, 0, trval, StandardCharsets.UTF_8);
|
---|
544 | if (!s.containsKey(enstr))
|
---|
545 | s.put(enstr, trstr);
|
---|
546 | }
|
---|
547 | }
|
---|
548 | }
|
---|
549 | }
|
---|
550 | } catch (IOException e) {
|
---|
551 | Logging.trace(e);
|
---|
552 | return false;
|
---|
553 | }
|
---|
554 | if (!s.isEmpty()) {
|
---|
555 | strings = s;
|
---|
556 | pstrings = p;
|
---|
557 | return true;
|
---|
558 | }
|
---|
559 | return false;
|
---|
560 | }
|
---|
561 |
|
---|
562 | /**
|
---|
563 | * Sets the default locale (see {@link Locale#setDefault(Locale)} to the local
|
---|
564 | * given by <code>localName</code>.
|
---|
565 | *
|
---|
566 | * Ignored if localeName is null. If the locale with name <code>localName</code>
|
---|
567 | * isn't found the default local is set to <tt>en</tt> (english).
|
---|
568 | *
|
---|
569 | * @param localeName the locale name. Ignored if null.
|
---|
570 | */
|
---|
571 | public static void set(String localeName) {
|
---|
572 | if (localeName != null) {
|
---|
573 | Locale l = LanguageInfo.getLocale(localeName);
|
---|
574 | if (load(LanguageInfo.getJOSMLocaleCode(l))) {
|
---|
575 | Locale.setDefault(l);
|
---|
576 | } else {
|
---|
577 | if (!"en".equals(l.getLanguage())) {
|
---|
578 | Logging.info(tr("Unable to find translation for the locale {0}. Reverting to {1}.",
|
---|
579 | LanguageInfo.getDisplayName(l), LanguageInfo.getDisplayName(Locale.getDefault())));
|
---|
580 | } else {
|
---|
581 | strings = null;
|
---|
582 | pstrings = null;
|
---|
583 | }
|
---|
584 | }
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | private static int pluralEval(long n) {
|
---|
589 | switch(pluralMode) {
|
---|
590 | case MODE_NOTONE: /* bg, da, de, el, en, en_GB, es, et, eu, fi, gl, is, it, iw_IL, nb, nl, sv */
|
---|
591 | return (n != 1) ? 1 : 0;
|
---|
592 | case MODE_NONE: /* id, vi, ja, km, tr, zh_CN, zh_TW */
|
---|
593 | return 0;
|
---|
594 | case MODE_GREATERONE: /* fr, pt_BR */
|
---|
595 | return (n > 1) ? 1 : 0;
|
---|
596 | case MODE_CS:
|
---|
597 | return (n == 1) ? 0 : (((n >= 2) && (n <= 4)) ? 1 : 2);
|
---|
598 | //case MODE_AR:
|
---|
599 | // return ((n == 0) ? 0 : ((n == 1) ? 1 : ((n == 2) ? 2 : ((((n % 100) >= 3)
|
---|
600 | // && ((n % 100) <= 10)) ? 3 : ((((n % 100) >= 11) && ((n % 100) <= 99)) ? 4 : 5)))));
|
---|
601 | case MODE_PL:
|
---|
602 | return (n == 1) ? 0 : (((((n % 10) >= 2) && ((n % 10) <= 4))
|
---|
603 | && (((n % 100) < 10) || ((n % 100) >= 20))) ? 1 : 2);
|
---|
604 | //case MODE_RO:
|
---|
605 | // return ((n == 1) ? 0 : ((((n % 100) > 19) || (((n % 100) == 0) && (n != 0))) ? 2 : 1));
|
---|
606 | case MODE_LT:
|
---|
607 | return ((n % 10) == 1) && ((n % 100) != 11) ? 0 : (((n % 10) >= 2)
|
---|
608 | && (((n % 100) < 10) || ((n % 100) >= 20)) ? 1 : 2);
|
---|
609 | case MODE_RU:
|
---|
610 | return (((n % 10) == 1) && ((n % 100) != 11)) ? 0 : (((((n % 10) >= 2)
|
---|
611 | && ((n % 10) <= 4)) && (((n % 100) < 10) || ((n % 100) >= 20))) ? 1 : 2);
|
---|
612 | case MODE_SK:
|
---|
613 | return (n == 1) ? 1 : (((n >= 2) && (n <= 4)) ? 2 : 0);
|
---|
614 | //case MODE_SL:
|
---|
615 | // return (((n % 100) == 1) ? 1 : (((n % 100) == 2) ? 2 : ((((n % 100) == 3)
|
---|
616 | // || ((n % 100) == 4)) ? 3 : 0)));
|
---|
617 | }
|
---|
618 | return 0;
|
---|
619 | }
|
---|
620 | }
|
---|