2 * jSite - I18n.java - Copyright © 2006–2012 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.i18n;
21 import java.util.Locale;
22 import java.util.MissingResourceException;
23 import java.util.ResourceBundle;
26 * Maps i18n keys to translated texts, depending on a current locale.
28 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32 /** The default locale, English. */
33 private static Locale defaultLocale = new Locale("en");
35 /** The current locale. */
36 private static Locale currentLocale;
39 * Returns the currently set locale.
41 * @return The current locale
43 public static Locale getLocale() {
44 if (currentLocale == null) {
45 currentLocale = Locale.getDefault();
51 * Sets the current locale.
54 * The new current locale
56 public static void setLocale(Locale locale) {
57 currentLocale = locale;
58 Locale.setDefault(locale);
62 * Returns the resource bundle for the current locale.
64 * @return The resource bundle for the current locale
66 public static ResourceBundle getResourceBundle() {
67 return getResourceBundle(getLocale());
71 * Returns the resource bundle for the given locale.
74 * The locale to get the resource bundle for
75 * @return The resource bundle for the given locale
77 public static ResourceBundle getResourceBundle(Locale locale) {
78 return ResourceBundle.getBundle("de.todesbaum.jsite.i18n.jSite", locale);
82 * Retrieves a translated text for the given i18n key. If the resource
83 * bundle for the current locale does not have a translation for the given
84 * key, the default locale is tried. If that fails, the key is returned.
87 * The key to get the translation for
88 * @return The translated value, or the key itself if not translation can be
91 public static String getMessage(String key) {
93 return getResourceBundle().getString(key);
94 } catch (MissingResourceException mre1) {
96 return getResourceBundle(defaultLocale).getString(key);
97 } catch (MissingResourceException mre2) {