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