X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fi18n%2FI18n.java;h=0acdced5cfbf0d64d9086dddb3883f05821868e0;hb=b71f4f4aeb7440b88dd82a252cd296506427c440;hp=db97d0bcdffe06a8c90ac81b9e25fb3ed61a0830;hpb=d4c6cfca50b43ce26f937132e046d54830dd6daf;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/i18n/I18n.java b/src/net/pterodactylus/jsite/i18n/I18n.java index db97d0b..0acdced 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; @@ -65,6 +67,9 @@ public class I18n { setLocale(Locale.getDefault()); } + /** List of I18nables that are notified when the language changes. */ + private static final List i18nables = new ArrayList(); + /** * Returns the translated value for a key. The translated values may contain * placeholders that are replaced with the given parameters. @@ -176,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; @@ -185,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(); + } + } + }