X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fde%2Ftodesbaum%2Fjsite%2Fi18n%2FI18n.java;h=1194473fcbdcda2e3d686fd8c703e6c4b813d67b;hb=efb64e003c7c2df5317566bbf3fd01e62162f1dc;hp=38a6916be80761235fd692d9488e129fd90e6381;hpb=6f1a8216cfba28add0ef365b46a08d16d4eb87fe;p=jSite.git diff --git a/src/de/todesbaum/jsite/i18n/I18n.java b/src/de/todesbaum/jsite/i18n/I18n.java index 38a6916..1194473 100644 --- a/src/de/todesbaum/jsite/i18n/I18n.java +++ b/src/de/todesbaum/jsite/i18n/I18n.java @@ -20,37 +20,85 @@ package de.todesbaum.jsite.i18n; import java.util.Locale; +import java.util.MissingResourceException; import java.util.ResourceBundle; /** - * @author David Roden <droden@gmail.com> - * @version $Id: I18n.java 355 2006-03-24 15:04:11Z bombe $ + * Maps i18n keys to translated texts, depending on a current locale. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ public class I18n { + /** The default locale, English. */ + private static Locale defaultLocale = new Locale("en"); + + /** The current locale. */ private static Locale currentLocale; + /** + * Returns the currently set locale. + * + * @return The current locale + */ public static Locale getLocale() { - if (currentLocale == null) + if (currentLocale == null) { currentLocale = Locale.getDefault(); + } return currentLocale; } + /** + * Sets the current locale. + * + * @param locale + * The new current locale + */ public static void setLocale(Locale locale) { currentLocale = locale; Locale.setDefault(locale); } + /** + * Returns the resource bundle for the current locale. + * + * @return The resource bundle for the current locale + */ public static ResourceBundle getResourceBundle() { return getResourceBundle(getLocale()); } + /** + * Returns the resource bundle for the given locale. + * + * @param locale + * The locale to get the resource bundle for + * @return The resource bundle for the given locale + */ public static ResourceBundle getResourceBundle(Locale locale) { return ResourceBundle.getBundle("de.todesbaum.jsite.i18n.jSite", locale); } + /** + * Retrieves a translated text for the given i18n key. If the resource + * bundle for the current locale does not have a translation for the given + * key, the default locale is tried. If that fails, the key is returned. + * + * @param key + * The key to get the translation for + * @return The translated value, or the key itself if not translation can be + * found + */ public static String getMessage(String key) { - return getResourceBundle().getString(key); + try { + return getResourceBundle().getString(key); + } catch (MissingResourceException mre1) { + try { + return getResourceBundle(defaultLocale).getString(key); + } catch (MissingResourceException mre2) { + return key; + } + } } }