[8228] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[626] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[8282] | 4 | import java.awt.GraphicsEnvironment;
|
---|
[2754] | 5 | import java.io.BufferedInputStream;
|
---|
[4159] | 6 | import java.io.File;
|
---|
| 7 | import java.io.FileInputStream;
|
---|
[6248] | 8 | import java.io.IOException;
|
---|
[2754] | 9 | import java.io.InputStream;
|
---|
[8975] | 10 | import java.lang.annotation.Retention;
|
---|
| 11 | import java.lang.annotation.RetentionPolicy;
|
---|
[2754] | 12 | import java.net.URL;
|
---|
[7082] | 13 | import java.nio.charset.StandardCharsets;
|
---|
[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 |
|
---|
[3340] | 25 | import javax.swing.JColorChooser;
|
---|
[3294] | 26 | import javax.swing.JFileChooser;
|
---|
| 27 | import javax.swing.UIManager;
|
---|
| 28 |
|
---|
[4489] | 29 | import org.openstreetmap.gui.jmapviewer.FeatureAdapter.TranslationAdapter;
|
---|
[2754] | 30 | import org.openstreetmap.josm.Main;
|
---|
[8282] | 31 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[7578] | 32 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
[626] | 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Internationalisation support.
|
---|
[1169] | 36 | *
|
---|
[626] | 37 | * @author Immanuel.Scholz
|
---|
| 38 | */
|
---|
[6362] | 39 | public final class I18n {
|
---|
[6830] | 40 |
|
---|
[8975] | 41 | /**
|
---|
| 42 | * This annotates strings which do not permit a clean i18n. This is mostly due to strings
|
---|
| 43 | * containing two nouns which can occur in singular or plural form.
|
---|
[8989] | 44 | * <br>
|
---|
| 45 | * No behaviour is associated with this annotation.
|
---|
[8975] | 46 | */
|
---|
| 47 | @Retention(RetentionPolicy.SOURCE)
|
---|
| 48 | public @interface QuirkyPluralString {
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[6360] | 51 | private I18n() {
|
---|
| 52 | // Hide default constructor for utils classes
|
---|
| 53 | }
|
---|
[6830] | 54 |
|
---|
[7894] | 55 | /**
|
---|
[7901] | 56 | * Enumeration of possible plural modes. It allows us to identify and implement logical conditions of
|
---|
| 57 | * plural forms defined on <a href="https://help.launchpad.net/Translations/PluralForms">Launchpad</a>.
|
---|
[7894] | 58 | * See <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html">CLDR</a>
|
---|
[7901] | 59 | * for another complete list.
|
---|
[7894] | 60 | * @see #pluralEval
|
---|
| 61 | */
|
---|
[7893] | 62 | private enum PluralMode {
|
---|
[7894] | 63 | /** Plural = Not 1. This is the default for many languages, including English: 1 day, but 0 days or 2 days. */
|
---|
| 64 | MODE_NOTONE,
|
---|
| 65 | /** No plural. Mainly for Asian languages (Indonesian, Chinese, Japanese, ...) */
|
---|
| 66 | MODE_NONE,
|
---|
| 67 | /** Plural = Greater than 1. For some latin languages (French, Brazilian Portuguese) */
|
---|
| 68 | MODE_GREATERONE,
|
---|
| 69 | /* Special mode for
|
---|
| 70 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ar">Arabic</a>.*
|
---|
| 71 | MODE_AR,*/
|
---|
| 72 | /** Special mode for
|
---|
| 73 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#cs">Czech</a>. */
|
---|
| 74 | MODE_CS,
|
---|
| 75 | /** Special mode for
|
---|
| 76 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#pl">Polish</a>. */
|
---|
| 77 | MODE_PL,
|
---|
| 78 | /* Special mode for
|
---|
| 79 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ro">Romanian</a>.*
|
---|
| 80 | MODE_RO,*/
|
---|
| 81 | /** Special mode for
|
---|
[8160] | 82 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#lt">Lithuanian</a>. */
|
---|
| 83 | MODE_LT,
|
---|
| 84 | /** Special mode for
|
---|
[7894] | 85 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#ru">Russian</a>. */
|
---|
| 86 | MODE_RU,
|
---|
| 87 | /** Special mode for
|
---|
| 88 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#sk">Slovak</a>. */
|
---|
| 89 | MODE_SK,
|
---|
| 90 | /* Special mode for
|
---|
| 91 | * <a href="http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html#sl">Slovenian</a>.*
|
---|
| 92 | MODE_SL,*/
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[8126] | 95 | private static volatile PluralMode pluralMode = PluralMode.MODE_NOTONE; /* english default */
|
---|
| 96 | private static volatile String loadedCode = "en";
|
---|
[3340] | 97 |
|
---|
| 98 | /* Localization keys for file chooser (and color chooser). */
|
---|
[3433] | 99 | private static final String[] javaInternalMessageKeys = new String[] {
|
---|
| 100 | /* JFileChooser windows laf */
|
---|
[3294] | 101 | "FileChooser.detailsViewActionLabelText",
|
---|
| 102 | "FileChooser.detailsViewButtonAccessibleName",
|
---|
| 103 | "FileChooser.detailsViewButtonToolTipText",
|
---|
| 104 | "FileChooser.fileAttrHeaderText",
|
---|
| 105 | "FileChooser.fileDateHeaderText",
|
---|
| 106 | "FileChooser.fileNameHeaderText",
|
---|
| 107 | "FileChooser.fileNameLabelText",
|
---|
| 108 | "FileChooser.fileSizeHeaderText",
|
---|
| 109 | "FileChooser.fileTypeHeaderText",
|
---|
| 110 | "FileChooser.filesOfTypeLabelText",
|
---|
| 111 | "FileChooser.homeFolderAccessibleName",
|
---|
| 112 | "FileChooser.homeFolderToolTipText",
|
---|
| 113 | "FileChooser.listViewActionLabelText",
|
---|
| 114 | "FileChooser.listViewButtonAccessibleName",
|
---|
| 115 | "FileChooser.listViewButtonToolTipText",
|
---|
| 116 | "FileChooser.lookInLabelText",
|
---|
| 117 | "FileChooser.newFolderAccessibleName",
|
---|
| 118 | "FileChooser.newFolderActionLabelText",
|
---|
| 119 | "FileChooser.newFolderToolTipText",
|
---|
| 120 | "FileChooser.refreshActionLabelText",
|
---|
| 121 | "FileChooser.saveInLabelText",
|
---|
| 122 | "FileChooser.upFolderAccessibleName",
|
---|
| 123 | "FileChooser.upFolderToolTipText",
|
---|
[3326] | 124 | "FileChooser.viewMenuLabelText",
|
---|
[3328] | 125 |
|
---|
[3433] | 126 | /* JFileChooser gtk laf */
|
---|
[3326] | 127 | "FileChooser.acceptAllFileFilterText",
|
---|
| 128 | "FileChooser.cancelButtonText",
|
---|
| 129 | "FileChooser.cancelButtonToolTipText",
|
---|
| 130 | "FileChooser.deleteFileButtonText",
|
---|
| 131 | "FileChooser.filesLabelText",
|
---|
| 132 | "FileChooser.filterLabelText",
|
---|
| 133 | "FileChooser.foldersLabelText",
|
---|
| 134 | "FileChooser.newFolderButtonText",
|
---|
| 135 | "FileChooser.newFolderDialogText",
|
---|
| 136 | "FileChooser.openButtonText",
|
---|
| 137 | "FileChooser.openButtonToolTipText",
|
---|
| 138 | "FileChooser.openDialogTitleText",
|
---|
| 139 | "FileChooser.pathLabelText",
|
---|
| 140 | "FileChooser.renameFileButtonText",
|
---|
| 141 | "FileChooser.renameFileDialogText",
|
---|
| 142 | "FileChooser.renameFileErrorText",
|
---|
| 143 | "FileChooser.renameFileErrorTitle",
|
---|
| 144 | "FileChooser.saveButtonText",
|
---|
| 145 | "FileChooser.saveButtonToolTipText",
|
---|
| 146 | "FileChooser.saveDialogTitleText",
|
---|
[3328] | 147 |
|
---|
[3433] | 148 | /* JFileChooser motif laf */
|
---|
[3328] | 149 | //"FileChooser.cancelButtonText",
|
---|
| 150 | //"FileChooser.cancelButtonToolTipText",
|
---|
[3326] | 151 | "FileChooser.enterFileNameLabelText",
|
---|
[3328] | 152 | //"FileChooser.filesLabelText",
|
---|
| 153 | //"FileChooser.filterLabelText",
|
---|
| 154 | //"FileChooser.foldersLabelText",
|
---|
[3326] | 155 | "FileChooser.helpButtonText",
|
---|
| 156 | "FileChooser.helpButtonToolTipText",
|
---|
[3328] | 157 | //"FileChooser.openButtonText",
|
---|
| 158 | //"FileChooser.openButtonToolTipText",
|
---|
| 159 | //"FileChooser.openDialogTitleText",
|
---|
| 160 | //"FileChooser.pathLabelText",
|
---|
| 161 | //"FileChooser.saveButtonText",
|
---|
| 162 | //"FileChooser.saveButtonToolTipText",
|
---|
| 163 | //"FileChooser.saveDialogTitleText",
|
---|
[3326] | 164 | "FileChooser.updateButtonText",
|
---|
[3340] | 165 | "FileChooser.updateButtonToolTipText",
|
---|
| 166 |
|
---|
[3433] | 167 | /* gtk color chooser */
|
---|
[3340] | 168 | "GTKColorChooserPanel.blueText",
|
---|
| 169 | "GTKColorChooserPanel.colorNameText",
|
---|
| 170 | "GTKColorChooserPanel.greenText",
|
---|
| 171 | "GTKColorChooserPanel.hueText",
|
---|
| 172 | "GTKColorChooserPanel.nameText",
|
---|
| 173 | "GTKColorChooserPanel.redText",
|
---|
| 174 | "GTKColorChooserPanel.saturationText",
|
---|
[3433] | 175 | "GTKColorChooserPanel.valueText",
|
---|
[3530] | 176 |
|
---|
[3433] | 177 | /* JOptionPane */
|
---|
| 178 | "OptionPane.okButtonText",
|
---|
| 179 | "OptionPane.yesButtonText",
|
---|
| 180 | "OptionPane.noButtonText",
|
---|
| 181 | "OptionPane.cancelButtonText"
|
---|
[3340] | 182 | };
|
---|
[8840] | 183 | private static volatile Map<String, String> strings;
|
---|
| 184 | private static volatile Map<String, String[]> pstrings;
|
---|
[7005] | 185 | private static Map<String, PluralMode> languages = new HashMap<>();
|
---|
[1065] | 186 |
|
---|
[1169] | 187 | /**
|
---|
[3461] | 188 | * Translates some text for the current locale.
|
---|
| 189 | * These strings are collected by a script that runs on the source code files.
|
---|
| 190 | * After translation, the localizations are distributed with the main program.
|
---|
[6830] | 191 | * <br>
|
---|
[7893] | 192 | * For example, <code>tr("JOSM''s default value is ''{0}''.", val)</code>.
|
---|
[6830] | 193 | * <br>
|
---|
[5266] | 194 | * Use {@link #trn} for distinguishing singular from plural text, i.e.,
|
---|
[4394] | 195 | * do not use {@code tr(size == 1 ? "singular" : "plural")} nor
|
---|
| 196 | * {@code size == 1 ? tr("singular") : tr("plural")}
|
---|
[3461] | 197 | *
|
---|
| 198 | * @param text the text to translate.
|
---|
| 199 | * Must be a string literal. (No constants or local vars.)
|
---|
| 200 | * Can be broken over multiple lines.
|
---|
| 201 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
[4394] | 202 | * @param objects the parameters for the string.
|
---|
[7893] | 203 | * Mark occurrences in {@code text} with <code>{0}</code>, <code>{1}</code>, ...
|
---|
[4394] | 204 | * @return the translated string.
|
---|
| 205 | * @see #trn
|
---|
| 206 | * @see #trc
|
---|
| 207 | * @see #trnc
|
---|
[1169] | 208 | */
|
---|
[8374] | 209 | public static String tr(String text, Object... objects) {
|
---|
[4399] | 210 | if (text == null) return null;
|
---|
[2754] | 211 | return MessageFormat.format(gettext(text, null), objects);
|
---|
[1169] | 212 | }
|
---|
[626] | 213 |
|
---|
[3461] | 214 | /**
|
---|
[4394] | 215 | * Translates some text in a context for the current locale.
|
---|
| 216 | * There can be different translations for the same text within different contexts.
|
---|
[3461] | 217 | *
|
---|
| 218 | * @param context string that helps translators to find an appropriate
|
---|
[4394] | 219 | * translation for {@code text}.
|
---|
| 220 | * @param text the text to translate.
|
---|
| 221 | * @return the translated string.
|
---|
| 222 | * @see #tr
|
---|
| 223 | * @see #trn
|
---|
| 224 | * @see #trnc
|
---|
[3461] | 225 | */
|
---|
[8374] | 226 | public static String trc(String context, String text) {
|
---|
[3461] | 227 | if (context == null)
|
---|
[3389] | 228 | return tr(text);
|
---|
| 229 | if (text == null)
|
---|
| 230 | return null;
|
---|
[8510] | 231 | return MessageFormat.format(gettext(text, context), (Object) null);
|
---|
[2179] | 232 | }
|
---|
| 233 |
|
---|
[10748] | 234 | public static String trcLazy(String context, String text) {
|
---|
[4144] | 235 | if (context == null)
|
---|
| 236 | return tr(text);
|
---|
| 237 | if (text == null)
|
---|
| 238 | return null;
|
---|
[10748] | 239 | return MessageFormat.format(gettextLazy(text, context), (Object) null);
|
---|
[4144] | 240 | }
|
---|
| 241 |
|
---|
[3461] | 242 | /**
|
---|
| 243 | * Marks a string for translation (such that a script can harvest
|
---|
| 244 | * the translatable strings from the source files).
|
---|
| 245 | *
|
---|
[7893] | 246 | * For example, <code>
|
---|
[3461] | 247 | * String[] options = new String[] {marktr("up"), marktr("down")};
|
---|
[7893] | 248 | * lbl.setText(tr(options[0]));</code>
|
---|
[4394] | 249 | * @param text the string to be marked for translation.
|
---|
| 250 | * @return {@code text} unmodified.
|
---|
[3461] | 251 | */
|
---|
[8374] | 252 | public static String marktr(String text) {
|
---|
[1169] | 253 | return text;
|
---|
| 254 | }
|
---|
[758] | 255 |
|
---|
[8374] | 256 | public static String marktrc(String context, String text) {
|
---|
[2225] | 257 | return text;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[3461] | 260 | /**
|
---|
[4394] | 261 | * Translates some text for the current locale and distinguishes between
|
---|
| 262 | * {@code singularText} and {@code pluralText} depending on {@code n}.
|
---|
[6830] | 263 | * <br>
|
---|
[4394] | 264 | * For instance, {@code trn("There was an error!", "There were errors!", i)} or
|
---|
[7893] | 265 | * <code>trn("Found {0} error in {1}!", "Found {0} errors in {1}!", i, Integer.toString(i), url)</code>.
|
---|
[4489] | 266 | *
|
---|
[4394] | 267 | * @param singularText the singular text to translate.
|
---|
| 268 | * Must be a string literal. (No constants or local vars.)
|
---|
| 269 | * Can be broken over multiple lines.
|
---|
| 270 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
| 271 | * @param pluralText the plural text to translate.
|
---|
| 272 | * Must be a string literal. (No constants or local vars.)
|
---|
| 273 | * Can be broken over multiple lines.
|
---|
| 274 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
| 275 | * @param n a number to determine whether {@code singularText} or {@code pluralText} is used.
|
---|
| 276 | * @param objects the parameters for the string.
|
---|
[7893] | 277 | * Mark occurrences in {@code singularText} and {@code pluralText} with <code>{0}</code>, <code>{1}</code>, ...
|
---|
[4394] | 278 | * @return the translated string.
|
---|
| 279 | * @see #tr
|
---|
| 280 | * @see #trc
|
---|
| 281 | * @see #trnc
|
---|
[3461] | 282 | */
|
---|
[8374] | 283 | public static String trn(String singularText, String pluralText, long n, Object... objects) {
|
---|
[4394] | 284 | return MessageFormat.format(gettextn(singularText, pluralText, null, n), objects);
|
---|
[1169] | 285 | }
|
---|
[626] | 286 |
|
---|
[3461] | 287 | /**
|
---|
[4394] | 288 | * Translates some text in a context for the current locale and distinguishes between
|
---|
| 289 | * {@code singularText} and {@code pluralText} depending on {@code n}.
|
---|
| 290 | * There can be different translations for the same text within different contexts.
|
---|
| 291 | *
|
---|
| 292 | * @param context string that helps translators to find an appropriate
|
---|
| 293 | * translation for {@code text}.
|
---|
| 294 | * @param singularText the singular text to translate.
|
---|
| 295 | * Must be a string literal. (No constants or local vars.)
|
---|
| 296 | * Can be broken over multiple lines.
|
---|
| 297 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
| 298 | * @param pluralText the plural text to translate.
|
---|
| 299 | * Must be a string literal. (No constants or local vars.)
|
---|
| 300 | * Can be broken over multiple lines.
|
---|
| 301 | * An apostrophe ' must be quoted by another apostrophe.
|
---|
| 302 | * @param n a number to determine whether {@code singularText} or {@code pluralText} is used.
|
---|
| 303 | * @param objects the parameters for the string.
|
---|
[7893] | 304 | * Mark occurrences in {@code singularText} and {@code pluralText} with <code>{0}</code>, <code>{1}</code>, ...
|
---|
[4394] | 305 | * @return the translated string.
|
---|
| 306 | * @see #tr
|
---|
| 307 | * @see #trc
|
---|
| 308 | * @see #trn
|
---|
[3461] | 309 | */
|
---|
[8374] | 310 | public static String trnc(String context, String singularText, String pluralText, long n, Object... objects) {
|
---|
[4394] | 311 | return MessageFormat.format(gettextn(singularText, pluralText, context, n), objects);
|
---|
[1169] | 312 | }
|
---|
[1065] | 313 |
|
---|
[8374] | 314 | private static String gettext(String text, String ctx, boolean lazy) {
|
---|
[2174] | 315 | int i;
|
---|
[8510] | 316 | if (ctx == null && text.startsWith("_:") && (i = text.indexOf('\n')) >= 0) {
|
---|
| 317 | ctx = text.substring(2, i-1);
|
---|
[2754] | 318 | text = text.substring(i+1);
|
---|
| 319 | }
|
---|
[8510] | 320 | if (strings != null) {
|
---|
[8846] | 321 | String trans = strings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
|
---|
[8510] | 322 | if (trans != null)
|
---|
[2754] | 323 | return trans;
|
---|
| 324 | }
|
---|
[8510] | 325 | if (pstrings != null) {
|
---|
[6796] | 326 | i = pluralEval(1);
|
---|
[8846] | 327 | String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
|
---|
[8510] | 328 | if (trans != null && trans.length > i)
|
---|
[6796] | 329 | return trans[i];
|
---|
[3037] | 330 | }
|
---|
[4394] | 331 | return lazy ? gettext(text, null) : text;
|
---|
[2174] | 332 | }
|
---|
| 333 |
|
---|
[8374] | 334 | private static String gettext(String text, String ctx) {
|
---|
[4394] | 335 | return gettext(text, ctx, false);
|
---|
| 336 | }
|
---|
| 337 |
|
---|
[4144] | 338 | /* try without context, when context try fails */
|
---|
[10748] | 339 | private static String gettextLazy(String text, String ctx) {
|
---|
[4394] | 340 | return gettext(text, ctx, true);
|
---|
[4144] | 341 | }
|
---|
| 342 |
|
---|
[8374] | 343 | private static String gettextn(String text, String plural, String ctx, long num) {
|
---|
[2754] | 344 | int i;
|
---|
[8510] | 345 | if (ctx == null && text.startsWith("_:") && (i = text.indexOf('\n')) >= 0) {
|
---|
| 346 | ctx = text.substring(2, i-1);
|
---|
[2754] | 347 | text = text.substring(i+1);
|
---|
| 348 | }
|
---|
[8510] | 349 | if (pstrings != null) {
|
---|
[2754] | 350 | i = pluralEval(num);
|
---|
[8846] | 351 | String[] trans = pstrings.get(ctx == null ? text : "_:"+ctx+'\n'+text);
|
---|
[8510] | 352 | if (trans != null && trans.length > i)
|
---|
[2754] | 353 | return trans[i];
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | return num == 1 ? text : plural;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[4477] | 359 | public static String escape(String msg) {
|
---|
| 360 | if (msg == null) return null;
|
---|
| 361 | return msg.replace("\'", "\'\'").replace("{", "\'{\'").replace("}", "\'}\'");
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[4719] | 364 | private static URL getTranslationFile(String lang) {
|
---|
[8846] | 365 | return Main.class.getResource("/data/"+lang.replace('@', '-')+".lang");
|
---|
[4719] | 366 | }
|
---|
| 367 |
|
---|
[1169] | 368 | /**
|
---|
| 369 | * Get a list of all available JOSM Translations.
|
---|
| 370 | * @return an array of locale objects.
|
---|
| 371 | */
|
---|
[8374] | 372 | public static Locale[] getAvailableTranslations() {
|
---|
[7005] | 373 | Collection<Locale> v = new ArrayList<>(languages.size());
|
---|
[8510] | 374 | if (getTranslationFile("en") != null) {
|
---|
[2754] | 375 | for (String loc : languages.keySet()) {
|
---|
[8510] | 376 | if (getTranslationFile(loc) != null) {
|
---|
[4211] | 377 | v.add(LanguageInfo.getLocale(loc));
|
---|
[2754] | 378 | }
|
---|
[1169] | 379 | }
|
---|
| 380 | }
|
---|
[2754] | 381 | v.add(Locale.ENGLISH);
|
---|
| 382 | Locale[] l = new Locale[v.size()];
|
---|
[1169] | 383 | l = v.toArray(l);
|
---|
[10647] | 384 | Arrays.sort(l, Comparator.comparing(Locale::toString));
|
---|
[1169] | 385 | return l;
|
---|
| 386 | }
|
---|
[1802] | 387 |
|
---|
[7893] | 388 | /**
|
---|
| 389 | * Determines if a language exists for the given code.
|
---|
| 390 | * @param code The language code
|
---|
| 391 | * @return {@code true} if a language exists, {@code false} otherwise
|
---|
| 392 | */
|
---|
| 393 | public static boolean hasCode(String code) {
|
---|
[4212] | 394 | return languages.containsKey(code);
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[7893] | 397 | /**
|
---|
| 398 | * I18n initialization.
|
---|
| 399 | */
|
---|
| 400 | public static void init() {
|
---|
[7894] | 401 | // Enable CLDR locale provider on Java 8 to get additional languages, such as Khmer.
|
---|
| 402 | // http://docs.oracle.com/javase/8/docs/technotes/guides/intl/enhancements.8.html#cldr
|
---|
[7897] | 403 | // FIXME: This can be removed after we switch to a minimal version of Java that enables CLDR by default
|
---|
[9599] | 404 | // or includes all languages we need in the JRE. See http://openjdk.java.net/jeps/252 for Java 9
|
---|
[10983] | 405 | System.setProperty("java.locale.providers", "JRE,CLDR"); // Don't call Utils.updateSystemProperty to avoid spurious log at startup
|
---|
[7894] | 406 |
|
---|
[4670] | 407 | //languages.put("ar", PluralMode.MODE_AR);
|
---|
[7898] | 408 | languages.put("ast", PluralMode.MODE_NOTONE);
|
---|
[2754] | 409 | languages.put("bg", PluralMode.MODE_NOTONE);
|
---|
[8414] | 410 | languages.put("be", PluralMode.MODE_RU);
|
---|
[5245] | 411 | languages.put("ca", PluralMode.MODE_NOTONE);
|
---|
[8207] | 412 | languages.put("ca@valencia", PluralMode.MODE_NOTONE);
|
---|
[2754] | 413 | languages.put("cs", PluralMode.MODE_CS);
|
---|
| 414 | languages.put("da", PluralMode.MODE_NOTONE);
|
---|
| 415 | languages.put("de", PluralMode.MODE_NOTONE);
|
---|
| 416 | languages.put("el", PluralMode.MODE_NOTONE);
|
---|
[3161] | 417 | languages.put("en_AU", PluralMode.MODE_NOTONE);
|
---|
[2754] | 418 | languages.put("en_GB", PluralMode.MODE_NOTONE);
|
---|
| 419 | languages.put("es", PluralMode.MODE_NOTONE);
|
---|
| 420 | languages.put("et", PluralMode.MODE_NOTONE);
|
---|
[6984] | 421 | //languages.put("eu", PluralMode.MODE_NOTONE);
|
---|
[2754] | 422 | languages.put("fi", PluralMode.MODE_NOTONE);
|
---|
| 423 | languages.put("fr", PluralMode.MODE_GREATERONE);
|
---|
| 424 | languages.put("gl", PluralMode.MODE_NOTONE);
|
---|
[4670] | 425 | //languages.put("he", PluralMode.MODE_NOTONE);
|
---|
[5179] | 426 | languages.put("hu", PluralMode.MODE_NOTONE);
|
---|
[4267] | 427 | languages.put("id", PluralMode.MODE_NONE);
|
---|
[4670] | 428 | //languages.put("is", PluralMode.MODE_NOTONE);
|
---|
[2754] | 429 | languages.put("it", PluralMode.MODE_NOTONE);
|
---|
| 430 | languages.put("ja", PluralMode.MODE_NONE);
|
---|
[7898] | 431 | // fully supported only with Java 8 and later (needs CLDR)
|
---|
| 432 | languages.put("km", PluralMode.MODE_NONE);
|
---|
[8160] | 433 | languages.put("lt", PluralMode.MODE_LT);
|
---|
[8972] | 434 | languages.put("nb", PluralMode.MODE_NOTONE);
|
---|
[2754] | 435 | languages.put("nl", PluralMode.MODE_NOTONE);
|
---|
| 436 | languages.put("pl", PluralMode.MODE_PL);
|
---|
[5244] | 437 | languages.put("pt", PluralMode.MODE_NOTONE);
|
---|
[2754] | 438 | languages.put("pt_BR", PluralMode.MODE_GREATERONE);
|
---|
[3957] | 439 | //languages.put("ro", PluralMode.MODE_RO);
|
---|
[2754] | 440 | languages.put("ru", PluralMode.MODE_RU);
|
---|
| 441 | languages.put("sk", PluralMode.MODE_SK);
|
---|
[3957] | 442 | //languages.put("sl", PluralMode.MODE_SL);
|
---|
[2754] | 443 | languages.put("sv", PluralMode.MODE_NOTONE);
|
---|
[6984] | 444 | //languages.put("tr", PluralMode.MODE_NONE);
|
---|
[3259] | 445 | languages.put("uk", PluralMode.MODE_RU);
|
---|
[8352] | 446 | languages.put("vi", PluralMode.MODE_NONE);
|
---|
[3415] | 447 | languages.put("zh_CN", PluralMode.MODE_NONE);
|
---|
[2754] | 448 | languages.put("zh_TW", PluralMode.MODE_NONE);
|
---|
| 449 |
|
---|
[1802] | 450 | /* try initial language settings, may be changed later again */
|
---|
[8510] | 451 | if (!load(LanguageInfo.getJOSMLocaleCode())) {
|
---|
[2754] | 452 | Locale.setDefault(Locale.ENGLISH);
|
---|
[2785] | 453 | }
|
---|
[1802] | 454 | }
|
---|
| 455 |
|
---|
[7012] | 456 | public static void addTexts(File source) {
|
---|
| 457 | if ("en".equals(loadedCode))
|
---|
[4171] | 458 | return;
|
---|
[8373] | 459 | final String enfile = "data/en.lang";
|
---|
| 460 | final String langfile = "data/"+loadedCode+".lang";
|
---|
[7033] | 461 | try (
|
---|
| 462 | FileInputStream fis = new FileInputStream(source);
|
---|
| 463 | JarInputStream jar = new JarInputStream(fis)
|
---|
| 464 | ) {
|
---|
[4159] | 465 | ZipEntry e;
|
---|
| 466 | boolean found = false;
|
---|
[7033] | 467 | while (!found && (e = jar.getNextEntry()) != null) {
|
---|
[4159] | 468 | String name = e.getName();
|
---|
[8373] | 469 | if (enfile.equals(name))
|
---|
[4159] | 470 | found = true;
|
---|
| 471 | }
|
---|
[7033] | 472 | if (found) {
|
---|
| 473 | try (
|
---|
| 474 | FileInputStream fisTrans = new FileInputStream(source);
|
---|
| 475 | JarInputStream jarTrans = new JarInputStream(fisTrans)
|
---|
| 476 | ) {
|
---|
| 477 | found = false;
|
---|
[8510] | 478 | while (!found && (e = jarTrans.getNextEntry()) != null) {
|
---|
[7033] | 479 | String name = e.getName();
|
---|
| 480 | if (name.equals(langfile))
|
---|
| 481 | found = true;
|
---|
| 482 | }
|
---|
| 483 | if (found)
|
---|
| 484 | load(jar, jarTrans, true);
|
---|
[4159] | 485 | }
|
---|
| 486 | }
|
---|
[7033] | 487 | } catch (IOException e) {
|
---|
[5874] | 488 | // Ignore
|
---|
[10626] | 489 | Main.trace(e);
|
---|
[4159] | 490 | }
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[7012] | 493 | private static boolean load(String l) {
|
---|
| 494 | if ("en".equals(l) || "en_US".equals(l)) {
|
---|
[2785] | 495 | strings = null;
|
---|
| 496 | pstrings = null;
|
---|
[4171] | 497 | loadedCode = "en";
|
---|
[2785] | 498 | pluralMode = PluralMode.MODE_NOTONE;
|
---|
| 499 | return true;
|
---|
[2754] | 500 | }
|
---|
[4719] | 501 | URL en = getTranslationFile("en");
|
---|
[7012] | 502 | if (en == null)
|
---|
[2754] | 503 | return false;
|
---|
[4719] | 504 | URL tr = getTranslationFile(l);
|
---|
[7012] | 505 | if (tr == null || !languages.containsKey(l)) {
|
---|
[8283] | 506 | return false;
|
---|
[2754] | 507 | }
|
---|
[7033] | 508 | try (
|
---|
| 509 | InputStream enStream = en.openStream();
|
---|
| 510 | InputStream trStream = tr.openStream()
|
---|
| 511 | ) {
|
---|
[5839] | 512 | if (load(enStream, trStream, false)) {
|
---|
[4159] | 513 | pluralMode = languages.get(l);
|
---|
| 514 | loadedCode = l;
|
---|
| 515 | return true;
|
---|
| 516 | }
|
---|
[7033] | 517 | } catch (IOException e) {
|
---|
[5839] | 518 | // Ignore exception
|
---|
[10626] | 519 | Main.trace(e);
|
---|
[4159] | 520 | }
|
---|
| 521 | return false;
|
---|
| 522 | }
|
---|
[2754] | 523 |
|
---|
[6316] | 524 | private static boolean load(InputStream en, InputStream tr, boolean add) {
|
---|
| 525 | Map<String, String> s;
|
---|
| 526 | Map<String, String[]> p;
|
---|
| 527 | if (add) {
|
---|
[4159] | 528 | s = strings;
|
---|
| 529 | p = pstrings;
|
---|
[6316] | 530 | } else {
|
---|
[7005] | 531 | s = new HashMap<>();
|
---|
| 532 | p = new HashMap<>();
|
---|
[4159] | 533 | }
|
---|
[2754] | 534 | /* file format:
|
---|
[4501] | 535 | Files are always a group. English file and translated file must provide identical datasets.
|
---|
| 536 |
|
---|
[2754] | 537 | for all single strings:
|
---|
| 538 | {
|
---|
| 539 | unsigned short (2 byte) stringlength
|
---|
[4501] | 540 | - length 0 indicates missing translation
|
---|
| 541 | - length 0xFFFE indicates translation equal to original, but otherwise is equal to length 0
|
---|
[2754] | 542 | string
|
---|
| 543 | }
|
---|
| 544 | unsigned short (2 byte) 0xFFFF (marks end of single strings)
|
---|
| 545 | for all multi strings:
|
---|
| 546 | {
|
---|
| 547 | unsigned char (1 byte) stringcount
|
---|
[4549] | 548 | - count 0 indicates missing translations
|
---|
| 549 | - count 0xFE indicates translations equal to original, but otherwise is equal to length 0
|
---|
[2754] | 550 | for stringcount
|
---|
| 551 | unsigned short (2 byte) stringlength
|
---|
| 552 | string
|
---|
| 553 | }
|
---|
[2785] | 554 | */
|
---|
[7893] | 555 | try {
|
---|
[4159] | 556 | InputStream ens = new BufferedInputStream(en);
|
---|
| 557 | InputStream trs = new BufferedInputStream(tr);
|
---|
[2754] | 558 | byte[] enlen = new byte[2];
|
---|
| 559 | byte[] trlen = new byte[2];
|
---|
| 560 | boolean multimode = false;
|
---|
| 561 | byte[] str = new byte[4096];
|
---|
[8510] | 562 | for (;;) {
|
---|
| 563 | if (multimode) {
|
---|
[2754] | 564 | int ennum = ens.read();
|
---|
| 565 | int trnum = trs.read();
|
---|
[8510] | 566 | if (trnum == 0xFE) /* marks identical string, handle equally to non-translated */
|
---|
[4549] | 567 | trnum = 0;
|
---|
[8510] | 568 | if ((ennum == -1 && trnum != -1) || (ennum != -1 && trnum == -1)) /* files do not match */
|
---|
[2754] | 569 | return false;
|
---|
[8510] | 570 | if (ennum == -1) {
|
---|
[2754] | 571 | break;
|
---|
[2785] | 572 | }
|
---|
[2754] | 573 | String[] enstrings = new String[ennum];
|
---|
[8510] | 574 | for (int i = 0; i < ennum; ++i) {
|
---|
[2754] | 575 | int val = ens.read(enlen);
|
---|
[8510] | 576 | if (val != 2) /* file corrupt */
|
---|
[2754] | 577 | return false;
|
---|
[8510] | 578 | val = (enlen[0] < 0 ? 256+enlen[0] : enlen[0])*256+(enlen[1] < 0 ? 256+enlen[1] : enlen[1]);
|
---|
| 579 | if (val > str.length) {
|
---|
[2754] | 580 | str = new byte[val];
|
---|
[2785] | 581 | }
|
---|
[2754] | 582 | int rval = ens.read(str, 0, val);
|
---|
[8510] | 583 | if (rval != val) /* file corrupt */
|
---|
[2754] | 584 | return false;
|
---|
[7082] | 585 | enstrings[i] = new String(str, 0, val, StandardCharsets.UTF_8);
|
---|
[2754] | 586 | }
|
---|
[9062] | 587 | String[] trstrings = new String[trnum];
|
---|
[8510] | 588 | for (int i = 0; i < trnum; ++i) {
|
---|
[2754] | 589 | int val = trs.read(trlen);
|
---|
[8510] | 590 | if (val != 2) /* file corrupt */
|
---|
[2754] | 591 | return false;
|
---|
[8510] | 592 | val = (trlen[0] < 0 ? 256+trlen[0] : trlen[0])*256+(trlen[1] < 0 ? 256+trlen[1] : trlen[1]);
|
---|
| 593 | if (val > str.length) {
|
---|
[2754] | 594 | str = new byte[val];
|
---|
[2785] | 595 | }
|
---|
[2754] | 596 | int rval = trs.read(str, 0, val);
|
---|
[8510] | 597 | if (rval != val) /* file corrupt */
|
---|
[2754] | 598 | return false;
|
---|
[7082] | 599 | trstrings[i] = new String(str, 0, val, StandardCharsets.UTF_8);
|
---|
[2754] | 600 | }
|
---|
[8510] | 601 | if (trnum > 0 && !p.containsKey(enstrings[0])) {
|
---|
[2754] | 602 | p.put(enstrings[0], trstrings);
|
---|
[2785] | 603 | }
|
---|
[7893] | 604 | } else {
|
---|
[2754] | 605 | int enval = ens.read(enlen);
|
---|
| 606 | int trval = trs.read(trlen);
|
---|
[8510] | 607 | if (enval != trval) /* files do not match */
|
---|
[2754] | 608 | return false;
|
---|
[8510] | 609 | if (enval == -1) {
|
---|
[2754] | 610 | break;
|
---|
[2785] | 611 | }
|
---|
[8510] | 612 | if (enval != 2) /* files corrupt */
|
---|
[2754] | 613 | return false;
|
---|
[8510] | 614 | enval = (enlen[0] < 0 ? 256+enlen[0] : enlen[0])*256+(enlen[1] < 0 ? 256+enlen[1] : enlen[1]);
|
---|
| 615 | trval = (trlen[0] < 0 ? 256+trlen[0] : trlen[0])*256+(trlen[1] < 0 ? 256+trlen[1] : trlen[1]);
|
---|
| 616 | if (trval == 0xFFFE) /* marks identical string, handle equally to non-translated */
|
---|
[4376] | 617 | trval = 0;
|
---|
[8510] | 618 | if (enval == 0xFFFF) {
|
---|
[2754] | 619 | multimode = true;
|
---|
[8510] | 620 | if (trval != 0xFFFF) /* files do not match */
|
---|
[2754] | 621 | return false;
|
---|
[7082] | 622 | } else {
|
---|
| 623 | if (enval > str.length) {
|
---|
[2754] | 624 | str = new byte[enval];
|
---|
[2785] | 625 | }
|
---|
[7082] | 626 | if (trval > str.length) {
|
---|
[2754] | 627 | str = new byte[trval];
|
---|
[2785] | 628 | }
|
---|
[2754] | 629 | int val = ens.read(str, 0, enval);
|
---|
[8510] | 630 | if (val != enval) /* file corrupt */
|
---|
[2754] | 631 | return false;
|
---|
[7082] | 632 | String enstr = new String(str, 0, enval, StandardCharsets.UTF_8);
|
---|
| 633 | if (trval != 0) {
|
---|
[2754] | 634 | val = trs.read(str, 0, trval);
|
---|
[8510] | 635 | if (val != trval) /* file corrupt */
|
---|
[2754] | 636 | return false;
|
---|
[7082] | 637 | String trstr = new String(str, 0, trval, StandardCharsets.UTF_8);
|
---|
[8510] | 638 | if (!s.containsKey(enstr))
|
---|
[4159] | 639 | s.put(enstr, trstr);
|
---|
[2754] | 640 | }
|
---|
| 641 | }
|
---|
| 642 | }
|
---|
| 643 | }
|
---|
[7893] | 644 | } catch (IOException e) {
|
---|
[10627] | 645 | Main.trace(e);
|
---|
[2754] | 646 | return false;
|
---|
| 647 | }
|
---|
[7082] | 648 | if (!s.isEmpty()) {
|
---|
[2754] | 649 | strings = s;
|
---|
| 650 | pstrings = p;
|
---|
| 651 | return true;
|
---|
| 652 | }
|
---|
| 653 | return false;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
[2358] | 656 | /**
|
---|
[5266] | 657 | * Sets the default locale (see {@link Locale#setDefault(Locale)} to the local
|
---|
[2358] | 658 | * given by <code>localName</code>.
|
---|
[2512] | 659 | *
|
---|
[3418] | 660 | * Ignored if localeName is null. If the locale with name <code>localName</code>
|
---|
[2358] | 661 | * isn't found the default local is set to <tt>en</tt> (english).
|
---|
[2512] | 662 | *
|
---|
[2358] | 663 | * @param localeName the locale name. Ignored if null.
|
---|
| 664 | */
|
---|
[8510] | 665 | public static void set(String localeName) {
|
---|
[1802] | 666 | if (localeName != null) {
|
---|
[4211] | 667 | Locale l = LanguageInfo.getLocale(localeName);
|
---|
| 668 | if (load(LanguageInfo.getJOSMLocaleCode(l))) {
|
---|
[1802] | 669 | Locale.setDefault(l);
|
---|
[2754] | 670 | } else {
|
---|
[7012] | 671 | if (!"en".equals(l.getLanguage())) {
|
---|
[6248] | 672 | Main.info(tr("Unable to find translation for the locale {0}. Reverting to {1}.",
|
---|
[8241] | 673 | LanguageInfo.getDisplayName(l), LanguageInfo.getDisplayName(Locale.getDefault())));
|
---|
[1802] | 674 | } else {
|
---|
[2754] | 675 | strings = null;
|
---|
| 676 | pstrings = null;
|
---|
[1802] | 677 | }
|
---|
| 678 | }
|
---|
| 679 | }
|
---|
| 680 | }
|
---|
[2754] | 681 |
|
---|
[3340] | 682 | /**
|
---|
| 683 | * Localizations for file chooser dialog.
|
---|
[3415] | 684 | * For some locales (e.g. de, fr) translations are provided
|
---|
| 685 | * by Java, but not for others (e.g. ru, uk).
|
---|
[3340] | 686 | */
|
---|
[3433] | 687 | public static void translateJavaInternalMessages() {
|
---|
[3340] | 688 | Locale l = Locale.getDefault();
|
---|
| 689 |
|
---|
[7578] | 690 | AbstractFileChooser.setDefaultLocale(l);
|
---|
[3340] | 691 | JFileChooser.setDefaultLocale(l);
|
---|
| 692 | JColorChooser.setDefaultLocale(l);
|
---|
[3433] | 693 | for (String key : javaInternalMessageKeys) {
|
---|
[3340] | 694 | String us = UIManager.getString(key, Locale.US);
|
---|
| 695 | String loc = UIManager.getString(key, l);
|
---|
| 696 | // only provide custom translation if it is not already localized by Java
|
---|
| 697 | if (us != null && us.equals(loc)) {
|
---|
| 698 | UIManager.put(key, tr(us));
|
---|
| 699 | }
|
---|
| 700 | }
|
---|
| 701 | }
|
---|
| 702 |
|
---|
[7893] | 703 | private static int pluralEval(long n) {
|
---|
| 704 | switch(pluralMode) {
|
---|
[3353] | 705 | case MODE_NOTONE: /* bg, da, de, el, en, en_GB, es, et, eu, fi, gl, is, it, iw_IL, nb, nl, sv */
|
---|
[8345] | 706 | return (n != 1) ? 1 : 0;
|
---|
[8352] | 707 | case MODE_NONE: /* id, vi, ja, km, tr, zh_CN, zh_TW */
|
---|
[2754] | 708 | return 0;
|
---|
| 709 | case MODE_GREATERONE: /* fr, pt_BR */
|
---|
[8345] | 710 | return (n > 1) ? 1 : 0;
|
---|
[2754] | 711 | case MODE_CS:
|
---|
[8345] | 712 | return (n == 1) ? 0 : (((n >= 2) && (n <= 4)) ? 1 : 2);
|
---|
[4670] | 713 | //case MODE_AR:
|
---|
| 714 | // return ((n == 0) ? 0 : ((n == 1) ? 1 : ((n == 2) ? 2 : ((((n % 100) >= 3)
|
---|
| 715 | // && ((n % 100) <= 10)) ? 3 : ((((n % 100) >= 11) && ((n % 100) <= 99)) ? 4 : 5)))));
|
---|
[2754] | 716 | case MODE_PL:
|
---|
[8345] | 717 | return (n == 1) ? 0 : (((((n % 10) >= 2) && ((n % 10) <= 4))
|
---|
| 718 | && (((n % 100) < 10) || ((n % 100) >= 20))) ? 1 : 2);
|
---|
[3957] | 719 | //case MODE_RO:
|
---|
| 720 | // return ((n == 1) ? 0 : ((((n % 100) > 19) || (((n % 100) == 0) && (n != 0))) ? 2 : 1));
|
---|
[8160] | 721 | case MODE_LT:
|
---|
[8345] | 722 | return ((n % 10) == 1) && ((n % 100) != 11) ? 0 : (((n % 10) >= 2)
|
---|
| 723 | && (((n % 100) < 10) || ((n % 100) >= 20)) ? 1 : 2);
|
---|
[2754] | 724 | case MODE_RU:
|
---|
[8345] | 725 | return (((n % 10) == 1) && ((n % 100) != 11)) ? 0 : (((((n % 10) >= 2)
|
---|
| 726 | && ((n % 10) <= 4)) && (((n % 100) < 10) || ((n % 100) >= 20))) ? 1 : 2);
|
---|
[2754] | 727 | case MODE_SK:
|
---|
[8345] | 728 | return (n == 1) ? 1 : (((n >= 2) && (n <= 4)) ? 2 : 0);
|
---|
[3957] | 729 | //case MODE_SL:
|
---|
| 730 | // return (((n % 100) == 1) ? 1 : (((n % 100) == 2) ? 2 : ((((n % 100) == 3)
|
---|
| 731 | // || ((n % 100) == 4)) ? 3 : 0)));
|
---|
[2754] | 732 | }
|
---|
| 733 | return 0;
|
---|
| 734 | }
|
---|
[4489] | 735 |
|
---|
| 736 | public static TranslationAdapter getTranslationAdapter() {
|
---|
[10625] | 737 | return I18n::tr;
|
---|
[4489] | 738 | }
|
---|
[8282] | 739 |
|
---|
| 740 | /**
|
---|
[8283] | 741 | * Setup special font for Khmer script, as the default Java fonts do not display these characters.
|
---|
[8282] | 742 | *
|
---|
[8283] | 743 | * @since 8282
|
---|
[8282] | 744 | */
|
---|
| 745 | public static void setupLanguageFonts() {
|
---|
| 746 | // Use special font for Khmer script, as the default Java font do not display these characters
|
---|
| 747 | if ("km".equals(LanguageInfo.getJOSMLocaleCode())) {
|
---|
| 748 | Collection<String> fonts = Arrays.asList(
|
---|
| 749 | GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
|
---|
| 750 | for (String f : new String[]{"Khmer UI", "DaunPenh", "MoolBoran"}) {
|
---|
| 751 | if (fonts.contains(f)) {
|
---|
| 752 | GuiHelper.setUIFont(f);
|
---|
| 753 | break;
|
---|
| 754 | }
|
---|
| 755 | }
|
---|
| 756 | }
|
---|
| 757 | }
|
---|
[626] | 758 | }
|
---|