make the GUI update its texts when a new language is chosen
[jSite2.git] / src / net / pterodactylus / jsite / i18n / I18n.java
1 /*
2  * jSite2 - I18n.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.i18n;
21
22 import java.awt.event.InputEvent;
23 import java.awt.event.KeyEvent;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.lang.reflect.Field;
27 import java.text.MessageFormat;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.MissingResourceException;
32 import java.util.Properties;
33 import java.util.StringTokenizer;
34
35 import javax.swing.KeyStroke;
36
37 import net.pterodactylus.util.io.Closer;
38
39 /**
40  * Class that handles i18n.
41  * 
42  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
43  * @version $Id$
44  */
45 public class I18n {
46
47         /** The current locale. */
48         private static Locale currentLocale;
49
50         /** The default language. */
51         private static Properties defaultLanguage;
52
53         /** The current language. */
54         private static Properties currentLanguage;
55
56         static {
57                 defaultLanguage = new Properties();
58                 InputStream inputStream = null;
59                 try {
60                         inputStream = I18n.class.getResourceAsStream("jSite.properties");
61                         if (inputStream != null) {
62                                 defaultLanguage.load(inputStream);
63                         }
64                 } catch (IOException e) {
65                         /* something is fucked. */
66                 }
67                 setLocale(Locale.getDefault());
68         }
69
70         /** List of I18nables that are notified when the language changes. */
71         private static final List<I18nable> i18nables = new ArrayList<I18nable>();
72
73         /**
74          * Returns the translated value for a key. The translated values may contain
75          * placeholders that are replaced with the given parameters.
76          * 
77          * @see MessageFormat
78          * @param key
79          *            The key to get
80          * @param parameters
81          *            The parameters in case the translated value contains
82          *            placeholders
83          * @return The translated message, or the key itself if no translation could
84          *         be found
85          */
86         public static String get(String key, Object... parameters) {
87                 String value = null;
88                 value = currentLanguage.getProperty(key);
89                 if (value == null) {
90                         return key;
91                 }
92                 if ((parameters != null) && (parameters.length > 0)) {
93                         return MessageFormat.format(value, parameters);
94                 }
95                 return value;
96         }
97
98         /**
99          * Returns the keycode from the value of the given key. You can specify the
100          * constants in {@link KeyEvent} in the properties file, e.g. VK_S for the
101          * keycode ‘s’ when used for mnemonics.
102          * 
103          * @param key
104          *            The key under which the keycode is stored
105          * @return The keycode
106          */
107         public static int getKey(String key) {
108                 String value = get(key);
109                 if (value.startsWith("VK_")) {
110                         try {
111                                 Field field = KeyEvent.class.getField(value);
112                                 return field.getInt(null);
113                         } catch (SecurityException e) {
114                                 /* ignore. */
115                         } catch (NoSuchFieldException e) {
116                                 /* ignore. */
117                         } catch (IllegalArgumentException e) {
118                                 /* ignore. */
119                         } catch (IllegalAccessException e) {
120                                 /* ignore. */
121                         }
122                 }
123                 return value.toUpperCase().charAt(0);
124         }
125
126         /**
127          * Returns a key stroke for use with swing accelerators.
128          * 
129          * @param key
130          *            The key of the key stroke
131          * @return The key stroke, or <code>null</code> if no key stroke could be
132          *         created from the translated value
133          */
134         public static KeyStroke getKeyStroke(String key) {
135                 String value = get(key);
136                 if (value == null) {
137                         return null;
138                 }
139                 StringTokenizer keyTokens = new StringTokenizer(value, "+- ");
140                 int modifierMask = 0;
141                 while (keyTokens.hasMoreTokens()) {
142                         String keyToken = keyTokens.nextToken();
143                         if ("ctrl".equalsIgnoreCase(keyToken)) {
144                                 modifierMask |= InputEvent.CTRL_DOWN_MASK;
145                         } else if ("alt".equalsIgnoreCase(keyToken)) {
146                                 modifierMask |= InputEvent.ALT_DOWN_MASK;
147                         } else if ("shift".equalsIgnoreCase(keyToken)) {
148                                 modifierMask |= InputEvent.SHIFT_DOWN_MASK;
149                         } else {
150                                 if (keyToken.startsWith("VK_")) {
151                                         try {
152                                                 Field field = KeyEvent.class.getField(keyToken);
153                                                 return KeyStroke.getKeyStroke(field.getInt(null), modifierMask);
154                                         } catch (SecurityException e) {
155                                                 /* ignore. */
156                                         } catch (NoSuchFieldException e) {
157                                                 /* ignore. */
158                                         } catch (IllegalArgumentException e) {
159                                                 /* ignore. */
160                                         } catch (IllegalAccessException e) {
161                                                 /* ignore. */
162                                         }
163                                 }
164                                 return KeyStroke.getKeyStroke(keyToken.charAt(0), modifierMask);
165                         }
166                 }
167                 return null;
168         }
169
170         /**
171          * Sets the current locale.
172          * 
173          * @param newLocale
174          *            The new locale to use
175          */
176         public static void setLocale(Locale newLocale) {
177                 currentLocale = newLocale;
178                 InputStream inputStream = null;
179                 try {
180                         currentLanguage = new Properties(defaultLanguage);
181                         inputStream = I18n.class.getResourceAsStream("jSite_" + currentLocale.toString() + ".properties");
182                         if (inputStream != null) {
183                                 currentLanguage.load(inputStream);
184                                 notifyI18nables();
185                         }
186                 } catch (MissingResourceException mre1) {
187                         currentLocale = Locale.ENGLISH;
188                 } catch (IOException ioe1) {
189                         currentLocale = Locale.ENGLISH;
190                 } finally {
191                         Closer.close(inputStream);
192                 }
193         }
194
195         /**
196          * Registers the given I18nable to be updated when the language is changed.
197          * 
198          * @param i18nable
199          *            The i18nable to register
200          */
201         public static void registerI18nable(I18nable i18nable) {
202                 i18nables.add(i18nable);
203         }
204
205         /**
206          * Deregisters the given I18nable to be updated when the language is
207          * changed.
208          * 
209          * @param i18nable
210          *            The i18nable to register
211          */
212         public static void deregisterI18nable(I18nable i18nable) {
213                 i18nables.remove(i18nable);
214         }
215
216         //
217         // PRIVATE METHODS
218         //
219
220         /**
221          * Notifies all registered {@link I18nable}s that the language was changed.
222          */
223         private static void notifyI18nables() {
224                 for (I18nable i18nable: i18nables) {
225                         i18nable.updateI18n();
226                 }
227         }
228
229 }