/[schmitzm]/branches/2.4.x/src/skrueger/i8n/I8NUtil.java
ViewVC logotype

Annotation of /branches/2.4.x/src/skrueger/i8n/I8NUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1374 - (hide annotations)
Wed Jan 19 13:06:33 2011 UTC (14 years, 1 month ago) by alfonx
Original Path: trunk/src/skrueger/i8n/I8NUtil.java
File size: 8932 byte(s)


1 alfonx 244 /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3     *
4     * This file is part of the SCHMITZM library - a collection of utility
5 alfonx 256 * classes based on Java 1.6, focusing (not only) on Java Swing
6 alfonx 244 * 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 alfonx 862 * Stefan A. Tzeggai - additional utility classes
29 alfonx 244 ******************************************************************************/
30 mojays 2 package skrueger.i8n;
31    
32 alfonx 1100 import java.util.ArrayList;
33 alfonx 1373 import java.util.Arrays;
34     import java.util.HashMap;
35 alfonx 518 import java.util.List;
36 alfonx 39 import java.util.Locale;
37 alfonx 38 import java.util.Set;
38     import java.util.TreeSet;
39 mojays 2
40 alfonx 43 import org.apache.log4j.Logger;
41    
42 mojays 2 public class I8NUtil {
43 alfonx 43 static final Logger LOGGER = Logger.getLogger(I8NUtil.class);
44 alfonx 39
45 alfonx 1373 public static final HashMap<String, PropertiesLocale> propLocales = new HashMap<String, PropertiesLocale>();
46    
47 alfonx 38 private static Set<String> languageCodes = new TreeSet<String>();
48 mojays 2 static {
49 alfonx 1100 for (final Locale locale : java.util.Locale.getAvailableLocales()) {
50 alfonx 1150 languageCodes.add(locale.getLanguage());
51 alfonx 39 }
52 alfonx 1150 for (String code : java.util.Locale.getISOLanguages()) {
53     languageCodes.add(code);
54     }
55 alfonx 1373 for (String lang : getPropertiesLanguages()) {
56     languageCodes.add(lang);
57     }
58 alfonx 39 }
59    
60 mojays 2 /**
61 alfonx 185 * 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 alfonx 862 * @author Stefan Alfons Tzeggai
68 alfonx 185 */
69 alfonx 1097 public static Translation createFromOneLine(final String oneLineCoded) {
70 alfonx 1100 final Translation result = new Translation();
71 alfonx 185 result.fromOneLine(oneLineCoded);
72     return result;
73     }
74    
75     /**
76 alfonx 1373 * A list of Locales defined in a <code>locales.properties</code> File on
77     * the classpath.
78     */
79     static Set<String> getPropertiesLanguages() {
80    
81 alfonx 1374 // TODO read these definitions from a text file
82 alfonx 1373 // URL resource = I8NUtil.class.getResource("/gplocales.properties");
83    
84 alfonx 1374 Locale oshikwanya = new Locale("ok", "na");
85 alfonx 1373 PropertiesLocale plo1 = new PropertiesLocale(oshikwanya, new Locale(
86     "kj", "na"), "Oshikwanya");
87     propLocales.put(plo1.getLanguage(), plo1);
88    
89 alfonx 1374 Locale oshindonga = new Locale("od", "na");
90 alfonx 1373 PropertiesLocale plo2 = new PropertiesLocale(oshindonga, new Locale(
91     "kj", "na"), "Oshindonga");
92     propLocales.put(plo2.getLanguage(), plo2);
93    
94     return propLocales.keySet();
95     }
96    
97     /**
98 alfonx 185 * Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/>
99     *
100     *
101 alfonx 862 * @author Stefan Alfons Tzeggai
102 alfonx 185 */
103 alfonx 1100 public static String toOneLine(final Translation source) {
104     final StringBuffer oneLine = new StringBuffer();
105     for (final String key : source.keySet()) {
106 alfonx 185 oneLine.append(key + "{" + source.get(key) + "}");
107     }
108     return oneLine.toString();
109     }
110    
111     /**
112 alfonx 862 * @author Stefan Alfons Tzeggai
113 mojays 2 * @param code
114     * @return true if the code paramter is a valid ISO Language code
115     */
116 alfonx 1100 public static boolean isValidISOLangCode(final String code) {
117 alfonx 1373 return Arrays.asList(java.util.Locale.getISOLanguages()).contains(code);
118     }
119    
120     /**
121     * @author Stefan Alfons Tzeggai
122     * @param code
123     * @return true if the code paramter is a valid ISO Language code
124     */
125     public static boolean isValidLangCode(final String code) {
126 alfonx 38 return getLanguageCodes().contains(code);
127 mojays 2 }
128 alfonx 38
129     /**
130 alfonx 39 * @return All language codes available in
131     * java.util.Locale.getISOLanguages() without duplicates.
132 alfonx 38 */
133     public static Set<String> getLanguageCodes() {
134     return languageCodes;
135     }
136 alfonx 39
137     /**
138 alfonx 1100 * Lookup {@link Locale} where they speak the 2 letter code language.
139     *
140 alfonx 39 * @param code
141     * A two-letter language code.
142     * @return <code>null</code> or one (of many possible) {@link Locale} that
143     * uses this language.
144     */
145 alfonx 1100 public static List<Locale> getLocalesForLang(final String code) {
146    
147     final ArrayList<Locale> locales = new ArrayList<Locale>();
148    
149     for (final Locale l : Locale.getAvailableLocales()) {
150 alfonx 140 if (l.getLanguage().equals(code.toLowerCase())) {
151 alfonx 1100 locales.add(l);
152 alfonx 39 }
153     }
154 alfonx 140
155 alfonx 1100 return locales;
156     }
157    
158     /**
159     * Lookup first country where they speak the 2 letter code language.
160     *
161     * @param code
162     * A two-letter language code.
163     * @return <code>null</code> or one (of many possible) {@link Locale} that
164     * uses this language.
165     */
166     public static Locale getFirstLocaleForLang(final String code) {
167    
168     List<Locale> locales = getLocalesForLang(code);
169    
170     if (locales.size() > 0)
171     return locales.get(0);
172    
173 alfonx 1150 Locale l = new Locale(code);
174 alfonx 1373 // LOGGER.error("Can't find Locale for code " + code
175     // + "! Returning a selfmade locale");
176 alfonx 1150 return l;
177 alfonx 140
178 alfonx 1150 // return Locale.getDefault();
179 alfonx 39 }
180 alfonx 44
181     /**
182     * A convenience method that checks if the {@link Translation} object
183     * contains a translation for the active language. A {@link String}
184     * containing only spaces will return <code>false</code>.
185     *
186     * @param trans
187     * {@link Translation} to check.
188     */
189     public static boolean isEmpty(final Translation trans) {
190     if (trans == null)
191     return true;
192     return isEmpty(trans.toString());
193     }
194    
195     /**
196     * A convenience method that checks if the {@link String} returned by from a
197     * {@link Translation} object contains a "valid" translation for the active
198     * language. A {@link String} containing only spaces or equals
199     * {@link Translation}.NO_TRANSLATION will return <code>false</code>.
200     *
201     * @param transString
202     * {@link String} to check.
203     */
204     public static boolean isEmpty(final String transString) {
205     if (transString == null)
206     return true;
207     if (transString.trim().isEmpty())
208     return true;
209     if (transString.equals(Translation.NO_TRANSLATION))
210     return true;
211     return false;
212     }
213 alfonx 518
214     /**
215     * @return a {@link Double} between 0 and 1 representing the part of the
216     * given {@link Translation} that has been filled.
217     * @param ac
218     * {@link AtlasConfig} to determine the languages to expect.
219     * @param trans
220     * The {@link Translation} to check.
221     */
222     public static double qmTranslation(final List<String> languages,
223     final Translation trans) {
224 alfonx 822
225 alfonx 518 if (trans == null)
226     return 0.;
227 alfonx 822
228 alfonx 518 Integer cunt = 0;
229     for (final String l : languages) {
230     final String t = trans.get(l);
231     if (!isEmpty(t))
232     cunt++;
233     }
234 alfonx 1373 return cunt.doubleValue() / languages.size();
235 alfonx 518 }
236 alfonx 822
237     /**
238     * The German Umlaute have standard ASCII alternatives that are sometimes
239     * use. This method will replace any possible ASCII-Umlaut Representation
240 alfonx 1100 * into real Umlaute. E.g. "ae" to "ä" and "ue" to "ü". Umlaute are returned
241     * as inline-UTF8.
242 alfonx 822 */
243 alfonx 1100 public static String mitUmlaute(final String withoutUmlaute) {
244 alfonx 823 String replaced = withoutUmlaute;
245 alfonx 1100
246 alfonx 823 replaced = replaced.replaceAll("ue", "\u00FC");
247     replaced = replaced.replaceAll("Ue", "\u00DC");
248 alfonx 1100
249 alfonx 823 replaced = replaced.replaceAll("oe", "\u00F6");
250     replaced = replaced.replaceAll("Oe", "\u00D6");
251 alfonx 1100
252 alfonx 823 replaced = replaced.replaceAll("ae", "\u00E4");
253     replaced = replaced.replaceAll("Ae", "\u00C4");
254 alfonx 822 return replaced;
255     }
256 alfonx 1373
257     public static boolean isPropertiesLanguage(String langCode) {
258     for (String lc : getPropertiesLanguages()) {
259     if (lc.equals(langCode))
260     return true;
261     }
262     return false;
263     }
264    
265     /**
266     * Returns a String that displays the denoted language in three ways, e.g.
267     * "German / Deutsch / de" on a computer that has English as an active
268     * Locale.
269     */
270     public static String getMultilanguageString(String langCode) {
271     if (isValidISOLangCode(langCode)) {
272     /**
273     * Lookup a Locale where they speak the language, so we can print
274     * the language in local tounge.
275     */
276     Locale locale = I8NUtil.getFirstLocaleForLang(langCode);
277     return locale.getDisplayLanguage(locale) + " / "
278     + locale.getDisplayLanguage() + " / " + langCode;
279     } else if (isPropertiesLanguage(langCode)) {
280     PropertiesLocale pl = I8NUtil.propLocales.get(langCode);
281     return pl.getDisplayLanguage() + " / "
282     + pl.getDisplayLanguage(langCode) + " " + langCode;
283     }
284    
285     return langCode;
286     }
287 mojays 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26