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