X-Git-Url: https://git.pterodactylus.net/?p=jkeytool.git;a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Futil%2Fi18n%2FI18n.java;fp=src%2Fnet%2Fpterodactylus%2Futil%2Fi18n%2FI18n.java;h=28d174a66ed13b3ab2dd59fd43f869b929c97c08;hp=e6f30b90c89b02cb64a8c2fef6f54ab8e14e3bd2;hb=35bfd61e813bd2545c290ef6decdb987cbc9979d;hpb=e9054ed5d01d2726743059bfef19b349edd5720e diff --git a/src/net/pterodactylus/util/i18n/I18n.java b/src/net/pterodactylus/util/i18n/I18n.java index e6f30b9..28d174a 100644 --- a/src/net/pterodactylus/util/i18n/I18n.java +++ b/src/net/pterodactylus/util/i18n/I18n.java @@ -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 i18nables = new ArrayList(); + private final List i18nables = new ArrayList(); /** 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 null 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 - * true to notify registered {@link I18nable}s - * after the language was changed + * true 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); } }