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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1100 - (hide annotations)
Mon Oct 11 00:07:14 2010 UTC (14 years, 4 months ago) by alfonx
File size: 6800 byte(s)
Geopublisher can now switch languages for the GUI and the atlas data mostly independent.
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 518 import java.util.List;
34 alfonx 39 import java.util.Locale;
35 alfonx 38 import java.util.Set;
36     import java.util.TreeSet;
37 mojays 2
38 alfonx 43 import org.apache.log4j.Logger;
39 alfonx 1100 import org.geotools.resources.i18n.Locales;
40 alfonx 43
41 mojays 2 public class I8NUtil {
42 alfonx 43 static final Logger LOGGER = Logger.getLogger(I8NUtil.class);
43 alfonx 39
44 alfonx 38 private static Set<String> languageCodes = new TreeSet<String>();
45 mojays 2 static {
46 alfonx 1100 for (final Locale locale : java.util.Locale.getAvailableLocales()) {
47 alfonx 39 getLanguageCodes().add(locale.getLanguage());
48     }
49     // for (String code : java.util.Locale.getISOLanguages()) {
50     // getLanguageCodes().add(code);
51     // }
52     }
53    
54 mojays 2 /**
55 alfonx 185 * Creates a {@link Translation}<br/>
56     * <p>
57     * <li>If <code>oneLineCoded</code> is empty or null, NO TRANSLATION is set.
58     * <li>If format can't be recognized, the {@link String} is interpreted as
59     * the translation in the <code>{@value #DEFAULT_KEY}</code> language
60     *
61 alfonx 862 * @author Stefan Alfons Tzeggai
62 alfonx 185 */
63 alfonx 1097 public static Translation createFromOneLine(final String oneLineCoded) {
64 alfonx 1100 final Translation result = new Translation();
65 alfonx 185 result.fromOneLine(oneLineCoded);
66     return result;
67     }
68    
69     /**
70     * Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/>
71     *
72     *
73 alfonx 862 * @author Stefan Alfons Tzeggai
74 alfonx 185 */
75 alfonx 1100 public static String toOneLine(final Translation source) {
76     final StringBuffer oneLine = new StringBuffer();
77     for (final String key : source.keySet()) {
78 alfonx 185 oneLine.append(key + "{" + source.get(key) + "}");
79     }
80     return oneLine.toString();
81     }
82    
83     /**
84 alfonx 862 * @author Stefan Alfons Tzeggai
85 mojays 2 * @param code
86     * @return true if the code paramter is a valid ISO Language code
87     */
88 alfonx 1100 public static boolean isValidISOLangCode(final String code) {
89 alfonx 38 return getLanguageCodes().contains(code);
90 mojays 2 }
91 alfonx 38
92     /**
93 alfonx 39 * @return All language codes available in
94     * java.util.Locale.getISOLanguages() without duplicates.
95 alfonx 38 */
96     public static Set<String> getLanguageCodes() {
97     return languageCodes;
98     }
99 alfonx 39
100     /**
101 alfonx 1100 * Lookup {@link Locale} where they speak the 2 letter code language.
102     *
103 alfonx 39 * @param code
104     * A two-letter language code.
105     * @return <code>null</code> or one (of many possible) {@link Locale} that
106     * uses this language.
107     */
108 alfonx 1100 public static List<Locale> getLocalesForLang(final String code) {
109    
110     final ArrayList<Locale> locales = new ArrayList<Locale>();
111    
112     for (final Locale l : Locale.getAvailableLocales()) {
113 alfonx 140 if (l.getLanguage().equals(code.toLowerCase())) {
114 alfonx 1100 locales.add(l);
115 alfonx 39 }
116     }
117 alfonx 140
118 alfonx 1100 return locales;
119     }
120    
121     /**
122     * Lookup first country where they speak the 2 letter code language.
123     *
124     * @param code
125     * A two-letter language code.
126     * @return <code>null</code> or one (of many possible) {@link Locale} that
127     * uses this language.
128     */
129     public static Locale getFirstLocaleForLang(final String code) {
130    
131     List<Locale> locales = getLocalesForLang(code);
132    
133     if (locales.size() > 0)
134     return locales.get(0);
135    
136 alfonx 44 LOGGER.error("Can't create a Locale for code " + code
137     + "! Returning the system default locale to avoid NPEs.");
138 alfonx 140
139 alfonx 43 return Locale.getDefault();
140 alfonx 39 }
141 alfonx 44
142     /**
143     * A convenience method that checks if the {@link Translation} object
144     * contains a translation for the active language. A {@link String}
145     * containing only spaces will return <code>false</code>.
146     *
147     * @param trans
148     * {@link Translation} to check.
149     */
150     public static boolean isEmpty(final Translation trans) {
151     if (trans == null)
152     return true;
153     return isEmpty(trans.toString());
154     }
155    
156     /**
157     * A convenience method that checks if the {@link String} returned by from a
158     * {@link Translation} object contains a "valid" translation for the active
159     * language. A {@link String} containing only spaces or equals
160     * {@link Translation}.NO_TRANSLATION will return <code>false</code>.
161     *
162     * @param transString
163     * {@link String} to check.
164     */
165     public static boolean isEmpty(final String transString) {
166     if (transString == null)
167     return true;
168     if (transString.trim().isEmpty())
169     return true;
170     if (transString.equals(Translation.NO_TRANSLATION))
171     return true;
172     return false;
173     }
174 alfonx 518
175     /**
176     * @return a {@link Double} between 0 and 1 representing the part of the
177     * given {@link Translation} that has been filled.
178     * @param ac
179     * {@link AtlasConfig} to determine the languages to expect.
180     * @param trans
181     * The {@link Translation} to check.
182     */
183     public static double qmTranslation(final List<String> languages,
184     final Translation trans) {
185 alfonx 822
186 alfonx 518 if (trans == null)
187     return 0.;
188 alfonx 822
189 alfonx 518 Integer cunt = 0;
190     for (final String l : languages) {
191     final String t = trans.get(l);
192     if (!isEmpty(t))
193     cunt++;
194     }
195     return cunt.doubleValue() / (double) languages.size();
196     }
197 alfonx 822
198     /**
199     * The German Umlaute have standard ASCII alternatives that are sometimes
200     * use. This method will replace any possible ASCII-Umlaut Representation
201 alfonx 1100 * into real Umlaute. E.g. "ae" to "ä" and "ue" to "ü". Umlaute are returned
202     * as inline-UTF8.
203 alfonx 822 */
204 alfonx 1100 public static String mitUmlaute(final String withoutUmlaute) {
205 alfonx 823 String replaced = withoutUmlaute;
206 alfonx 1100
207 alfonx 823 replaced = replaced.replaceAll("ue", "\u00FC");
208     replaced = replaced.replaceAll("Ue", "\u00DC");
209 alfonx 1100
210 alfonx 823 replaced = replaced.replaceAll("oe", "\u00F6");
211     replaced = replaced.replaceAll("Oe", "\u00D6");
212 alfonx 1100
213 alfonx 823 replaced = replaced.replaceAll("ae", "\u00E4");
214     replaced = replaced.replaceAll("Ae", "\u00C4");
215 alfonx 822 return replaced;
216     }
217 mojays 2 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26