X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fi18n%2FI18n.java;h=90df053fb7dc641912c335b73b7b706f8792949e;hb=f212e5bc9dcc2007b080b6ba873b570de43c6b39;hp=0acdced5cfbf0d64d9086dddb3883f05821868e0;hpb=b71f4f4aeb7440b88dd82a252cd296506427c440;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/i18n/I18n.java b/src/net/pterodactylus/jsite/i18n/I18n.java index 0acdced..90df053 100644 --- a/src/net/pterodactylus/jsite/i18n/I18n.java +++ b/src/net/pterodactylus/jsite/i18n/I18n.java @@ -31,10 +31,12 @@ import java.util.Locale; import java.util.MissingResourceException; import java.util.Properties; import java.util.StringTokenizer; +import java.util.logging.Logger; import javax.swing.KeyStroke; import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.logging.Logging; /** * Class that handles i18n. @@ -44,6 +46,12 @@ import net.pterodactylus.util.io.Closer; */ public class I18n { + /** Logger. */ + private static final Logger logger = Logging.getLogger(I18n.class.getName()); + + /** List of I18nables that are notified when the language changes. */ + private static final List i18nables = new ArrayList(); + /** The current locale. */ private static Locale currentLocale; @@ -64,12 +72,9 @@ public class I18n { } catch (IOException e) { /* something is fucked. */ } - setLocale(Locale.getDefault()); + setLocale(Locale.getDefault(), false); } - /** 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. @@ -87,7 +92,9 @@ public class I18n { String value = null; value = currentLanguage.getProperty(key); if (value == null) { - return key; + logger.warning("please fix “" + key + "”!"); + /* TODO - replace with value when done! */ + return null; } if ((parameters != null) && (parameters.length > 0)) { return MessageFormat.format(value, parameters); @@ -105,8 +112,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); @@ -120,7 +127,8 @@ public class I18n { /* ignore. */ } } - return value.toUpperCase().charAt(0); + System.err.println("please fix “" + key + "”!"); + return KeyEvent.VK_UNDEFINED; } /** @@ -132,7 +140,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; } @@ -174,14 +182,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); - notifyI18nables(); + if (notify) { + notifyI18nables(); + } } } catch (MissingResourceException mre1) { currentLocale = Locale.ENGLISH; @@ -193,6 +222,27 @@ public class I18n { } /** + * 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