make the GUI update its texts when a new language is chosen
[jSite2.git] / src / net / pterodactylus / jsite / i18n / I18n.java
index 4a244c5..0acdced 100644 (file)
@@ -24,6 +24,9 @@ import java.awt.event.KeyEvent;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Field;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.Properties;
@@ -64,20 +67,31 @@ public class I18n {
                setLocale(Locale.getDefault());
        }
 
+       /** 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.
+        * Returns the translated value for a key. The translated values may contain
+        * placeholders that are replaced with the given parameters.
         * 
+        * @see MessageFormat
         * @param key
         *            The key to get
+        * @param parameters
+        *            The parameters in case the translated value contains
+        *            placeholders
         * @return The translated message, or the key itself if no translation could
         *         be found
         */
-       public static String get(String key) {
+       public static String get(String key, Object... parameters) {
                String value = null;
                value = currentLanguage.getProperty(key);
                if (value == null) {
                        return key;
                }
+               if ((parameters != null) && (parameters.length > 0)) {
+                       return MessageFormat.format(value, parameters);
+               }
                return value;
        }
 
@@ -136,7 +150,6 @@ public class I18n {
                                if (keyToken.startsWith("VK_")) {
                                        try {
                                                Field field = KeyEvent.class.getField(keyToken);
-                                               System.out.println("asked for: “" + key + "”, will return " + field.getInt(null));
                                                return KeyStroke.getKeyStroke(field.getInt(null), modifierMask);
                                        } catch (SecurityException e) {
                                                /* ignore. */
@@ -148,7 +161,6 @@ public class I18n {
                                                /* ignore. */
                                        }
                                }
-                               System.out.println("asked for: “" + key + "”, will return " + keyToken.charAt(0));
                                return KeyStroke.getKeyStroke(keyToken.charAt(0), modifierMask);
                        }
                }
@@ -169,6 +181,7 @@ public class I18n {
                        inputStream = I18n.class.getResourceAsStream("jSite_" + currentLocale.toString() + ".properties");
                        if (inputStream != null) {
                                currentLanguage.load(inputStream);
+                               notifyI18nables();
                        }
                } catch (MissingResourceException mre1) {
                        currentLocale = Locale.ENGLISH;
@@ -178,4 +191,39 @@ public class I18n {
                        Closer.close(inputStream);
                }
        }
+
+       /**
+        * Registers the given I18nable to be updated when the language is changed.
+        * 
+        * @param i18nable
+        *            The i18nable to register
+        */
+       public static void registerI18nable(I18nable i18nable) {
+               i18nables.add(i18nable);
+       }
+
+       /**
+        * Deregisters the given I18nable to be updated when the language is
+        * changed.
+        * 
+        * @param i18nable
+        *            The i18nable to register
+        */
+       public static void deregisterI18nable(I18nable i18nable) {
+               i18nables.remove(i18nable);
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
+        * Notifies all registered {@link I18nable}s that the language was changed.
+        */
+       private static void notifyI18nables() {
+               for (I18nable i18nable: i18nables) {
+                       i18nable.updateI18n();
+               }
+       }
+
 }