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