source: josm/trunk/src/org/openstreetmap/josm/tools/LanguageInfo.java@ 8373

Last change on this file since 8373 was 8284, checked in by stoecker, 9 years ago

typo

  • Property svn:eol-style set to native
File size: 8.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.trc;
5
6import java.util.Collection;
7import java.util.LinkedList;
8import java.util.Locale;
9
10public final class LanguageInfo {
11
12 private LanguageInfo() {
13 // Hide default constructor for utils classes
14 }
15
16 /**
17 * Type of the locale to use
18 * @since 5915
19 */
20 public enum LocaleType {
21 /** The current default language */
22 DEFAULT,
23 /** The current default language, but not english */
24 DEFAULTNOTENGLISH,
25 /** The base language (i.e. pt for pt_BR) */
26 BASELANGUAGE,
27 /** The standard english texts */
28 ENGLISH
29 }
30
31 /**
32 * Replies the wiki language prefix for the given locale. The wiki language
33 * prefix has the form 'Xy:' where 'Xy' is a ISO 639 language code in title
34 * case (or Xy_AB: for sub languages).
35 *
36 * @param type the type
37 * @return the wiki language prefix or {@code null} for {@link LocaleType#BASELANGUAGE}, when
38 * base language is identical to default or english
39 * @since 5915
40 */
41 public static String getWikiLanguagePrefix(LocaleType type) {
42 if(type == LocaleType.ENGLISH)
43 return "";
44
45 String code = getJOSMLocaleCode();
46 if(type == LocaleType.BASELANGUAGE) {
47 if(code.matches("[^_]+_[^_]+")) {
48 code = code.substring(0,2);
49 if ("en".equals(code))
50 return null;
51 } else {
52 return null;
53 }
54 } else if(type == LocaleType.DEFAULTNOTENGLISH && "en".equals(code)) {
55 return null;
56 } else if(code.matches(".+@.+")) {
57 return code.substring(0,1).toUpperCase() + code.substring(1,2)
58 + "-" + code.substring(3,4).toUpperCase() + code.substring(4) + ":";
59 }
60 return code.substring(0,1).toUpperCase() + code.substring(1) + ":";
61 }
62
63 /**
64 * Replies the wiki language prefix for the current locale.
65 *
66 * @return the wiki language prefix
67 * @see Locale#getDefault()
68 * @see #getWikiLanguagePrefix(LocaleType)
69 */
70 public static String getWikiLanguagePrefix() {
71 return getWikiLanguagePrefix(LocaleType.DEFAULT);
72 }
73
74 /**
75 * Replies the JOSM locale code for the default locale.
76 *
77 * @return the JOSM locale code for the default locale
78 * @see #getJOSMLocaleCode(Locale)
79 */
80 public static String getJOSMLocaleCode() {
81 return getJOSMLocaleCode(Locale.getDefault());
82 }
83
84 /**
85 * Replies the locale code used by JOSM for a given locale.
86 *
87 * In most cases JOSM uses the 2-character ISO 639 language code ({@link Locale#getLanguage()}
88 * to identify the locale of a localized resource, but in some cases it may use the
89 * programmatic name for locales, as replied by {@link Locale#toString()}.
90 *
91 * For unknown country codes and variants this function already does fallback to
92 * internally known translations.
93 *
94 * @param locale the locale. Replies "en" if null.
95 * @return the JOSM code for the given locale
96 */
97 public static String getJOSMLocaleCode(Locale locale) {
98 if (locale == null) return "en";
99 for(String full : getLanguageCodes(locale)) {
100 if ("iw_IL".equals(full))
101 return "he";
102 else if ("in".equals(full))
103 return "id";
104 else if (I18n.hasCode(full)) // catch all non-single codes
105 return full;
106 }
107
108 // return single code as fallback
109 return locale.getLanguage();
110 }
111
112 /**
113 * Replies the locale code used by Java for a given locale.
114 *
115 * In most cases JOSM and Java uses the same codes, but for some exceptions this is needed.
116 *
117 * @param locale the locale. Replies "en" if null.
118 * @return the Java code for the given locale
119 * @since 8232
120 */
121 public static String getJavaLocaleCode(String localeName) {
122 if (localeName == null) return "en";
123 if ("ca@valencia".equals(localeName)) {
124 localeName = "ca__valencia";
125 } else if ("he".equals(localeName)) {
126 localeName = "iw_IL";
127 } else if ("id".equals(localeName)) {
128 localeName = "in";
129 }
130 return localeName;
131 }
132
133 /**
134 * Replies the display string used by JOSM for a given locale.
135 *
136 * In most cases returns text replied by {@link Locale#getDisplayName()}, for some
137 * locales an override is used (i.e. when unsupported by Java).
138 *
139 * @param locale the locale. Replies "en" if null.
140 * @return the display string for the given locale
141 * @since 8232
142 */
143 public static String getDisplayName(Locale locale) {
144 /*String full = locale.toString();
145 if ("ca__valencia".equals(full))
146 return t_r_c("language", "Valencian");*/
147
148 return locale.getDisplayName();
149 }
150
151 /**
152 * Replies the locale used by Java for a given language code.
153 *
154 * Accepts JOSM and Java codes as input.
155 *
156 * @param localeName the locale code.
157 * @return the resulting locale
158 */
159 public static Locale getLocale(String localeName) {
160 int country = localeName.indexOf("_");
161 int variant = localeName.indexOf("@");
162 if (variant < 0 && country >= 0)
163 variant = localeName.indexOf("_", country+1);
164 Locale l;
165 if (variant > 0 && country > 0) {
166 l = new Locale(localeName.substring(0, country), localeName.substring(country+1, variant), localeName.substring(variant + 1));
167 } else if (variant > 0) {
168 l = new Locale(localeName.substring(0, variant), "", localeName.substring(variant + 1));
169 } else if (country > 0) {
170 l = new Locale(localeName.substring(0, country), localeName.substring(country + 1));
171 } else {
172 l = new Locale(localeName);
173 }
174 return l;
175 }
176
177 /**
178 * Check if a new language is better than a previous existing. Can be used in classes where
179 * multiple user supplied language marked strings appear and the best one is searched. Following
180 * priorities: current language, english, any other
181 *
182 * @param oldLanguage the language code of the existing string
183 * @param newLanguage the language code of the new string
184 * @return true if new one is better
185 * @since 8091
186 */
187 public static boolean isBetterLanguage(String oldLanguage, String newLanguage) {
188 if (oldLanguage == null)
189 return true;
190 String want = getJOSMLocaleCode();
191 return want.equals(newLanguage) || (!want.equals(oldLanguage) && newLanguage.startsWith("en"));
192 }
193
194 /**
195 * Replies the language prefix for use in XML elements (with a dot appended).
196 *
197 * @return the XML language prefix
198 * @see #getJOSMLocaleCode()
199 */
200 public static String getLanguageCodeXML() {
201 String code = getJOSMLocaleCode();
202 code = code.replace("@", "-");
203 return code+".";
204 }
205
206 /**
207 * Replies the language prefix for use in manifests (with an underscore appended).
208 *
209 * @return the manifest language prefix
210 * @see #getJOSMLocaleCode()
211 */
212 public static String getLanguageCodeManifest() {
213 String code = getJOSMLocaleCode();
214 code = code.replace("@", "-");
215 return code+"_";
216 }
217
218 /**
219 * Replies a list of language codes for local names. Prefixes range from very specific
220 * to more generic.
221 * <ul>
222 * <li>lang_COUNTRY@variant of the current locale</li>
223 * <li>lang@variant of the current locale</li>
224 * <li>lang_COUNTRY of the current locale</li>
225 * <li>lang of the current locale</li>
226 * </ul>
227 *
228 * @param locale the locale to use, <code>null</code> for default locale
229 * @since 8283
230 * @return list of codes
231 */
232 public static Collection<String> getLanguageCodes(Locale l) {
233 Collection<String> list = new LinkedList<String>();
234 if(l == null)
235 l = Locale.getDefault();
236 String lang = l.getLanguage();
237 String c = l.getCountry();
238 String v = l.getVariant();
239 if(c.isEmpty())
240 c = null;
241 if(v != null && !v.isEmpty()) {
242 if(c != null)
243 list.add(lang+"_"+c+"@"+v);
244 list.add(lang+"@"+v);
245 }
246 if(c != null)
247 list.add(lang+"_"+c);
248 list.add(lang);
249 return list;
250 }
251}
Note: See TracBrowser for help on using the repository browser.