[Schmitzm-commits] r1373 - in trunk: src/schmitzm/geotools/gui/resource/locales src/skrueger/i8n src_junit/skrueger/i8n
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Wed Jan 19 13:59:52 CET 2011
Author: alfonx
Date: 2011-01-19 13:59:51 +0100 (Wed, 19 Jan 2011)
New Revision: 1373
Added:
trunk/src/skrueger/i8n/PropertiesLocale.java
Modified:
trunk/src/schmitzm/geotools/gui/resource/locales/GTResourceBundle.properties
trunk/src/skrueger/i8n/I8NUtil.java
trunk/src/skrueger/i8n/LanguagesComboBox.java
trunk/src/skrueger/i8n/Translation.java
trunk/src_junit/skrueger/i8n/I8NUtilTest.java
Log:
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.
Modified: trunk/src/schmitzm/geotools/gui/resource/locales/GTResourceBundle.properties
===================================================================
--- trunk/src/schmitzm/geotools/gui/resource/locales/GTResourceBundle.properties 2011-01-17 23:11:17 UTC (rev 1372)
+++ trunk/src/schmitzm/geotools/gui/resource/locales/GTResourceBundle.properties 2011-01-19 12:59:51 UTC (rev 1373)
@@ -197,4 +197,4 @@
Logoposition.TOPRIGHT=top-right
Logoposition.BOTTOMRIGHT=bottom-right
Logoposition.TOPLEFT=top-left
-Logoposition.BOTTOMLEFT=bottom-left
\ No newline at end of file
+Logoposition.BOTTOMLEFT=bottom-left
Modified: trunk/src/skrueger/i8n/I8NUtil.java
===================================================================
--- trunk/src/skrueger/i8n/I8NUtil.java 2011-01-17 23:11:17 UTC (rev 1372)
+++ trunk/src/skrueger/i8n/I8NUtil.java 2011-01-19 12:59:51 UTC (rev 1373)
@@ -30,6 +30,8 @@
package skrueger.i8n;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@@ -40,6 +42,8 @@
public class I8NUtil {
static final Logger LOGGER = Logger.getLogger(I8NUtil.class);
+ public static final HashMap<String, PropertiesLocale> propLocales = new HashMap<String, PropertiesLocale>();
+
private static Set<String> languageCodes = new TreeSet<String>();
static {
for (final Locale locale : java.util.Locale.getAvailableLocales()) {
@@ -48,6 +52,9 @@
for (String code : java.util.Locale.getISOLanguages()) {
languageCodes.add(code);
}
+ for (String lang : getPropertiesLanguages()) {
+ languageCodes.add(lang);
+ }
}
/**
@@ -66,6 +73,27 @@
}
/**
+ * A list of Locales defined in a <code>locales.properties</code> File on
+ * the classpath.
+ */
+ static Set<String> getPropertiesLanguages() {
+
+ // URL resource = I8NUtil.class.getResource("/gplocales.properties");
+
+ Locale oshikwanya = new Locale("x1", "na");
+ PropertiesLocale plo1 = new PropertiesLocale(oshikwanya, new Locale(
+ "kj", "na"), "Oshikwanya");
+ propLocales.put(plo1.getLanguage(), plo1);
+
+ Locale oshindonga = new Locale("x2", "na");
+ PropertiesLocale plo2 = new PropertiesLocale(oshindonga, new Locale(
+ "kj", "na"), "Oshindonga");
+ propLocales.put(plo2.getLanguage(), plo2);
+
+ return propLocales.keySet();
+ }
+
+ /**
* Returns the Translation to a String of the Format: "de{Baum}en{tree}" <br/>
*
*
@@ -85,6 +113,15 @@
* @return true if the code paramter is a valid ISO Language code
*/
public static boolean isValidISOLangCode(final String code) {
+ return Arrays.asList(java.util.Locale.getISOLanguages()).contains(code);
+ }
+
+ /**
+ * @author Stefan Alfons Tzeggai
+ * @param code
+ * @return true if the code paramter is a valid ISO Language code
+ */
+ public static boolean isValidLangCode(final String code) {
return getLanguageCodes().contains(code);
}
@@ -133,8 +170,8 @@
return locales.get(0);
Locale l = new Locale(code);
-// LOGGER.error("Can't find Locale for code " + code
-// + "! Returning a selfmade locale");
+ // LOGGER.error("Can't find Locale for code " + code
+ // + "! Returning a selfmade locale");
return l;
// return Locale.getDefault();
@@ -193,7 +230,7 @@
if (!isEmpty(t))
cunt++;
}
- return cunt.doubleValue() / (double) languages.size();
+ return cunt.doubleValue() / languages.size();
}
/**
@@ -215,4 +252,35 @@
replaced = replaced.replaceAll("Ae", "\u00C4");
return replaced;
}
+
+ public static boolean isPropertiesLanguage(String langCode) {
+ for (String lc : getPropertiesLanguages()) {
+ if (lc.equals(langCode))
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Returns a String that displays the denoted language in three ways, e.g.
+ * "German / Deutsch / de" on a computer that has English as an active
+ * Locale.
+ */
+ public static String getMultilanguageString(String langCode) {
+ if (isValidISOLangCode(langCode)) {
+ /**
+ * Lookup a Locale where they speak the language, so we can print
+ * the language in local tounge.
+ */
+ Locale locale = I8NUtil.getFirstLocaleForLang(langCode);
+ return locale.getDisplayLanguage(locale) + " / "
+ + locale.getDisplayLanguage() + " / " + langCode;
+ } else if (isPropertiesLanguage(langCode)) {
+ PropertiesLocale pl = I8NUtil.propLocales.get(langCode);
+ return pl.getDisplayLanguage() + " / "
+ + pl.getDisplayLanguage(langCode) + " " + langCode;
+ }
+
+ return langCode;
+ }
}
Modified: trunk/src/skrueger/i8n/LanguagesComboBox.java
===================================================================
--- trunk/src/skrueger/i8n/LanguagesComboBox.java 2011-01-17 23:11:17 UTC (rev 1372)
+++ trunk/src/skrueger/i8n/LanguagesComboBox.java 2011-01-19 12:59:51 UTC (rev 1373)
@@ -3,7 +3,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
-import java.util.Locale;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
@@ -14,7 +13,7 @@
*/
public class LanguagesComboBox extends JComboBox {
- private Vector<String> availLangs = new Vector<String>();
+ private final Vector<String> availLangs = new Vector<String>();
public LanguagesComboBox() {
super();
@@ -57,10 +56,19 @@
availLangs.clear();
for (String lc : langCodes) {
- Locale locale = I8NUtil.getFirstLocaleForLang(lc);
+ //
+ // if (I8NUtil.isValidISOLangCode(lc)) {
+ // Locale locale = I8NUtil.getFirstLocaleForLang(lc);
+ // availLangs.add(locale.getDisplayLanguage() + " / "
+ // + locale.getDisplayLanguage(locale) + " " + lc);
+ // } else if (I8NUtil.isPropertiesLanguage(lc)) {
+ //
+ // PropertiesLocale pl = I8NUtil.propLocales.get(lc);
+ // availLangs.add(pl.getDisplayLanguage() + " / "
+ // + pl.getDisplayLanguage(lc) + " " + lc);
+ // }
- availLangs.add(locale.getDisplayLanguage() + " / "
- + locale.getDisplayLanguage(locale) + " " + lc);
+ availLangs.add(I8NUtil.getMultilanguageString(lc));
}
Collections.sort(availLangs);
Added: trunk/src/skrueger/i8n/PropertiesLocale.java
===================================================================
--- trunk/src/skrueger/i8n/PropertiesLocale.java 2011-01-17 23:11:17 UTC (rev 1372)
+++ trunk/src/skrueger/i8n/PropertiesLocale.java 2011-01-19 12:59:51 UTC (rev 1373)
@@ -0,0 +1,63 @@
+package skrueger.i8n;
+
+import java.util.HashMap;
+import java.util.Locale;
+
+/**
+ */
+public class PropertiesLocale {
+
+ /**
+ * Language names => Translations
+ */
+ HashMap<String, String> displayLanguages = new HashMap<String, String>();
+
+ private final Locale locale;
+
+ private final String nativeName;
+
+ private final Locale parentLocale;
+
+ public PropertiesLocale(Locale locale, Locale parentLocale,
+ String nativeName) {
+ if (I8NUtil.isValidISOLangCode(locale.getLanguage()))
+ throw new IllegalArgumentException(
+ locale.getLanguage()
+ + " can not be defined as a PropertiesLocale langauge, since it is an existing ISO language code.");
+
+ this.parentLocale = parentLocale;
+ this.locale = locale;
+ this.nativeName = nativeName;
+ }
+
+ public String getDisplayLanguage(String langCode) {
+ if (langCode == null || displayLanguages.containsKey(langCode))
+ return displayLanguages.get(langCode);
+ else
+ return nativeName;
+ }
+
+ public String getDisplayLanguage() {
+ return getDisplayLanguage(Locale.getDefault().getLanguage());
+ }
+
+ public void setDisplayLanguages(String langCode, String languageName) {
+ displayLanguages.put(langCode, languageName);
+ }
+
+ public String getLanguage() {
+ return locale.getLanguage();
+ }
+
+ public String getCountry() {
+ return locale.getCountry();
+ }
+
+ public String getDisplayCountry() {
+ return locale.getDisplayCountry();
+ }
+
+ public Locale getParentLocale() {
+ return parentLocale;
+ }
+}
Property changes on: trunk/src/skrueger/i8n/PropertiesLocale.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id URL
Name: svn:eol-style
+ native
Modified: trunk/src/skrueger/i8n/Translation.java
===================================================================
--- trunk/src/skrueger/i8n/Translation.java 2011-01-17 23:11:17 UTC (rev 1372)
+++ trunk/src/skrueger/i8n/Translation.java 2011-01-19 12:59:51 UTC (rev 1373)
@@ -94,7 +94,7 @@
* informed if any of the translations changed. TODO: Should be registerable
* for specific languages
*/
- private WeakHashSet<ActionListener> changeListeners = new WeakHashSet<ActionListener>(
+ private final WeakHashSet<ActionListener> changeListeners = new WeakHashSet<ActionListener>(
ActionListener.class);
static {
@@ -150,8 +150,14 @@
}
if (!I8NUtil.isValidISOLangCode(newLang)) {
- throw new IllegalArgumentException("'" + newLang
- + "' is not a valid ISO language code.");
+
+ if (!I8NUtil.isPropertiesLanguage(newLang)) {
+ throw new IllegalArgumentException(
+ "'"
+ + newLang
+ + "' is not a valid ISO language code, nor is it a valid userdefined Properties code.");
+ }
+
}
Translation.activeLang = newLang;
@@ -532,6 +538,11 @@
if (locale == null)
return;
+ if (I8NUtil.isPropertiesLanguage(locale.getLanguage())) {
+ locale = I8NUtil.propLocales.get(locale.getLanguage())
+ .getParentLocale();
+ }
+
if (Locale.getDefault().equals(locale))
return;
Locale.setDefault(locale);
Modified: trunk/src_junit/skrueger/i8n/I8NUtilTest.java
===================================================================
--- trunk/src_junit/skrueger/i8n/I8NUtilTest.java 2011-01-17 23:11:17 UTC (rev 1372)
+++ trunk/src_junit/skrueger/i8n/I8NUtilTest.java 2011-01-19 12:59:51 UTC (rev 1373)
@@ -14,6 +14,7 @@
import org.junit.Test;
import schmitzm.junit.TestingClass;
+
public class I8NUtilTest extends TestingClass {
final static String oneLineCoded = "de{Baum}en{tree}";
@@ -67,6 +68,10 @@
Set<String> languageCodes = I8NUtil.getLanguageCodes();
assertTrue(languageCodes.contains("kj"));
+
+ assertTrue(languageCodes.contains("x1"));
+ assertTrue(languageCodes.contains("x2"));
+
}
@Test
@@ -75,4 +80,29 @@
assertNotNull(tjLocaleSelfmade);
System.out.println(tjLocaleSelfmade);
}
+
+ @Test
+ public void testGetPropertiesLanguages() {
+ Set<String> propertiesLocales = I8NUtil.getPropertiesLanguages();
+ String o1 = propertiesLocales.iterator().next();
+
+ PropertiesLocale po1 = I8NUtil.propLocales.get(o1);
+
+ assertEquals("Namibia", po1.getDisplayCountry());
+ assertEquals("x2", po1.getLanguage());
+ assertEquals("Oshindonga", po1.getDisplayLanguage());
+ assertEquals("Oshindonga",
+ po1.getDisplayLanguage(Locale.GERMAN.getLanguage()));
+ assertEquals("Oshindonga",
+ po1.getDisplayLanguage(Locale.ENGLISH.getLanguage()));
+
+ Translation t = new Translation();
+ t.put(po1.getLanguage(), "native");
+ t.put("en", "english");
+
+ Translation.setActiveLang("en");
+ assertEquals("english", t.toString());
+ Translation.setActiveLang(po1.getLanguage());
+ assertEquals("native", t.toString());
+ }
}
More information about the Schmitzm-commits
mailing list