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

Last change on this file since 1814 was 1802, checked in by stoecker, 15 years ago

cleanup license and contributions a bit

  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.text.MessageFormat;
5import java.util.Arrays;
6import java.util.Comparator;
7import java.util.Locale;
8import java.util.LinkedList;
9import java.util.MissingResourceException;
10import java.util.Vector;
11import org.openstreetmap.josm.gui.MainApplication;
12import org.xnap.commons.i18n.I18nFactory;
13
14/**
15 * Internationalisation support.
16 *
17 * @author Immanuel.Scholz
18 */
19public class I18n {
20
21 /* Base name for translation data. Used for detecting available translations */
22 private static final String TR_BASE = "org.openstreetmap.josm.i18n.Translation_";
23
24 /**
25 * Set by MainApplication. Changes here later will probably mess up everything, because
26 * many strings are already loaded.
27 */
28 public static org.xnap.commons.i18n.I18n i18n;
29
30 public static final String tr(String text, Object... objects) {
31 if (i18n == null)
32 return MessageFormat.format(text, objects);
33 return i18n.tr(text, objects);
34 }
35
36 public static final String tr(String text) {
37 if (i18n == null)
38 return text;
39 return i18n.tr(text);
40 }
41
42 public static final String marktr(String text) {
43 return text;
44 }
45
46 public static final String trn(String text, String pluralText, long n, Object... objects) {
47 if (i18n == null)
48 return n == 1 ? tr(text, objects) : tr(pluralText, objects);
49 return i18n.trn(text, pluralText, n, objects);
50 }
51
52 public static final String trn(String text, String pluralText, long n) {
53 if (i18n == null)
54 return n == 1 ? tr(text) : tr(pluralText);
55 return i18n.trn(text, pluralText, n);
56 }
57
58 /**
59 * Get a list of all available JOSM Translations.
60 * @return an array of locale objects.
61 */
62 public static final Locale[] getAvailableTranslations() {
63 Vector<Locale> v = new Vector<Locale>();
64 LinkedList<String>str = new LinkedList<String>();
65 Locale[] l = Locale.getAvailableLocales();
66 for (int i = 0; i < l.length; i++) {
67 String loc = l[i].toString();
68 String cn = TR_BASE + loc;
69 try {
70 Class.forName(cn);
71 v.add(l[i]);
72 str.add(loc);
73 } catch (ClassNotFoundException e) {
74 }
75 }
76 /* hmm, don't know why this is necessary */
77 try {
78 if(!str.contains("nb"))
79 v.add(new Locale("nb"));
80 } catch (Exception e) {}
81 try {
82 if(!str.contains("gl"))
83 v.add(new Locale("gl"));
84 } catch (Exception e) {}
85 l = new Locale[v.size()];
86 l = v.toArray(l);
87 Arrays.sort(l, new Comparator<Locale>() {
88 public int compare(Locale o1, Locale o2) {
89 return o1.toString().compareTo(o2.toString());
90 }
91 });
92 return l;
93 }
94
95 public static void init()
96 {
97 /* try initial language settings, may be changed later again */
98 try { i18n = I18nFactory.getI18n(MainApplication.class); }
99 catch (MissingResourceException ex) { Locale.setDefault(Locale.ENGLISH);}
100 }
101
102 public static void set(String localeName)
103 {
104 if (localeName != null) {
105 Locale l;
106 Locale d = Locale.getDefault();
107 if (localeName.equals("he")) localeName = "iw_IL";
108 int i = localeName.indexOf('_');
109 if (i > 0) {
110 l = new Locale(localeName.substring(0, i), localeName.substring(i + 1));
111 } else {
112 l = new Locale(localeName);
113 }
114 try {
115 Locale.setDefault(l);
116 i18n = I18nFactory.getI18n(MainApplication.class);
117 } catch (MissingResourceException ex) {
118 if (!l.getLanguage().equals("en")) {
119 System.out.println(tr("Unable to find translation for the locale {0}. Reverting to {1}.",
120 l.getDisplayName(), d.getDisplayName()));
121 Locale.setDefault(d);
122 } else {
123 i18n = null;
124 }
125 }
126 }
127 }
128}
Note: See TracBrowser for help on using the repository browser.