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; |
42 |
|
|
43 |
private static Set<String> languageCodes = new TreeSet<String>(); |
private static Set<String> languageCodes = new TreeSet<String>(); |
44 |
static { |
static { |
45 |
for (Locale locale : java.util.Locale.getAvailableLocales()) { |
for (final Locale locale : java.util.Locale.getAvailableLocales()) { |
46 |
getLanguageCodes().add(locale.getLanguage()); |
languageCodes.add(locale.getLanguage()); |
47 |
|
} |
48 |
|
for (String code : java.util.Locale.getISOLanguages()) { |
49 |
|
languageCodes.add(code); |
50 |
} |
} |
|
// for (String code : java.util.Locale.getISOLanguages()) { |
|
|
// getLanguageCodes().add(code); |
|
|
// } |
|
51 |
} |
} |
52 |
|
|
53 |
/** |
/** |
59 |
* |
* |
60 |
* @author Stefan Alfons Tzeggai |
* @author Stefan Alfons Tzeggai |
61 |
*/ |
*/ |
62 |
public static Translation createFromOneLIne(final String oneLineCoded) { |
public static Translation createFromOneLine(final String oneLineCoded) { |
63 |
Translation result = new Translation(); |
final Translation result = new Translation(); |
64 |
result.fromOneLine(oneLineCoded); |
result.fromOneLine(oneLineCoded); |
65 |
return result; |
return result; |
66 |
} |
} |
71 |
* |
* |
72 |
* @author Stefan Alfons Tzeggai |
* @author Stefan Alfons Tzeggai |
73 |
*/ |
*/ |
74 |
public static String toOneLine(Translation source) { |
public static String toOneLine(final Translation source) { |
75 |
StringBuffer oneLine = new StringBuffer(); |
final StringBuffer oneLine = new StringBuffer(); |
76 |
for (String key : source.keySet()) { |
for (final String key : source.keySet()) { |
77 |
oneLine.append(key + "{" + source.get(key) + "}"); |
oneLine.append(key + "{" + source.get(key) + "}"); |
78 |
} |
} |
79 |
return oneLine.toString(); |
return oneLine.toString(); |
84 |
* @param code |
* @param code |
85 |
* @return true if the code paramter is a valid ISO Language code |
* @return true if the code paramter is a valid ISO Language code |
86 |
*/ |
*/ |
87 |
public static boolean isValidISOLangCode(String code) { |
public static boolean isValidISOLangCode(final String code) { |
88 |
return getLanguageCodes().contains(code); |
return getLanguageCodes().contains(code); |
89 |
} |
} |
90 |
|
|
97 |
} |
} |
98 |
|
|
99 |
/** |
/** |
100 |
|
* Lookup {@link Locale} where they speak the 2 letter code language. |
101 |
|
* |
102 |
* @param code |
* @param code |
103 |
* A two-letter language code. |
* A two-letter language code. |
104 |
* @return <code>null</code> or one (of many possible) {@link Locale} that |
* @return <code>null</code> or one (of many possible) {@link Locale} that |
105 |
* uses this language. |
* uses this language. |
106 |
*/ |
*/ |
107 |
public static Locale getLocaleFor(String code) { |
public static List<Locale> getLocalesForLang(final String code) { |
108 |
for (Locale l : Locale.getAvailableLocales()) { |
|
109 |
|
final ArrayList<Locale> locales = new ArrayList<Locale>(); |
110 |
|
|
111 |
|
for (final Locale l : Locale.getAvailableLocales()) { |
112 |
if (l.getLanguage().equals(code.toLowerCase())) { |
if (l.getLanguage().equals(code.toLowerCase())) { |
113 |
return l; |
locales.add(l); |
114 |
} |
} |
|
// LOGGER.debug(l.getLanguage() + " not = " + code); |
|
115 |
} |
} |
116 |
|
|
117 |
LOGGER.error("Can't create a Locale for code " + code |
return locales; |
118 |
+ "! Returning the system default locale to avoid NPEs."); |
} |
119 |
|
|
120 |
return Locale.getDefault(); |
/** |
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 |
|
Locale l = new Locale(code); |
136 |
|
LOGGER.error("Can't find Locale for code " + code |
137 |
|
+ "! Returning a selfmade locale"); |
138 |
|
return l; |
139 |
|
|
140 |
|
// return Locale.getDefault(); |
141 |
} |
} |
142 |
|
|
143 |
/** |
/** |
199 |
/** |
/** |
200 |
* The German Umlaute have standard ASCII alternatives that are sometimes |
* The German Umlaute have standard ASCII alternatives that are sometimes |
201 |
* use. This method will replace any possible ASCII-Umlaut Representation |
* use. This method will replace any possible ASCII-Umlaut Representation |
202 |
* into real Umlaute. E.g. "ae" to "ä" and "ue" to "ü". Umlaute are returned as inline-UTF8. |
* into real Umlaute. E.g. "ae" to "ä" and "ue" to "ü". Umlaute are returned |
203 |
|
* as inline-UTF8. |
204 |
*/ |
*/ |
205 |
public static String mitUmlaute(String withoutUmlaute) { |
public static String mitUmlaute(final String withoutUmlaute) { |
206 |
String replaced = withoutUmlaute; |
String replaced = withoutUmlaute; |
207 |
|
|
208 |
replaced = replaced.replaceAll("ue", "\u00FC"); |
replaced = replaced.replaceAll("ue", "\u00FC"); |
209 |
replaced = replaced.replaceAll("Ue", "\u00DC"); |
replaced = replaced.replaceAll("Ue", "\u00DC"); |
210 |
|
|
211 |
replaced = replaced.replaceAll("oe", "\u00F6"); |
replaced = replaced.replaceAll("oe", "\u00F6"); |
212 |
replaced = replaced.replaceAll("Oe", "\u00D6"); |
replaced = replaced.replaceAll("Oe", "\u00D6"); |
213 |
|
|
214 |
replaced = replaced.replaceAll("ae", "\u00E4"); |
replaced = replaced.replaceAll("ae", "\u00E4"); |
215 |
replaced = replaced.replaceAll("Ae", "\u00C4"); |
replaced = replaced.replaceAll("Ae", "\u00C4"); |
|
// TODO mehr |
|
|
// TODO besser UTF \u123321 schreibweise |
|
216 |
return replaced; |
return replaced; |
217 |
} |
} |
218 |
} |
} |