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