Make I18n class non-static.
[jkeytool.git] / src / net / pterodactylus / util / i18n / I18n.java
index e6f30b9..28d174a 100644 (file)
@@ -49,23 +49,39 @@ public class I18n {
        /** Logger. */
        private static final Logger logger = Logging.getLogger(I18n.class.getName());
 
+       /** The base name of the resource files. */
+       private String resourceName;
+
+       /** The class whose class loader is used to load resource files. */
+       private final Class<?> resourceClass;
+
        /** List of I18nables that are notified when the language changes. */
-       private static final List<I18nable> i18nables = new ArrayList<I18nable>();
+       private final List<I18nable> i18nables = new ArrayList<I18nable>();
 
        /** The current locale. */
-       private static Locale currentLocale;
+       private Locale currentLocale;
 
        /** The default language. */
-       private static Properties defaultLanguage;
+       private Properties defaultLanguage;
 
        /** The current language. */
-       private static Properties currentLanguage;
+       private Properties currentLanguage;
 
-       static {
+       /**
+        * Creates a new I18n container.
+        *
+        * @param resourceName
+        *            The base name of the language resource files
+        * @param resourceClass
+        *            The class whose class loader is used to load resource files
+        */
+       public I18n(String resourceName, Class<?> resourceClass) {
+               this.resourceName = resourceName;
+               this.resourceClass = resourceClass;
                defaultLanguage = new Properties();
                InputStream inputStream = null;
                try {
-                       inputStream = I18n.class.getResourceAsStream("jkeytool.properties");
+                       inputStream = resourceClass.getResourceAsStream(resourceName + ".properties");
                        if (inputStream != null) {
                                defaultLanguage.load(inputStream);
                        }
@@ -88,7 +104,7 @@ public class I18n {
         * @return The translated message, or the key itself if no translation could
         *         be found
         */
-       public static String get(String key, Object... parameters) {
+       public String get(String key, Object... parameters) {
                String value = null;
                value = currentLanguage.getProperty(key);
                if (value == null) {
@@ -111,7 +127,7 @@ public class I18n {
         *            The key under which the keycode is stored
         * @return The keycode
         */
-       public static int getKey(String key) {
+       public int getKey(String key) {
                String value = currentLanguage.getProperty(key);
                if ((value != null) && value.startsWith("VK_")) {
                        try {
@@ -139,7 +155,7 @@ public class I18n {
         * @return The key stroke, or <code>null</code> if no key stroke could be
         *         created from the translated value
         */
-       public static KeyStroke getKeyStroke(String key) {
+       public KeyStroke getKeyStroke(String key) {
                String value = currentLanguage.getProperty(key);
                if (value == null) {
                        return null;
@@ -184,7 +200,7 @@ public class I18n {
         * @param newLocale
         *            The new locale to use
         */
-       public static void setLocale(Locale newLocale) {
+       public void setLocale(Locale newLocale) {
                setLocale(newLocale, true);
        }
 
@@ -194,10 +210,10 @@ public class I18n {
         * @param newLocale
         *            The new locale to use
         * @param notify
-        *            <code>true</code> to notify registered {@link I18nable}s
-        *            after the language was changed
+        *            <code>true</code> to notify registered {@link I18nable}s after
+        *            the language was changed
         */
-       private static void setLocale(Locale newLocale, boolean notify) {
+       private void setLocale(Locale newLocale, boolean notify) {
                currentLocale = newLocale;
                InputStream inputStream = null;
                try {
@@ -208,7 +224,7 @@ public class I18n {
                                }
                                return;
                        }
-                       inputStream = I18n.class.getResourceAsStream("jSite_" + newLocale.getLanguage() + ".properties");
+                       inputStream = resourceClass.getResourceAsStream(resourceName + "_" + newLocale.getLanguage() + ".properties");
                        if (inputStream != null) {
                                currentLanguage.load(inputStream);
                                if (notify) {
@@ -229,7 +245,7 @@ public class I18n {
         *
         * @return The current locale
         */
-       public static Locale getLocale() {
+       public Locale getLocale() {
                return currentLocale;
        }
 
@@ -251,7 +267,7 @@ public class I18n {
         * @param i18nable
         *            The i18nable to register
         */
-       public static void registerI18nable(I18nable i18nable) {
+       public void registerI18nable(I18nable i18nable) {
                i18nables.add(i18nable);
        }
 
@@ -262,7 +278,7 @@ public class I18n {
         * @param i18nable
         *            The i18nable to register
         */
-       public static void deregisterI18nable(I18nable i18nable) {
+       public void deregisterI18nable(I18nable i18nable) {
                i18nables.remove(i18nable);
        }
 
@@ -273,9 +289,9 @@ public class I18n {
        /**
         * Notifies all registered {@link I18nable}s that the language was changed.
         */
-       private static void notifyI18nables() {
-               for (I18nable i18nable: i18nables) {
-                       i18nable.updateI18n();
+       private void notifyI18nables() {
+               for (I18nable i18nable : i18nables) {
+                       i18nable.updateI18n(this);
                }
        }