/** 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);
}
* @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) {
* 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 {
* @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;
* @param newLocale
* The new locale to use
*/
- public static void setLocale(Locale newLocale) {
+ public void setLocale(Locale newLocale) {
setLocale(newLocale, true);
}
* @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 {
}
return;
}
- inputStream = I18n.class.getResourceAsStream("jSite_" + newLocale.getLanguage() + ".properties");
+ inputStream = resourceClass.getResourceAsStream(resourceName + "_" + newLocale.getLanguage() + ".properties");
if (inputStream != null) {
currentLanguage.load(inputStream);
if (notify) {
*
* @return The current locale
*/
- public static Locale getLocale() {
+ public Locale getLocale() {
return currentLocale;
}
* @param i18nable
* The i18nable to register
*/
- public static void registerI18nable(I18nable i18nable) {
+ public void registerI18nable(I18nable i18nable) {
i18nables.add(i18nable);
}
* @param i18nable
* The i18nable to register
*/
- public static void deregisterI18nable(I18nable i18nable) {
+ public void deregisterI18nable(I18nable i18nable) {
i18nables.remove(i18nable);
}
/**
* 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);
}
}
/**
* 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);
}
-
package net.pterodactylus.util.i18n.gui;
import javax.swing.AbstractAction;
* 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
* @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
+}
/**
* Label that can update itself from {@link I18n}.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
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
* <code>null</code> 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
* 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"));
}
}
/**
* Menu that receives its properties from {@link I18n}.
- *
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
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);
}
//
/**
* {@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"));
}
}