add language menu
[jSite2.git] / src / net / pterodactylus / jsite / i18n / I18n.java
index 0acdced..504dc88 100644 (file)
@@ -44,6 +44,9 @@ import net.pterodactylus.util.io.Closer;
  */
 public class I18n {
 
+       /** List of I18nables that are notified when the language changes. */
+       private static final List<I18nable> i18nables = new ArrayList<I18nable>();
+
        /** The current locale. */
        private static Locale currentLocale;
 
@@ -64,12 +67,9 @@ public class I18n {
                } catch (IOException e) {
                        /* something is fucked. */
                }
-               setLocale(Locale.getDefault());
+               setLocale(Locale.getDefault(), false);
        }
 
-       /** List of I18nables that are notified when the language changes. */
-       private static final List<I18nable> i18nables = new ArrayList<I18nable>();
-
        /**
         * Returns the translated value for a key. The translated values may contain
         * placeholders that are replaced with the given parameters.
@@ -105,7 +105,10 @@ public class I18n {
         * @return The keycode
         */
        public static int getKey(String key) {
-               String value = get(key);
+               String value = currentLanguage.getProperty(key);
+               if (value == null) {
+                       return -1;
+               }
                if (value.startsWith("VK_")) {
                        try {
                                Field field = KeyEvent.class.getField(value);
@@ -132,7 +135,7 @@ public class I18n {
         *         created from the translated value
         */
        public static KeyStroke getKeyStroke(String key) {
-               String value = get(key);
+               String value = currentLanguage.getProperty(key);
                if (value == null) {
                        return null;
                }
@@ -174,14 +177,35 @@ public class I18n {
         *            The new locale to use
         */
        public static void setLocale(Locale newLocale) {
+               setLocale(newLocale, true);
+       }
+
+       /**
+        * Sets the current locale.
+        * 
+        * @param newLocale
+        *            The new locale to use
+        * @param notify
+        *            <code>true</code> to notify registered {@link I18nable}s
+        *            after the language was changed
+        */
+       private static void setLocale(Locale newLocale, boolean notify) {
                currentLocale = newLocale;
                InputStream inputStream = null;
                try {
                        currentLanguage = new Properties(defaultLanguage);
-                       inputStream = I18n.class.getResourceAsStream("jSite_" + currentLocale.toString() + ".properties");
+                       if (newLocale == Locale.ENGLISH) {
+                               if (notify) {
+                                       notifyI18nables();
+                               }
+                               return;
+                       }
+                       inputStream = I18n.class.getResourceAsStream("jSite_" + newLocale.getLanguage() + ".properties");
                        if (inputStream != null) {
                                currentLanguage.load(inputStream);
-                               notifyI18nables();
+                               if (notify) {
+                                       notifyI18nables();
+                               }
                        }
                } catch (MissingResourceException mre1) {
                        currentLocale = Locale.ENGLISH;
@@ -193,6 +217,27 @@ public class I18n {
        }
 
        /**
+        * Returns the current locale.
+        * 
+        * @return The current locale
+        */
+       public static Locale getLocale() {
+               return currentLocale;
+       }
+
+       /**
+        * Finds all available locales.
+        * 
+        * @return All available locales
+        */
+       public static List<Locale> findAvailableLanguages() {
+               List<Locale> availableLanguages = new ArrayList<Locale>();
+               availableLanguages.add(Locale.ENGLISH);
+               availableLanguages.add(Locale.GERMAN);
+               return availableLanguages;
+       }
+
+       /**
         * Registers the given I18nable to be updated when the language is changed.
         * 
         * @param i18nable