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