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