📄 Update year in copyright line
[jSite.git] / src / main / java / de / todesbaum / jsite / i18n / I18n.java
1 /*
2  * jSite - I18n.java - Copyright Â© 2006–2019 David Roden
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package de.todesbaum.jsite.i18n;
20
21 import java.util.Locale;
22 import java.util.MissingResourceException;
23 import java.util.ResourceBundle;
24
25 /**
26  * Maps i18n keys to translated texts, depending on a current locale.
27  *
28  * @author David â€˜Bombe’ Roden <bombe@freenetproject.org>
29  */
30 public class I18n {
31
32         /** The default locale, English. */
33         private static Locale defaultLocale = new Locale("en");
34
35         /** The current locale. */
36         private static Locale currentLocale;
37
38         /**
39          * Returns the currently set locale.
40          *
41          * @return The current locale
42          */
43         public static Locale getLocale() {
44                 if (currentLocale == null) {
45                         currentLocale = Locale.getDefault();
46                 }
47                 return currentLocale;
48         }
49
50         /**
51          * Sets the current locale.
52          *
53          * @param locale
54          *            The new current locale
55          */
56         public static void setLocale(Locale locale) {
57                 currentLocale = locale;
58                 Locale.setDefault(locale);
59         }
60
61         /**
62          * Returns the resource bundle for the current locale.
63          *
64          * @return The resource bundle for the current locale
65          */
66         public static ResourceBundle getResourceBundle() {
67                 return getResourceBundle(getLocale());
68         }
69
70         /**
71          * Returns the resource bundle for the given locale.
72          *
73          * @param locale
74          *            The locale to get the resource bundle for
75          * @return The resource bundle for the given locale
76          */
77         public static ResourceBundle getResourceBundle(Locale locale) {
78                 return ResourceBundle.getBundle("de.todesbaum.jsite.i18n.jSite", locale);
79         }
80
81         /**
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.
85          *
86          * @param key
87          *            The key to get the translation for
88          * @return The translated value, or the key itself if not translation can be
89          *         found
90          */
91         public static String getMessage(String key) {
92                 try {
93                         return getResourceBundle().getString(key);
94                 } catch (MissingResourceException mre1) {
95                         try {
96                                 return getResourceBundle(defaultLocale).getString(key);
97                         } catch (MissingResourceException mre2) {
98                                 return key;
99                         }
100                 }
101         }
102
103 }