/** 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);
}
}