25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.i8n; |
package skrueger.i8n; |
31 |
|
|
32 |
|
import java.util.ArrayList; |
33 |
import java.util.List; |
import java.util.List; |
34 |
import java.util.Locale; |
import java.util.Locale; |
35 |
import java.util.Set; |
import java.util.Set; |
36 |
import java.util.TreeSet; |
import java.util.TreeSet; |
37 |
|
|
38 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
39 |
|
import org.geotools.resources.i18n.Locales; |
40 |
|
|
41 |
public class I8NUtil { |
public class I8NUtil { |
42 |
static final Logger LOGGER = Logger.getLogger(I8NUtil.class); |
static final Logger LOGGER = Logger.getLogger(I8NUtil.class); |
43 |
|
|
44 |
private static Set<String> languageCodes = new TreeSet<String>(); |
private static Set<String> languageCodes = new TreeSet<String>(); |
45 |
static { |
static { |
46 |
for (Locale locale : java.util.Locale.getAvailableLocales()) { |
for (final Locale locale : java.util.Locale.getAvailableLocales()) { |
47 |
getLanguageCodes().add(locale.getLanguage()); |
getLanguageCodes().add(locale.getLanguage()); |
48 |
} |
} |
49 |
// for (String code : java.util.Locale.getISOLanguages()) { |
// for (String code : java.util.Locale.getISOLanguages()) { |
58 |
* <li>If format can't be recognized, the {@link String} is interpreted as |
* <li>If format can't be recognized, the {@link String} is interpreted as |
59 |
* the translation in the <code>{@value #DEFAULT_KEY}</code> language |
* the translation in the <code>{@value #DEFAULT_KEY}</code> language |
60 |
* |
* |
61 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Tzeggai |
62 |
*/ |
*/ |
63 |
public static Translation createFromOneLIne(final String oneLineCoded) { |
public static Translation createFromOneLine(final String oneLineCoded) { |
64 |
Translation result = new Translation(); |
final Translation result = new Translation(); |
65 |
result.fromOneLine(oneLineCoded); |
result.fromOneLine(oneLineCoded); |
66 |
return result; |
return result; |
67 |
} |
} |
70 |
* Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/> |
* Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/> |
71 |
* |
* |
72 |
* |
* |
73 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Tzeggai |
74 |
*/ |
*/ |
75 |
public static String toOneLine(Translation source) { |
public static String toOneLine(final Translation source) { |
76 |
StringBuffer oneLine = new StringBuffer(); |
final StringBuffer oneLine = new StringBuffer(); |
77 |
for (String key : source.keySet()) { |
for (final String key : source.keySet()) { |
78 |
oneLine.append(key + "{" + source.get(key) + "}"); |
oneLine.append(key + "{" + source.get(key) + "}"); |
79 |
} |
} |
80 |
return oneLine.toString(); |
return oneLine.toString(); |
81 |
} |
} |
82 |
|
|
83 |
/** |
/** |
84 |
* @author Stefan Alfons Krüger |
* @author Stefan Alfons Tzeggai |
85 |
* @param code |
* @param code |
86 |
* @return true if the code paramter is a valid ISO Language code |
* @return true if the code paramter is a valid ISO Language code |
87 |
*/ |
*/ |
88 |
public static boolean isValidISOLangCode(String code) { |
public static boolean isValidISOLangCode(final String code) { |
89 |
return getLanguageCodes().contains(code); |
return getLanguageCodes().contains(code); |
90 |
} |
} |
91 |
|
|
98 |
} |
} |
99 |
|
|
100 |
/** |
/** |
101 |
|
* Lookup {@link Locale} where they speak the 2 letter code language. |
102 |
|
* |
103 |
* @param code |
* @param code |
104 |
* A two-letter language code. |
* A two-letter language code. |
105 |
* @return <code>null</code> or one (of many possible) {@link Locale} that |
* @return <code>null</code> or one (of many possible) {@link Locale} that |
106 |
* uses this language. |
* uses this language. |
107 |
*/ |
*/ |
108 |
public static Locale getLocaleFor(String code) { |
public static List<Locale> getLocalesForLang(final String code) { |
109 |
for (Locale l : Locale.getAvailableLocales()) { |
|
110 |
|
final ArrayList<Locale> locales = new ArrayList<Locale>(); |
111 |
|
|
112 |
|
for (final Locale l : Locale.getAvailableLocales()) { |
113 |
if (l.getLanguage().equals(code.toLowerCase())) { |
if (l.getLanguage().equals(code.toLowerCase())) { |
114 |
return l; |
locales.add(l); |
115 |
} |
} |
|
// LOGGER.debug(l.getLanguage() + " not = " + code); |
|
116 |
} |
} |
117 |
|
|
118 |
|
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 |
LOGGER.error("Can't create a Locale for code " + code |
LOGGER.error("Can't create a Locale for code " + code |
137 |
+ "! Returning the system default locale to avoid NPEs."); |
+ "! Returning the system default locale to avoid NPEs."); |
138 |
|
|
182 |
*/ |
*/ |
183 |
public static double qmTranslation(final List<String> languages, |
public static double qmTranslation(final List<String> languages, |
184 |
final Translation trans) { |
final Translation trans) { |
185 |
|
|
186 |
if (trans == null) |
if (trans == null) |
187 |
return 0.; |
return 0.; |
188 |
|
|
189 |
Integer cunt = 0; |
Integer cunt = 0; |
190 |
for (final String l : languages) { |
for (final String l : languages) { |
191 |
final String t = trans.get(l); |
final String t = trans.get(l); |
194 |
} |
} |
195 |
return cunt.doubleValue() / (double) languages.size(); |
return cunt.doubleValue() / (double) languages.size(); |
196 |
} |
} |
197 |
|
|
198 |
|
/** |
199 |
|
* The German Umlaute have standard ASCII alternatives that are sometimes |
200 |
|
* use. This method will replace any possible ASCII-Umlaut Representation |
201 |
|
* into real Umlaute. E.g. "ae" to "ä" and "ue" to "ü". Umlaute are returned |
202 |
|
* as inline-UTF8. |
203 |
|
*/ |
204 |
|
public static String mitUmlaute(final String withoutUmlaute) { |
205 |
|
String replaced = withoutUmlaute; |
206 |
|
|
207 |
|
replaced = replaced.replaceAll("ue", "\u00FC"); |
208 |
|
replaced = replaced.replaceAll("Ue", "\u00DC"); |
209 |
|
|
210 |
|
replaced = replaced.replaceAll("oe", "\u00F6"); |
211 |
|
replaced = replaced.replaceAll("Oe", "\u00D6"); |
212 |
|
|
213 |
|
replaced = replaced.replaceAll("ae", "\u00E4"); |
214 |
|
replaced = replaced.replaceAll("Ae", "\u00C4"); |
215 |
|
return replaced; |
216 |
|
} |
217 |
} |
} |