From 35bfd61e813bd2545c290ef6decdb987cbc9979d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 28 Jan 2009 00:27:51 +0100 Subject: [PATCH] Make I18n class non-static. --- src/net/pterodactylus/util/i18n/I18n.java | 56 ++++++++++++++-------- src/net/pterodactylus/util/i18n/I18nable.java | 7 ++- .../pterodactylus/util/i18n/gui/I18nAction.java | 39 ++++++++------- src/net/pterodactylus/util/i18n/gui/I18nLabel.java | 40 +++++++++------- src/net/pterodactylus/util/i18n/gui/I18nMenu.java | 16 ++++--- 5 files changed, 97 insertions(+), 61 deletions(-) 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); } } diff --git a/src/net/pterodactylus/util/i18n/I18nable.java b/src/net/pterodactylus/util/i18n/I18nable.java index c32fda6..59c5877 100644 --- a/src/net/pterodactylus/util/i18n/I18nable.java +++ b/src/net/pterodactylus/util/i18n/I18nable.java @@ -21,14 +21,17 @@ package net.pterodactylus.util.i18n; /** * Interface for objects that want to be notified when the language is changed. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public interface I18nable { /** * Notifies the object that the language in {@link I18n} was changed. + * + * @param i18n + * The i18n container that is updated */ - public void updateI18n(); + public void updateI18n(I18n i18n); } diff --git a/src/net/pterodactylus/util/i18n/gui/I18nAction.java b/src/net/pterodactylus/util/i18n/gui/I18nAction.java index 6078335..0682b35 100644 --- a/src/net/pterodactylus/util/i18n/gui/I18nAction.java +++ b/src/net/pterodactylus/util/i18n/gui/I18nAction.java @@ -1,4 +1,3 @@ - package net.pterodactylus.util.i18n.gui; import javax.swing.AbstractAction; @@ -22,43 +21,51 @@ public abstract class I18nAction extends AbstractAction implements I18nable { * Creates a new action that uses the given name as base name to get values * from {@link I18n}. * + * @param i18n + * The I18n container * @param i18nName * The base name of the action */ - public I18nAction(String i18nName) { - this(i18nName, null); + public I18nAction(I18n i18n, String i18nName) { + this(i18n, i18nName, null); } /** * Creates a new action that uses the given name as base name to get values * from {@link I18n} and the given icon. * + * @param i18n + * The I18n container * @param i18nName * The base name of the action * @param icon * The icon for the action */ - public I18nAction(String i18nName, Icon icon) { - this(i18nName, true, icon); + public I18nAction(I18n i18n, String i18nName, Icon icon) { + this(i18n, i18nName, true, icon); } /** * Creates a new action that uses the given name as base name to get values * from {@link I18n}. * + * @param i18n + * The I18n container * @param i18nName * The base name of the action * @param enabled * Whether the action should be enabled */ - public I18nAction(String i18nName, boolean enabled) { - this(i18nName, enabled, null); + public I18nAction(I18n i18n, String i18nName, boolean enabled) { + this(i18n, i18nName, enabled, null); } /** * Creates a new action that uses the given name as base name to get values * from {@link I18n} and the given icon. * + * @param i18n + * The I18n container * @param i18nName * The base name of the action * @param enabled @@ -66,24 +73,24 @@ public abstract class I18nAction extends AbstractAction implements I18nable { * @param icon * The icon for the action */ - public I18nAction(String i18nName, boolean enabled, Icon icon) { + public I18nAction(I18n i18n, String i18nName, boolean enabled, Icon icon) { this.i18nName = i18nName; if (icon != null) { putValue(Action.SMALL_ICON, icon); } setEnabled(enabled); - updateI18n(); + updateI18n(i18n); } /** * {@inheritDoc} */ - public void updateI18n() { - putValue(Action.NAME, I18n.get(i18nName + ".name")); - putValue(Action.MNEMONIC_KEY, I18n.getKey(i18nName + ".mnemonic")); - putValue(Action.ACCELERATOR_KEY, I18n.getKeyStroke(i18nName + ".accelerator")); - putValue(Action.SHORT_DESCRIPTION, I18n.get(i18nName + ".shortDescription")); - putValue(Action.LONG_DESCRIPTION, I18n.get(i18nName + ".longDescription")); + public void updateI18n(I18n i18n) { + putValue(Action.NAME, i18n.get(i18nName + ".name")); + putValue(Action.MNEMONIC_KEY, i18n.getKey(i18nName + ".mnemonic")); + putValue(Action.ACCELERATOR_KEY, i18n.getKeyStroke(i18nName + ".accelerator")); + putValue(Action.SHORT_DESCRIPTION, i18n.get(i18nName + ".shortDescription")); + putValue(Action.LONG_DESCRIPTION, i18n.get(i18nName + ".longDescription")); } -} \ No newline at end of file +} diff --git a/src/net/pterodactylus/util/i18n/gui/I18nLabel.java b/src/net/pterodactylus/util/i18n/gui/I18nLabel.java index 32ad57d..a42e626 100644 --- a/src/net/pterodactylus/util/i18n/gui/I18nLabel.java +++ b/src/net/pterodactylus/util/i18n/gui/I18nLabel.java @@ -28,7 +28,7 @@ import net.pterodactylus.util.i18n.I18nable; /** * Label that can update itself from {@link I18n}. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public class I18nLabel extends JLabel implements I18nable { @@ -41,18 +41,22 @@ public class I18nLabel extends JLabel implements I18nable { /** * Creates a new label with the given I18n basename. - * + * + * @param i18n + * The I18n container * @param i18nBasename * The I18n basename of the label */ - public I18nLabel(String i18nBasename) { - this(i18nBasename, (Component) null); + public I18nLabel(I18n i18n, String i18nBasename) { + this(i18n, i18nBasename, (Component) null); } /** * Creates a new label with the given I18n basename that optionally is a * label for the given component. - * + * + * @param i18n + * The I18n container * @param i18nBasename * The I18n basename of the label * @param component @@ -60,28 +64,32 @@ public class I18nLabel extends JLabel implements I18nable { * null if this label should not activate a * component */ - public I18nLabel(String i18nBasename, Component component) { - this(i18nBasename, component, (Object[]) null); + public I18nLabel(I18n i18n, String i18nBasename, Component component) { + this(i18n, i18nBasename, component, (Object[]) null); } /** * Creates a new label with the given I18n basename that optionally is a * label for the given component. - * + * + * @param i18n + * The I18n container * @param i18nBasename * The I18n basename of the label * @param arguments * Optional arguments that are handed in to * {@link I18n#get(String, Object...)} */ - public I18nLabel(String i18nBasename, Object... arguments) { - this(i18nBasename, null, arguments); + public I18nLabel(I18n i18n, String i18nBasename, Object... arguments) { + this(i18n, i18nBasename, null, arguments); } /** * Creates a new label with the given I18n basename that optionally is a * label for the given component. - * + * + * @param i18n + * The I18n container * @param i18nBasename * The I18n basename of the label * @param component @@ -92,23 +100,23 @@ public class I18nLabel extends JLabel implements I18nable { * Optional arguments that are handed in to * {@link I18n#get(String, Object...)} */ - public I18nLabel(String i18nBasename, Component component, Object... arguments) { + public I18nLabel(I18n i18n, String i18nBasename, Component component, Object... arguments) { super(); this.i18nBasename = i18nBasename; this.arguments = arguments; if (component != null) { setLabelFor(component); } - updateI18n(); + updateI18n(i18n); } /** * {@inheritDoc} */ - public void updateI18n() { - setText(I18n.get(i18nBasename + ".name", arguments)); + public void updateI18n(I18n i18n) { + setText(i18n.get(i18nBasename + ".name", arguments)); if (getLabelFor() != null) { - setDisplayedMnemonic(I18n.getKey(i18nBasename + ".mnemonic")); + setDisplayedMnemonic(i18n.getKey(i18nBasename + ".mnemonic")); } } diff --git a/src/net/pterodactylus/util/i18n/gui/I18nMenu.java b/src/net/pterodactylus/util/i18n/gui/I18nMenu.java index 993be97..e47ec1c 100644 --- a/src/net/pterodactylus/util/i18n/gui/I18nMenu.java +++ b/src/net/pterodactylus/util/i18n/gui/I18nMenu.java @@ -26,7 +26,7 @@ import net.pterodactylus.util.i18n.I18nable; /** * Menu that receives its properties from {@link I18n}. - * + * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public class I18nMenu extends JMenu implements I18nable { @@ -36,13 +36,15 @@ public class I18nMenu extends JMenu implements I18nable { /** * Creates a new menu with the given {@link I18n} basename. - * + * + * @param i18n + * The I18n container * @param i18nBasename * The basename of the {@link I18n} properties */ - public I18nMenu(String i18nBasename) { + public I18nMenu(I18n i18n, String i18nBasename) { this.i18nBasename = i18nBasename; - updateI18n(); + updateI18n(i18n); } // @@ -52,9 +54,9 @@ public class I18nMenu extends JMenu implements I18nable { /** * {@inheritDoc} */ - public void updateI18n() { - setText(I18n.get(i18nBasename + ".name")); - setMnemonic(I18n.getKey(i18nBasename + ".mnemonic")); + public void updateI18n(I18n i18n) { + setText(i18n.get(i18nBasename + ".name")); + setMnemonic(i18n.getKey(i18nBasename + ".mnemonic")); } } -- 2.7.4