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.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.lang.annotation.Retention;
|
---|
9 | import java.lang.annotation.RetentionPolicy;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.nio.file.InvalidPathException;
|
---|
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.regex.Matcher;
|
---|
22 | import java.util.regex.Pattern;
|
---|
23 | import java.util.zip.ZipEntry;
|
---|
24 | import java.util.zip.ZipFile;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Internationalisation support.
|
---|
28 | *
|
---|
29 | * @author Immanuel.Scholz
|
---|
30 | */
|
---|
31 | public final class I18n {
|
---|
32 |
|
---|
33 | private static final String CORE_TRANS_DIRECTORY = "/data/";
|
---|
34 | private static final String PLUGIN_TRANS_DIRECTORY = "data/";
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * This annotates strings which do not permit a clean i18n. This is mostly due to strings
|
---|
38 | * containing two nouns which can occur in singular or plural form.
|
---|
39 | * <br>
|
---|
40 | * No behaviour is associated with this annotation.
|
---|
41 | */
|
---|
42 | @Retention(RetentionPolicy.SOURCE)
|
---|
43 | public @interface QuirkyPluralString {
|
---|
44 | }
|
---|
45 |
|
---|
46 | private I18n() {
|
---|
47 | // Hide default constructor for utils classes
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Enumeration of possible plural modes. It allows us to identify and implement logical conditions of
|
---|
52 | * plural forms defined on <a href="https://help.launchpad.net/Translations/PluralForms">Launchpad</a>.
|
---|
53 | * See <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html">CLDR</a>
|
---|
54 | * for another complete list.
|
---|
55 | * @see #pluralEval
|
---|
56 | */
|
---|
57 | private enum PluralMode {
|
---|
58 | /** Plural = Not 1. This is the default for many languages, including English: 1 day, but 0 days or 2 days. */
|
---|
59 | MODE_NOTONE,
|
---|
60 | /** No plural. Mainly for Asian languages (Indonesian, Chinese, Japanese, ...) */
|
---|
61 | MODE_NONE,
|
---|
62 | /** Plural = Greater than 1. For some latin languages (French, Brazilian Portuguese) */
|
---|
63 | MODE_GREATERONE,
|
---|
64 | /* Special mode for
|
---|
65 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ar">Arabic</a>.*/
|
---|
66 | MODE_AR,
|
---|
67 | /** Special mode for
|
---|
68 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#cs">Czech</a>. */
|
---|
69 | MODE_CS,
|
---|
70 | /** Special mode for
|
---|
71 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#pl">Polish</a>. */
|
---|
72 | MODE_PL,
|
---|
73 | /* Special mode for
|
---|
74 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ro">Romanian</a>.*
|
---|
75 | MODE_RO,*/
|
---|
76 | /** Special mode for
|
---|
77 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#lt">Lithuanian</a>. */
|
---|
78 | MODE_LT,
|
---|
79 | /** Special mode for
|
---|
80 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ru">Russian</a>. */
|
---|
81 | MODE_RU,
|
---|
82 | /** Special mode for
|
---|
83 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#sk">Slovak</a>. */
|
---|
84 | MODE_SK,
|
---|
85 | /* Special mode for
|
---|
86 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#sl">Slovenian</a>.*
|
---|
87 | MODE_SL,*/
|
---|
88 | }
|
---|
89 |
|
---|
90 | private static volatile PluralMode pluralMode = PluralMode.MODE_NOTONE; /* english default */
|
---|
91 | private static volatile String loadedCode = "en";
|
---|
92 |
|
---|
93 | /** Map (english/locale) of singular strings **/
|
---|
94 | private static volatile Map<String, String> strings;
|
---|
95 | /** Map (english/locale) of plural strings **/
|
---|
96 | private static volatile Map<String, String[]> pstrings;
|
---|
97 | private static Locale originalLocale = Locale.getDefault();
|
---|
98 | private static Map<String, PluralMode> languages = new HashMap<>();
|
---|
99 | // NOTE: check also WikiLanguage handling in LanguageInfo.java when adding new languages
|
---|
100 | static {
|
---|
101 | languages.put("ar", PluralMode.MODE_AR);
|
---|
102 | languages.put("ast", PluralMode.MODE_NOTONE);
|
---|
103 | languages.put("bg", PluralMode.MODE_NOTONE);
|
---|
104 | languages.put("be", PluralMode.MODE_RU);
|
---|
105 | languages.put("ca", PluralMode.MODE_NOTONE);
|
---|
106 | languages.put("ca@valencia", PluralMode.MODE_NOTONE);
|
---|
107 | languages.put("cs", PluralMode.MODE_CS);
|
---|
108 | languages.put("da", PluralMode.MODE_NOTONE);
|
---|
109 | languages.put("de", PluralMode.MODE_NOTONE);
|
---|
110 | languages.put("el", PluralMode.MODE_NOTONE);
|
---|
111 | languages.put("en_AU", PluralMode.MODE_NOTONE);
|
---|
112 | //languages.put("en_CA", PluralMode.MODE_NOTONE);
|
---|
113 | languages.put("en_GB", PluralMode.MODE_NOTONE);
|
---|
114 | languages.put("es", PluralMode.MODE_NOTONE);
|
---|
115 | languages.put("et", PluralMode.MODE_NOTONE);
|
---|
116 | //languages.put("eu", PluralMode.MODE_NOTONE);
|
---|
117 | languages.put("fi", PluralMode.MODE_NOTONE);
|
---|
118 | languages.put("fr", PluralMode.MODE_GREATERONE);
|
---|
119 | languages.put("gl", PluralMode.MODE_NOTONE);
|
---|
120 | //languages.put("he", PluralMode.MODE_NOTONE);
|
---|
121 | languages.put("hu", PluralMode.MODE_NOTONE);
|
---|
122 | languages.put("id", PluralMode.MODE_NONE);
|
---|
123 | //languages.put("is", PluralMode.MODE_NOTONE);
|
---|
124 | languages.put("it", PluralMode.MODE_NOTONE);
|
---|
125 | languages.put("ja", PluralMode.MODE_NONE);
|
---|
126 | languages.put("ko", PluralMode.MODE_NONE);
|
---|
127 | // fully supported only with Java 8 and later (needs CLDR)
|
---|
128 | languages.put("km", PluralMode.MODE_NONE);
|
---|
129 | languages.put("lt", PluralMode.MODE_LT);
|
---|
130 | languages.put("mr", PluralMode.MODE_NOTONE);
|
---|
131 | languages.put("nb", PluralMode.MODE_NOTONE);
|
---|
132 | languages.put("nl", PluralMode.MODE_NOTONE);
|
---|
133 | languages.put("pl", PluralMode.MODE_PL);
|
---|
134 | languages.put("pt", PluralMode.MODE_NOTONE);
|
---|
135 | languages.put("pt_BR", PluralMode.MODE_GREATERONE);
|
---|
136 | //languages.put("ro", PluralMode.MODE_RO);
|
---|
137 | languages.put("ru", PluralMode.MODE_RU);
|
---|
138 | languages.put("sk", PluralMode.MODE_SK);
|
---|
139 | //languages.put("sl", PluralMode.MODE_SL);
|
---|
140 | languages.put("sv", PluralMode.MODE_NOTONE);
|
---|
141 | //languages.put("tr", PluralMode.MODE_NONE);
|
---|
142 | languages.put("uk", PluralMode.MODE_RU);
|
---|
143 | languages.put("vi", PluralMode.MODE_NONE);
|
---|
144 | languages.put("zh_CN", PluralMode.MODE_NONE);
|
---|
145 | languages.put("zh_TW", PluralMode.MODE_NONE);
|
---|
146 | }
|
---|
147 |
|
---|
148 | private static final String HIRAGANA = "hira";
|
---|
149 | private static final String KATAKANA = "kana";
|
---|
150 | private static final String LATIN = "latn";
|
---|
151 | private static final String PINYIN = "pinyin";
|
---|
152 | private static final String ROMAJI = "rm";
|
---|
153 |
|
---|
154 | // Matches ISO-639 two and three letters language codes + scripts
|
---|
155 | private static final Pattern LANGUAGE_NAMES = Pattern.compile(
|
---|
156 | "name:(\\p{Lower}{2,3})(?:[-_](?i:(" + String.join("|", HIRAGANA, KATAKANA, LATIN, PINYIN, ROMAJI) + ")))?");
|
---|
157 |
|
---|
158 | private static String format(String text, Object... objects) {
|
---|
159 | try {
|
---|
160 | return MessageFormat.format(text, objects);
|
---|
161 | } catch (InvalidPathException e) {
|
---|
162 | System.err.println("!!! Unable to format '" + text + "': " + e.getMessage());
|
---|
163 | e.printStackTrace();
|
---|
164 | return null;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Translates some text for the current locale.
|
---|
170 | * These strings are collected by a script that runs on the source code files.
|
---|
171 | * After translation, the localizations are distributed with the main program.
|
---|
172 | * <br>
|
---|
173 | * For example, <code>tr("JOSM''s default value is ''{0}''.", val)</code>.
|
---|
174 | * <br>
|
---|
175 | * Use {@link #trn} for distinguishing singular from plural text, i.e.,
|
---|
176 | * do not use {@code tr(size == 1 ? "singular" : "plural")} nor
|
---|
177 | * {@code size == 1 ? tr("singular") : tr("plural")}
|
---|
178 | *
|
---|
179 | * @param text the text to translate.
|
---|
180 | * Must be a string literal. (No constants or local vars.)
|
---|
181 | * Can be broken over multiple lines.
|
---|
182 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
183 | * @param objects the parameters for the string.
|
---|
184 | * Mark occurrences in {@code text} with <code>{0}</code>, <code>{1}</code>, ...
|
---|
185 | * @return the translated string.
|
---|
186 | * @see #trn
|
---|
187 | * @see #trc
|
---|
188 | * @see #trnc
|
---|
189 | */
|
---|
190 | public static String tr(String text, Object... objects) {
|
---|
191 | if (text == null) return null;
|
---|
192 | return format(gettext(text, null), objects);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Translates some text in a context for the current locale.
|
---|
197 | * There can be different translations for the same text within different contexts.
|
---|
198 | *
|
---|
199 | * @param context string that helps translators to find an appropriate
|
---|
200 | * translation for {@code text}.
|
---|
201 | * @param text the text to translate.
|
---|
202 | * @return the translated string.
|
---|
203 | * @see #tr
|
---|
204 | * @see #trn
|
---|
205 | * @see #trnc
|
---|
206 | */
|
---|
207 | public static String trc(String context, String text) {
|
---|
208 | if (context == null)
|
---|
209 | return tr(text);
|
---|
210 | if (text == null)
|
---|
211 | return null;
|
---|
212 | return format(gettext(text, context), (Object) null);
|
---|
213 | }
|
---|
214 |
|
---|
215 | public static String trcLazy(String context, String text) {
|
---|
216 | if (context == null)
|
---|
217 | return tr(text);
|
---|
218 | if (text == null)
|
---|
219 | return null;
|
---|
220 | return format(gettextLazy(text, context), (Object) null);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Marks a string for translation (such that a script can harvest
|
---|
225 | * the translatable strings from the source files).
|
---|
226 | *
|
---|
227 | * For example, <code>
|
---|
228 | * String[] options = new String[] {marktr("up"), marktr("down")};
|
---|
229 | * lbl.setText(tr(options[0]));</code>
|
---|
230 | * @param text the string to be marked for translation.
|
---|
231 | * @return {@code text} unmodified.
|
---|
232 | */
|
---|
233 | public static String marktr(String text) {
|
---|
234 | return text;
|
---|
235 | }
|
---|
236 |
|
---|
237 | public static String marktrc(String context, String text) {
|
---|
238 | return text;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Translates some text for the current locale and distinguishes between
|
---|
243 | * {@code singularText} and {@code pluralText} depending on {@code n}.
|
---|
244 | * <br>
|
---|
245 | * For instance, {@code trn("There was an error!", "There were errors!", i)} or
|
---|
246 | * <code>trn("Found {0} error in {1}!", "Found {0} errors in {1}!", i, Integer.toString(i), url)</code>.
|
---|
247 | *
|
---|
248 | * @param singularText the singular text to translate.
|
---|
249 | * Must be a string literal. (No constants or local vars.)
|
---|
250 | * Can be broken over multiple lines.
|
---|
251 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
252 | * @param pluralText the plural text to translate.
|
---|
253 | * Must be a string literal. (No constants or local vars.)
|
---|
254 | * Can be broken over multiple lines.
|
---|
255 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
256 | * @param n a number to determine whether {@code singularText} or {@code pluralText} is used.
|
---|
257 | * @param objects the parameters for the string.
|
---|
258 | * Mark occurrences in {@code singularText} and {@code pluralText} with <code>{0}</code>, <code>{1}</code>, ...
|
---|
259 | * @return the translated string.
|
---|
260 | * @see #tr
|
---|
261 | * @see #trc
|
---|
262 | * @see #trnc
|
---|
263 | */
|
---|
264 | public static String trn(String singularText, String pluralText, long n, Object... objects) {
|
---|
265 | return format(gettextn(singularText, pluralText, null, n), objects);
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Translates some text in a context for the current locale and distinguishes between
|
---|
270 | * {@code singularText} and {@code pluralText} depending on {@code n}.
|
---|
271 | * There can be different translations for the same text within different contexts.
|
---|
272 | *
|
---|
273 | * @param context string that helps translators to find an appropriate
|
---|
274 | * translation for {@code text}.
|
---|
275 | * @param singularText the singular text to translate.
|
---|
276 | * Must be a string literal. (No constants or local vars.)
|
---|
277 | * Can be broken over multiple lines.
|
---|
278 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
279 | * @param pluralText the plural text to translate.
|
---|
280 | * Must be a string literal. (No constants or local vars.)
|
---|
281 | * Can be broken over multiple lines.
|
---|
282 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
283 | * @param n a number to determine whether {@code singularText} or {@code pluralText} is used.
|
---|
284 | * @param objects the parameters for the string.
|
---|
285 | * Mark occurrences in {@code singularText} and {@code pluralText} with <code>{0}</code>, <code>{1}</code>, ...
|
---|
286 | * @return the translated string.
|
---|
287 | * @see #tr
|
---|
288 | * @see #trc
|
---|
289 | * @see #trn
|
---|
290 | */
|
---|
291 | public static String trnc(String context, String singularText, String pluralText, long n, Object... objects) {
|
---|
292 | return format(gettextn(singularText, pluralText, context, n), objects);
|
---|
293 | }
|
---|
294 |
|
---|
295 | private static String gettext(String text, String ctx, boolean lazy) {
|
---|
296 | int i;
|
---|
297 | if (ctx == null && text.startsWith("_:") && (i = text.indexOf('\n')) >= 0) {
|
---|
298 | ctx = text.substring(2, i-1);
|
---|
299 | text = text.substring(i+1);
|
---|
300 | }
|
---|
301 | if (strings != null) {
|
---|
302 | String trans = strings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
|
---|
303 | if (trans != null)
|
---|
304 | return trans;
|
---|
305 | }
|
---|
306 | if (pstrings != null) {
|
---|
307 | i = pluralEval(1);
|
---|
308 | String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
|
---|
309 | if (trans != null && trans.length > i)
|
---|
310 | return trans[i];
|
---|
311 | }
|
---|
312 | return lazy ? gettext(text, null) : text;
|
---|
313 | }
|
---|
314 |
|
---|
315 | private static String gettext(String text, String ctx) {
|
---|
316 | return gettext(text, ctx, false);
|
---|
317 | }
|
---|
318 |
|
---|
319 | /* try without context, when context try fails */
|
---|
320 | private static String gettextLazy(String text, String ctx) {
|
---|
321 | return gettext(text, ctx, true);
|
---|
322 | }
|
---|
323 |
|
---|
324 | private static String gettextn(String text, String plural, String ctx, long num) {
|
---|
325 | int i;
|
---|
326 | if (ctx == null && text.startsWith("_:") && (i = text.indexOf('\n')) >= 0) {
|
---|
327 | ctx = text.substring(2, i-1);
|
---|
328 | text = text.substring(i+1);
|
---|
329 | }
|
---|
330 | if (pstrings != null) {
|
---|
331 | i = pluralEval(num);
|
---|
332 | String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
|
---|
333 | if (trans != null && trans.length > i)
|
---|
334 | return trans[i];
|
---|
335 | }
|
---|
336 |
|
---|
337 | return num == 1 ? text : plural;
|
---|
338 | }
|
---|
339 |
|
---|
340 | public static String escape(String msg) {
|
---|
341 | if (msg == null) return null;
|
---|
342 | return msg.replace("\'", "\'\'").replace("{", "\'{\'").replace("}", "\'}\'");
|
---|
343 | }
|
---|
344 |
|
---|
345 | private static URL getTranslationFile(String lang) {
|
---|
346 | return I18n.class.getResource(CORE_TRANS_DIRECTORY + lang.replace('@', '-') + ".lang");
|
---|
347 | }
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Get a list of all available JOSM Translations.
|
---|
351 | * @return an array of locale objects.
|
---|
352 | */
|
---|
353 | public static Locale[] getAvailableTranslations() {
|
---|
354 | Collection<Locale> v = new ArrayList<>(languages.size());
|
---|
355 | if (getTranslationFile("en") != null) {
|
---|
356 | for (String loc : languages.keySet()) {
|
---|
357 | if (getTranslationFile(loc) != null) {
|
---|
358 | v.add(LanguageInfo.getLocale(loc));
|
---|
359 | }
|
---|
360 | }
|
---|
361 | }
|
---|
362 | v.add(Locale.ENGLISH);
|
---|
363 | Locale[] l = new Locale[v.size()];
|
---|
364 | l = v.toArray(l);
|
---|
365 | Arrays.sort(l, Comparator.comparing(Locale::toString));
|
---|
366 | return l;
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Determines if a language exists for the given code.
|
---|
371 | * @param code The language code
|
---|
372 | * @return {@code true} if a language exists, {@code false} otherwise
|
---|
373 | */
|
---|
374 | public static boolean hasCode(String code) {
|
---|
375 | return languages.containsKey(code);
|
---|
376 | }
|
---|
377 |
|
---|
378 | static String setupJavaLocaleProviders() {
|
---|
379 | // Look up SPI providers first (for JosmDecimalFormatSymbolsProvider).
|
---|
380 | // Enable CLDR locale provider on Java 8 to get additional languages, such as Khmer.
|
---|
381 | // https://docs.oracle.com/javase/8/docs/technotes/guides/intl/enhancements.8.html#cldr
|
---|
382 | // FIXME: This must be updated after we switch to Java 9.
|
---|
383 | // See https://docs.oracle.com/javase/9/docs/api/java/util/spi/LocaleServiceProvider.html
|
---|
384 | try {
|
---|
385 | try {
|
---|
386 | // First check we're able to open a stream to our own SPI file
|
---|
387 | // Java will fail on Windows if the jar file is in a folder with a space character!
|
---|
388 | I18n.class.getResourceAsStream("/META-INF/services/java.text.spi.DecimalFormatSymbolsProvider").close();
|
---|
389 | // Don't call Utils.updateSystemProperty to avoid spurious log at startup
|
---|
390 | return System.setProperty("java.locale.providers", "SPI,JRE,CLDR");
|
---|
391 | } catch (RuntimeException | IOException e) {
|
---|
392 | // Don't call Logging class, it may not be fully initialized yet
|
---|
393 | System.err.println("Unable to set SPI locale provider: " + e.getMessage());
|
---|
394 | }
|
---|
395 | } catch (SecurityException e) {
|
---|
396 | // Don't call Logging class, it may not be fully initialized yet
|
---|
397 | System.err.println("Unable to set locale providers: " + e.getMessage());
|
---|
398 | }
|
---|
399 | return System.setProperty("java.locale.providers", "JRE,CLDR");
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * I18n initialization.
|
---|
404 | */
|
---|
405 | public static void init() {
|
---|
406 | setupJavaLocaleProviders();
|
---|
407 |
|
---|
408 | /* try initial language settings, may be changed later again */
|
---|
409 | if (!load(LanguageInfo.getJOSMLocaleCode())) {
|
---|
410 | Locale.setDefault(new Locale("en", Locale.getDefault().getCountry()));
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * I18n initialization for plugins.
|
---|
416 | * @param source file path/name of the JAR or Zip file containing translation strings
|
---|
417 | * @since 4159
|
---|
418 | */
|
---|
419 | public static void addTexts(File source) {
|
---|
420 | if ("en".equals(loadedCode))
|
---|
421 | return;
|
---|
422 | final ZipEntry enfile = new ZipEntry(PLUGIN_TRANS_DIRECTORY + "en.lang");
|
---|
423 | final ZipEntry langfile = new ZipEntry(PLUGIN_TRANS_DIRECTORY + loadedCode + ".lang");
|
---|
424 | try (
|
---|
425 | ZipFile zipFile = new ZipFile(source, StandardCharsets.UTF_8);
|
---|
426 | InputStream orig = zipFile.getInputStream(enfile);
|
---|
427 | InputStream trans = zipFile.getInputStream(langfile)
|
---|
428 | ) {
|
---|
429 | if (orig != null && trans != null)
|
---|
430 | load(orig, trans, true);
|
---|
431 | } catch (IOException | InvalidPathException e) {
|
---|
432 | Logging.trace(e);
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 | private static boolean load(String l) {
|
---|
437 | if ("en".equals(l) || "en_US".equals(l)) {
|
---|
438 | strings = null;
|
---|
439 | pstrings = null;
|
---|
440 | loadedCode = "en";
|
---|
441 | pluralMode = PluralMode.MODE_NOTONE;
|
---|
442 | return true;
|
---|
443 | }
|
---|
444 | URL en = getTranslationFile("en");
|
---|
445 | if (en == null)
|
---|
446 | return false;
|
---|
447 | URL tr = getTranslationFile(l);
|
---|
448 | if (tr == null || !languages.containsKey(l)) {
|
---|
449 | return false;
|
---|
450 | }
|
---|
451 | try (
|
---|
452 | InputStream enStream = Utils.openStream(en);
|
---|
453 | InputStream trStream = Utils.openStream(tr)
|
---|
454 | ) {
|
---|
455 | if (load(enStream, trStream, false)) {
|
---|
456 | pluralMode = languages.get(l);
|
---|
457 | loadedCode = l;
|
---|
458 | return true;
|
---|
459 | }
|
---|
460 | } catch (IOException e) {
|
---|
461 | // Ignore exception
|
---|
462 | Logging.trace(e);
|
---|
463 | }
|
---|
464 | return false;
|
---|
465 | }
|
---|
466 |
|
---|
467 | private static boolean load(InputStream en, InputStream tr, boolean add) {
|
---|
468 | Map<String, String> s;
|
---|
469 | Map<String, String[]> p;
|
---|
470 | if (add) {
|
---|
471 | s = strings;
|
---|
472 | p = pstrings;
|
---|
473 | } else {
|
---|
474 | s = new HashMap<>();
|
---|
475 | p = new HashMap<>();
|
---|
476 | }
|
---|
477 | /* file format:
|
---|
478 | Files are always a group. English file and translated file must provide identical datasets.
|
---|
479 |
|
---|
480 | for all single strings:
|
---|
481 | {
|
---|
482 | unsigned short (2 byte) stringlength
|
---|
483 | - length 0 indicates missing translation
|
---|
484 | - length 0xFFFE indicates translation equal to original, but otherwise is equal to length 0
|
---|
485 | string
|
---|
486 | }
|
---|
487 | unsigned short (2 byte) 0xFFFF (marks end of single strings)
|
---|
488 | for all multi strings:
|
---|
489 | {
|
---|
490 | unsigned char (1 byte) stringcount
|
---|
491 | - count 0 indicates missing translations
|
---|
492 | - count 0xFE indicates translations equal to original, but otherwise is equal to length 0
|
---|
493 | for stringcount
|
---|
494 | unsigned short (2 byte) stringlength
|
---|
495 | string
|
---|
496 | }
|
---|
497 | */
|
---|
498 | try {
|
---|
499 | InputStream ens = new BufferedInputStream(en);
|
---|
500 | InputStream trs = new BufferedInputStream(tr);
|
---|
501 | byte[] enlen = new byte[2];
|
---|
502 | byte[] trlen = new byte[2];
|
---|
503 | boolean multimode = false;
|
---|
504 | byte[] str = new byte[4096];
|
---|
505 | for (;;) {
|
---|
506 | if (multimode) {
|
---|
507 | int ennum = ens.read();
|
---|
508 | int trnum = trs.read();
|
---|
509 | if (trnum == 0xFE) /* marks identical string, handle equally to non-translated */
|
---|
510 | trnum = 0;
|
---|
511 | if ((ennum == -1 && trnum != -1) || (ennum != -1 && trnum == -1)) /* files do not match */
|
---|
512 | return false;
|
---|
513 | if (ennum == -1) {
|
---|
514 | break;
|
---|
515 | }
|
---|
516 | String[] enstrings = new String[ennum];
|
---|
517 | for (int i = 0; i < ennum; ++i) {
|
---|
518 | int val = ens.read(enlen);
|
---|
519 | if (val != 2) /* file corrupt */
|
---|
520 | return false;
|
---|
521 | val = (enlen[0] < 0 ? 256+enlen[0] : enlen[0])*256+(enlen[1] < 0 ? 256+enlen[1] : enlen[1]);
|
---|
522 | if (val > str.length) {
|
---|
523 | str = new byte[val];
|
---|
524 | }
|
---|
525 | int rval = ens.read(str, 0, val);
|
---|
526 | if (rval != val) /* file corrupt */
|
---|
527 | return false;
|
---|
528 | enstrings[i] = new String(str, 0, val, StandardCharsets.UTF_8);
|
---|
529 | }
|
---|
530 | String[] trstrings = new String[trnum];
|
---|
531 | for (int i = 0; i < trnum; ++i) {
|
---|
532 | int val = trs.read(trlen);
|
---|
533 | if (val != 2) /* file corrupt */
|
---|
534 | return false;
|
---|
535 | val = (trlen[0] < 0 ? 256+trlen[0] : trlen[0])*256+(trlen[1] < 0 ? 256+trlen[1] : trlen[1]);
|
---|
536 | if (val > str.length) {
|
---|
537 | str = new byte[val];
|
---|
538 | }
|
---|
539 | int rval = trs.read(str, 0, val);
|
---|
540 | if (rval != val) /* file corrupt */
|
---|
541 | return false;
|
---|
542 | trstrings[i] = new String(str, 0, val, StandardCharsets.UTF_8);
|
---|
543 | }
|
---|
544 | if (trnum > 0 && !p.containsKey(enstrings[0])) {
|
---|
545 | p.put(enstrings[0], trstrings);
|
---|
546 | }
|
---|
547 | } else {
|
---|
548 | int enval = ens.read(enlen);
|
---|
549 | int trval = trs.read(trlen);
|
---|
550 | if (enval != trval) /* files do not match */
|
---|
551 | return false;
|
---|
552 | if (enval == -1) {
|
---|
553 | break;
|
---|
554 | }
|
---|
555 | if (enval != 2) /* files corrupt */
|
---|
556 | return false;
|
---|
557 | enval = (enlen[0] < 0 ? 256+enlen[0] : enlen[0])*256+(enlen[1] < 0 ? 256+enlen[1] : enlen[1]);
|
---|
558 | trval = (trlen[0] < 0 ? 256+trlen[0] : trlen[0])*256+(trlen[1] < 0 ? 256+trlen[1] : trlen[1]);
|
---|
559 | if (trval == 0xFFFE) /* marks identical string, handle equally to non-translated */
|
---|
560 | trval = 0;
|
---|
561 | if (enval == 0xFFFF) {
|
---|
562 | multimode = true;
|
---|
563 | if (trval != 0xFFFF) /* files do not match */
|
---|
564 | return false;
|
---|
565 | } else {
|
---|
566 | if (enval > str.length) {
|
---|
567 | str = new byte[enval];
|
---|
568 | }
|
---|
569 | if (trval > str.length) {
|
---|
570 | str = new byte[trval];
|
---|
571 | }
|
---|
572 | int val = ens.read(str, 0, enval);
|
---|
573 | if (val != enval) /* file corrupt */
|
---|
574 | return false;
|
---|
575 | String enstr = new String(str, 0, enval, StandardCharsets.UTF_8);
|
---|
576 | if (trval != 0) {
|
---|
577 | val = trs.read(str, 0, trval);
|
---|
578 | if (val != trval) /* file corrupt */
|
---|
579 | return false;
|
---|
580 | String trstr = new String(str, 0, trval, StandardCharsets.UTF_8);
|
---|
581 | if (!s.containsKey(enstr))
|
---|
582 | s.put(enstr, trstr);
|
---|
583 | }
|
---|
584 | }
|
---|
585 | }
|
---|
586 | }
|
---|
587 | } catch (IOException e) {
|
---|
588 | Logging.trace(e);
|
---|
589 | return false;
|
---|
590 | }
|
---|
591 | if (!s.isEmpty()) {
|
---|
592 | strings = s;
|
---|
593 | pstrings = p;
|
---|
594 | return true;
|
---|
595 | }
|
---|
596 | return false;
|
---|
597 | }
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Sets the default locale (see {@link Locale#setDefault(Locale)} to the local
|
---|
601 | * given by <code>localName</code>.
|
---|
602 | *
|
---|
603 | * Ignored if localeName is null. If the locale with name <code>localName</code>
|
---|
604 | * isn't found the default local is set to <code>en</code> (english).
|
---|
605 | *
|
---|
606 | * @param localeName the locale name. Ignored if null.
|
---|
607 | */
|
---|
608 | public static void set(String localeName) {
|
---|
609 | if (localeName != null) {
|
---|
610 | Locale l = LanguageInfo.getLocale(localeName, true);
|
---|
611 | if (load(LanguageInfo.getJOSMLocaleCode(l))) {
|
---|
612 | Locale.setDefault(l);
|
---|
613 | } else {
|
---|
614 | if (!"en".equals(l.getLanguage())) {
|
---|
615 | Logging.info(tr("Unable to find translation for the locale {0}. Reverting to {1}.",
|
---|
616 | LanguageInfo.getDisplayName(l), LanguageInfo.getDisplayName(Locale.getDefault())));
|
---|
617 | } else {
|
---|
618 | strings = null;
|
---|
619 | pstrings = null;
|
---|
620 | }
|
---|
621 | }
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | private static int pluralEval(long n) {
|
---|
626 | switch(pluralMode) {
|
---|
627 | case MODE_NOTONE: /* bg, da, de, el, en, en_AU, en_CA, en_GB, es, et, eu, fi, gl, is, it, iw_IL, mr, nb, nl, sv */
|
---|
628 | return (n != 1) ? 1 : 0;
|
---|
629 | case MODE_NONE: /* id, vi, ja, km, tr, zh_CN, zh_TW */
|
---|
630 | return 0;
|
---|
631 | case MODE_GREATERONE: /* fr, pt_BR */
|
---|
632 | return (n > 1) ? 1 : 0;
|
---|
633 | case MODE_CS:
|
---|
634 | return (n == 1) ? 0 : (((n >= 2) && (n <= 4)) ? 1 : 2);
|
---|
635 | case MODE_AR:
|
---|
636 | return ((n == 0) ? 0 : ((n == 1) ? 1 : ((n == 2) ? 2 : ((((n % 100) >= 3)
|
---|
637 | && ((n % 100) <= 10)) ? 3 : ((((n % 100) >= 11) && ((n % 100) <= 99)) ? 4 : 5)))));
|
---|
638 | case MODE_PL:
|
---|
639 | return (n == 1) ? 0 : (((((n % 10) >= 2) && ((n % 10) <= 4))
|
---|
640 | && (((n % 100) < 10) || ((n % 100) >= 20))) ? 1 : 2);
|
---|
641 | //case MODE_RO:
|
---|
642 | // return ((n == 1) ? 0 : ((((n % 100) > 19) || (((n % 100) == 0) && (n != 0))) ? 2 : 1));
|
---|
643 | case MODE_LT:
|
---|
644 | return ((n % 10) == 1) && ((n % 100) != 11) ? 0 : (((n % 10) >= 2)
|
---|
645 | && (((n % 100) < 10) || ((n % 100) >= 20)) ? 1 : 2);
|
---|
646 | case MODE_RU:
|
---|
647 | return (((n % 10) == 1) && ((n % 100) != 11)) ? 0 : (((((n % 10) >= 2)
|
---|
648 | && ((n % 10) <= 4)) && (((n % 100) < 10) || ((n % 100) >= 20))) ? 1 : 2);
|
---|
649 | case MODE_SK:
|
---|
650 | return (n == 1) ? 1 : (((n >= 2) && (n <= 4)) ? 2 : 0);
|
---|
651 | //case MODE_SL:
|
---|
652 | // return (((n % 100) == 1) ? 1 : (((n % 100) == 2) ? 2 : ((((n % 100) == 3)
|
---|
653 | // || ((n % 100) == 4)) ? 3 : 0)));
|
---|
654 | }
|
---|
655 | return 0;
|
---|
656 | }
|
---|
657 |
|
---|
658 | /**
|
---|
659 | * Returns the map of singular translations.
|
---|
660 | * @return the map of singular translations.
|
---|
661 | * @since 13761
|
---|
662 | */
|
---|
663 | public static Map<String, String> getSingularTranslations() {
|
---|
664 | return new HashMap<>(strings);
|
---|
665 | }
|
---|
666 |
|
---|
667 | /**
|
---|
668 | * Returns the map of plural translations.
|
---|
669 | * @return the map of plural translations.
|
---|
670 | * @since 13761
|
---|
671 | */
|
---|
672 | public static Map<String, String[]> getPluralTranslations() {
|
---|
673 | return new HashMap<>(pstrings);
|
---|
674 | }
|
---|
675 |
|
---|
676 | /**
|
---|
677 | * Returns the original default locale found when the JVM started.
|
---|
678 | * Used to guess real language/country of current user disregarding language chosen in JOSM preferences.
|
---|
679 | * @return the original default locale found when the JVM started
|
---|
680 | * @since 14013
|
---|
681 | */
|
---|
682 | public static Locale getOriginalLocale() {
|
---|
683 | return originalLocale;
|
---|
684 | }
|
---|
685 |
|
---|
686 | /**
|
---|
687 | * Returns the localized name of the given script. Only scripts used in the OSM database are known.
|
---|
688 | * @param script Writing system
|
---|
689 | * @return the localized name of the given script, or null
|
---|
690 | * @since 15501
|
---|
691 | */
|
---|
692 | public static String getLocalizedScript(String script) {
|
---|
693 | if (script != null) {
|
---|
694 | switch (script.toLowerCase(Locale.ENGLISH)) {
|
---|
695 | case HIRAGANA:
|
---|
696 | return /* I18n: a Japanese syllabary */ tr("Hiragana");
|
---|
697 | case KATAKANA:
|
---|
698 | return /* I18n: a Japanese syllabary */ tr("Katakana");
|
---|
699 | case LATIN:
|
---|
700 | return /* I18n: usage of latin letters/script for usually non-latin languages */ tr("Latin");
|
---|
701 | case PINYIN:
|
---|
702 | return /* I18n: official romanization system for Standard Chinese */ tr("Pinyin");
|
---|
703 | case ROMAJI:
|
---|
704 | return /* I18n: a Japanese syllabary (latin script) */ tr("Rōmaji");
|
---|
705 | default:
|
---|
706 | Logging.warn("Unsupported script: {0}", script);
|
---|
707 | }
|
---|
708 | }
|
---|
709 | return null;
|
---|
710 | }
|
---|
711 |
|
---|
712 | /**
|
---|
713 | * Returns the localized name of the given language and optional script.
|
---|
714 | * @param language Language
|
---|
715 | * @return the pair of localized name + known state of the given language, or null
|
---|
716 | * @since 15501
|
---|
717 | */
|
---|
718 | public static Pair<String, Boolean> getLocalizedLanguageName(String language) {
|
---|
719 | Matcher m = LANGUAGE_NAMES.matcher(language);
|
---|
720 | if (m.matches()) {
|
---|
721 | String code = m.group(1);
|
---|
722 | String label = new Locale(code).getDisplayLanguage();
|
---|
723 | boolean knownNameKey = !code.equals(label);
|
---|
724 | String script = getLocalizedScript(m.group(2));
|
---|
725 | if (script != null) {
|
---|
726 | label += " (" + script + ")";
|
---|
727 | }
|
---|
728 | return new Pair<>(label, knownNameKey);
|
---|
729 | }
|
---|
730 | return null;
|
---|
731 | }
|
---|
732 | }
|
---|