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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26