X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fi18n%2FI18n.java;h=ae6bef5043b131d814839f4f2ca240ccdfe96d0d;hb=90f3a1c8e0c85f19c82130e49d7a605cb9e55286;hp=db97d0bcdffe06a8c90ac81b9e25fb3ed61a0830;hpb=708351fe5e8c91e5bc6e6a257997b660cba51415;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/i18n/I18n.java b/src/net/pterodactylus/jsite/i18n/I18n.java index db97d0b..ae6bef5 100644 --- a/src/net/pterodactylus/jsite/i18n/I18n.java +++ b/src/net/pterodactylus/jsite/i18n/I18n.java @@ -25,6 +25,8 @@ 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; @@ -42,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 i18nables = new ArrayList(); + /** The current locale. */ private static Locale currentLocale; @@ -62,7 +67,7 @@ public class I18n { } catch (IOException e) { /* something is fucked. */ } - setLocale(Locale.getDefault()); + setLocale(Locale.getDefault(), false); } /** @@ -82,7 +87,8 @@ public class I18n { String value = null; value = currentLanguage.getProperty(key); if (value == null) { - return key; + System.err.println("please fix “" + key + "”!"); + return null; } if ((parameters != null) && (parameters.length > 0)) { return MessageFormat.format(value, parameters); @@ -100,8 +106,8 @@ public class I18n { * @return The keycode */ public static int getKey(String key) { - String value = get(key); - if (value.startsWith("VK_")) { + String value = currentLanguage.getProperty(key); + if ((value != null) && value.startsWith("VK_")) { try { Field field = KeyEvent.class.getField(value); return field.getInt(null); @@ -115,7 +121,8 @@ public class I18n { /* ignore. */ } } - return value.toUpperCase().charAt(0); + System.err.println("please fix “" + key + "”!"); + return KeyEvent.VK_UNDEFINED; } /** @@ -127,7 +134,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; } @@ -169,13 +176,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 + * true 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); + if (notify) { + notifyI18nables(); + } } } catch (MissingResourceException mre1) { currentLocale = Locale.ENGLISH; @@ -185,4 +214,60 @@ public class I18n { Closer.close(inputStream); } } + + /** + * 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 findAvailableLanguages() { + List availableLanguages = new ArrayList(); + availableLanguages.add(Locale.ENGLISH); + availableLanguages.add(Locale.GERMAN); + return availableLanguages; + } + + /** + * 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(); + } + } + }