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