/[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 1373 - (hide annotations)
Wed Jan 19 12:59:51 2011 UTC (14 years, 1 month ago) by alfonx
Original Path: trunk/src/skrueger/i8n/I8NUtil.java
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 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     // 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 alfonx 185 * Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/>
98     *
99     *
100 alfonx 862 * @author Stefan Alfons Tzeggai
101 alfonx 185 */
102 alfonx 1100 public static String toOneLine(final Translation source) {
103     final StringBuffer oneLine = new StringBuffer();
104     for (final String key : source.keySet()) {
105 alfonx 185 oneLine.append(key + "{" + source.get(key) + "}");
106     }
107     return oneLine.toString();
108     }
109    
110     /**
111 alfonx 862 * @author Stefan Alfons Tzeggai
112 mojays 2 * @param code
113     * @return true if the code paramter is a valid ISO Language code
114     */
115 alfonx 1100 public static boolean isValidISOLangCode(final String code) {
116 alfonx 1373 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 alfonx 38 return getLanguageCodes().contains(code);
126 mojays 2 }
127 alfonx 38
128     /**
129 alfonx 39 * @return All language codes available in
130     * java.util.Locale.getISOLanguages() without duplicates.
131 alfonx 38 */
132     public static Set<String> getLanguageCodes() {
133     return languageCodes;
134     }
135 alfonx 39
136     /**
137 alfonx 1100 * Lookup {@link Locale} where they speak the 2 letter code language.
138     *
139 alfonx 39 * @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 alfonx 1100 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 alfonx 140 if (l.getLanguage().equals(code.toLowerCase())) {
150 alfonx 1100 locales.add(l);
151 alfonx 39 }
152     }
153 alfonx 140
154 alfonx 1100 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 alfonx 1150 Locale l = new Locale(code);
173 alfonx 1373 // LOGGER.error("Can't find Locale for code " + code
174     // + "! Returning a selfmade locale");
175 alfonx 1150 return l;
176 alfonx 140
177 alfonx 1150 // return Locale.getDefault();
178 alfonx 39 }
179 alfonx 44
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 alfonx 518
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 alfonx 822
224 alfonx 518 if (trans == null)
225     return 0.;
226 alfonx 822
227 alfonx 518 Integer cunt = 0;
228     for (final String l : languages) {
229     final String t = trans.get(l);
230     if (!isEmpty(t))
231     cunt++;
232     }
233 alfonx 1373 return cunt.doubleValue() / languages.size();
234 alfonx 518 }
235 alfonx 822
236     /**
237     * The German Umlaute have standard ASCII alternatives that are sometimes
238     * use. This method will replace any possible ASCII-Umlaut Representation
239 alfonx 1100 * into real Umlaute. E.g. "ae" to "ä" and "ue" to "ü". Umlaute are returned
240     * as inline-UTF8.
241 alfonx 822 */
242 alfonx 1100 public static String mitUmlaute(final String withoutUmlaute) {
243 alfonx 823 String replaced = withoutUmlaute;
244 alfonx 1100
245 alfonx 823 replaced = replaced.replaceAll("ue", "\u00FC");
246     replaced = replaced.replaceAll("Ue", "\u00DC");
247 alfonx 1100
248 alfonx 823 replaced = replaced.replaceAll("oe", "\u00F6");
249     replaced = replaced.replaceAll("Oe", "\u00D6");
250 alfonx 1100
251 alfonx 823 replaced = replaced.replaceAll("ae", "\u00E4");
252     replaced = replaced.replaceAll("Ae", "\u00C4");
253 alfonx 822 return replaced;
254     }
255 alfonx 1373
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 mojays 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26