/[schmitzm]/trunk/src/skrueger/i8n/I8NUtil.java
ViewVC logotype

Contents of /trunk/src/skrueger/i8n/I8NUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1373 - (show annotations)
Wed Jan 19 12:59:51 2011 UTC (14 years, 1 month ago) by alfonx
File size: 8882 byte(s)
GP-Feature: Added support for "user defined Locales". This allows to define a language, that is only spoken in a small area and which is not a official ISO language.
1 /*******************************************************************************
2 * Copyright (c) 2009 Martin O. J. Schmitz.
3 *
4 * This file is part of the SCHMITZM library - a collection of utility
5 * classes based on Java 1.6, focusing (not only) on Java Swing
6 * and the Geotools library.
7 *
8 * The SCHMITZM project is hosted at:
9 * http://wald.intevation.org/projects/schmitzm/
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License (license.txt)
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * or try this link: http://www.gnu.org/licenses/lgpl.html
25 *
26 * Contributors:
27 * Martin O. J. Schmitz - initial API and implementation
28 * Stefan A. Tzeggai - additional utility classes
29 ******************************************************************************/
30 package skrueger.i8n;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Locale;
37 import java.util.Set;
38 import java.util.TreeSet;
39
40 import org.apache.log4j.Logger;
41
42 public class I8NUtil {
43 static final Logger LOGGER = Logger.getLogger(I8NUtil.class);
44
45 public static final HashMap<String, PropertiesLocale> propLocales = new HashMap<String, PropertiesLocale>();
46
47 private static Set<String> languageCodes = new TreeSet<String>();
48 static {
49 for (final Locale locale : java.util.Locale.getAvailableLocales()) {
50 languageCodes.add(locale.getLanguage());
51 }
52 for (String code : java.util.Locale.getISOLanguages()) {
53 languageCodes.add(code);
54 }
55 for (String lang : getPropertiesLanguages()) {
56 languageCodes.add(lang);
57 }
58 }
59
60 /**
61 * Creates a {@link Translation}<br/>
62 * <p>
63 * <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.
64 * <li>If format can't be recognized, the {@link String} is interpreted as
65 * the translation in the <code>{@value #DEFAULT_KEY}</code> language
66 *
67 * @author Stefan Alfons Tzeggai
68 */
69 public static Translation createFromOneLine(final String oneLineCoded) {
70 final Translation result = new Translation();
71 result.fromOneLine(oneLineCoded);
72 return result;
73 }
74
75 /**
76 * A list of Locales defined in a <code>locales.properties</code> File on
77 * the classpath.
78 */
79 static Set<String> getPropertiesLanguages() {
80
81 // URL resource = I8NUtil.class.getResource("/gplocales.properties");
82
83 Locale oshikwanya = new Locale("x1", "na");
84 PropertiesLocale plo1 = new PropertiesLocale(oshikwanya, new Locale(
85 "kj", "na"), "Oshikwanya");
86 propLocales.put(plo1.getLanguage(), plo1);
87
88 Locale oshindonga = new Locale("x2", "na");
89 PropertiesLocale plo2 = new PropertiesLocale(oshindonga, new Locale(
90 "kj", "na"), "Oshindonga");
91 propLocales.put(plo2.getLanguage(), plo2);
92
93 return propLocales.keySet();
94 }
95
96 /**
97 * Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/>
98 *
99 *
100 * @author Stefan Alfons Tzeggai
101 */
102 public static String toOneLine(final Translation source) {
103 final StringBuffer oneLine = new StringBuffer();
104 for (final String key : source.keySet()) {
105 oneLine.append(key + "{" + source.get(key) + "}");
106 }
107 return oneLine.toString();
108 }
109
110 /**
111 * @author Stefan Alfons Tzeggai
112 * @param code
113 * @return true if the code paramter is a valid ISO Language code
114 */
115 public static boolean isValidISOLangCode(final String code) {
116 return Arrays.asList(java.util.Locale.getISOLanguages()).contains(code);
117 }
118
119 /**
120 * @author Stefan Alfons Tzeggai
121 * @param code
122 * @return true if the code paramter is a valid ISO Language code
123 */
124 public static boolean isValidLangCode(final String code) {
125 return getLanguageCodes().contains(code);
126 }
127
128 /**
129 * @return All language codes available in
130 * java.util.Locale.getISOLanguages() without duplicates.
131 */
132 public static Set<String> getLanguageCodes() {
133 return languageCodes;
134 }
135
136 /**
137 * Lookup {@link Locale} where they speak the 2 letter code language.
138 *
139 * @param code
140 * A two-letter language code.
141 * @return <code>null</code> or one (of many possible) {@link Locale} that
142 * uses this language.
143 */
144 public static List<Locale> getLocalesForLang(final String code) {
145
146 final ArrayList<Locale> locales = new ArrayList<Locale>();
147
148 for (final Locale l : Locale.getAvailableLocales()) {
149 if (l.getLanguage().equals(code.toLowerCase())) {
150 locales.add(l);
151 }
152 }
153
154 return locales;
155 }
156
157 /**
158 * Lookup first country where they speak the 2 letter code language.
159 *
160 * @param code
161 * A two-letter language code.
162 * @return <code>null</code> or one (of many possible) {@link Locale} that
163 * uses this language.
164 */
165 public static Locale getFirstLocaleForLang(final String code) {
166
167 List<Locale> locales = getLocalesForLang(code);
168
169 if (locales.size() > 0)
170 return locales.get(0);
171
172 Locale l = new Locale(code);
173 // LOGGER.error("Can't find Locale for code " + code
174 // + "! Returning a selfmade locale");
175 return l;
176
177 // return Locale.getDefault();
178 }
179
180 /**
181 * A convenience method that checks if the {@link Translation} object
182 * contains a translation for the active language. A {@link String}
183 * containing only spaces will return <code>false</code>.
184 *
185 * @param trans
186 * {@link Translation} to check.
187 */
188 public static boolean isEmpty(final Translation trans) {
189 if (trans == null)
190 return true;
191 return isEmpty(trans.toString());
192 }
193
194 /**
195 * A convenience method that checks if the {@link String} returned by from a
196 * {@link Translation} object contains a "valid" translation for the active
197 * language. A {@link String} containing only spaces or equals
198 * {@link Translation}.NO_TRANSLATION will return <code>false</code>.
199 *
200 * @param transString
201 * {@link String} to check.
202 */
203 public static boolean isEmpty(final String transString) {
204 if (transString == null)
205 return true;
206 if (transString.trim().isEmpty())
207 return true;
208 if (transString.equals(Translation.NO_TRANSLATION))
209 return true;
210 return false;
211 }
212
213 /**
214 * @return a {@link Double} between 0 and 1 representing the part of the
215 * given {@link Translation} that has been filled.
216 * @param ac
217 * {@link AtlasConfig} to determine the languages to expect.
218 * @param trans
219 * The {@link Translation} to check.
220 */
221 public static double qmTranslation(final List<String> languages,
222 final Translation trans) {
223
224 if (trans == null)
225 return 0.;
226
227 Integer cunt = 0;
228 for (final String l : languages) {
229 final String t = trans.get(l);
230 if (!isEmpty(t))
231 cunt++;
232 }
233 return cunt.doubleValue() / languages.size();
234 }
235
236 /**
237 * The German Umlaute have standard ASCII alternatives that are sometimes
238 * use. This method will replace any possible ASCII-Umlaut Representation
239 * into real Umlaute. E.g. "ae" to "ä" and "ue" to "ü". Umlaute are returned
240 * as inline-UTF8.
241 */
242 public static String mitUmlaute(final String withoutUmlaute) {
243 String replaced = withoutUmlaute;
244
245 replaced = replaced.replaceAll("ue", "\u00FC");
246 replaced = replaced.replaceAll("Ue", "\u00DC");
247
248 replaced = replaced.replaceAll("oe", "\u00F6");
249 replaced = replaced.replaceAll("Oe", "\u00D6");
250
251 replaced = replaced.replaceAll("ae", "\u00E4");
252 replaced = replaced.replaceAll("Ae", "\u00C4");
253 return replaced;
254 }
255
256 public static boolean isPropertiesLanguage(String langCode) {
257 for (String lc : getPropertiesLanguages()) {
258 if (lc.equals(langCode))
259 return true;
260 }
261 return false;
262 }
263
264 /**
265 * Returns a String that displays the denoted language in three ways, e.g.
266 * "German / Deutsch / de" on a computer that has English as an active
267 * Locale.
268 */
269 public static String getMultilanguageString(String langCode) {
270 if (isValidISOLangCode(langCode)) {
271 /**
272 * Lookup a Locale where they speak the language, so we can print
273 * the language in local tounge.
274 */
275 Locale locale = I8NUtil.getFirstLocaleForLang(langCode);
276 return locale.getDisplayLanguage(locale) + " / "
277 + locale.getDisplayLanguage() + " / " + langCode;
278 } else if (isPropertiesLanguage(langCode)) {
279 PropertiesLocale pl = I8NUtil.propLocales.get(langCode);
280 return pl.getDisplayLanguage() + " / "
281 + pl.getDisplayLanguage(langCode) + " " + langCode;
282 }
283
284 return langCode;
285 }
286 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26