source: josm/trunk/src/org/openstreetmap/josm/tools/I18n.java@ 13502

Last change on this file since 13502 was 13502, checked in by stoecker, 6 years ago

see #11392 - first try to add I18n for external Validators (style, presets still missing)

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