3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.jsite.i18n;
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;
35 import javax.swing.KeyStroke;
37 import net.pterodactylus.util.io.Closer;
40 * Class that handles i18n.
42 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
47 /** The current locale. */
48 private static Locale currentLocale;
50 /** The default language. */
51 private static Properties defaultLanguage;
53 /** The current language. */
54 private static Properties currentLanguage;
57 defaultLanguage = new Properties();
58 InputStream inputStream = null;
60 inputStream = I18n.class.getResourceAsStream("jSite.properties");
61 if (inputStream != null) {
62 defaultLanguage.load(inputStream);
64 } catch (IOException e) {
65 /* something is fucked. */
67 setLocale(Locale.getDefault());
70 /** List of I18nables that are notified when the language changes. */
71 private static final List<I18nable> i18nables = new ArrayList<I18nable>();
74 * Returns the translated value for a key. The translated values may contain
75 * placeholders that are replaced with the given parameters.
81 * The parameters in case the translated value contains
83 * @return The translated message, or the key itself if no translation could
86 public static String get(String key, Object... parameters) {
88 value = currentLanguage.getProperty(key);
92 if ((parameters != null) && (parameters.length > 0)) {
93 return MessageFormat.format(value, parameters);
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.
104 * The key under which the keycode is stored
105 * @return The keycode
107 public static int getKey(String key) {
108 String value = get(key);
109 if (value.startsWith("VK_")) {
111 Field field = KeyEvent.class.getField(value);
112 return field.getInt(null);
113 } catch (SecurityException e) {
115 } catch (NoSuchFieldException e) {
117 } catch (IllegalArgumentException e) {
119 } catch (IllegalAccessException e) {
123 return value.toUpperCase().charAt(0);
127 * Returns a key stroke for use with swing accelerators.
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
134 public static KeyStroke getKeyStroke(String key) {
135 String value = get(key);
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;
150 if (keyToken.startsWith("VK_")) {
152 Field field = KeyEvent.class.getField(keyToken);
153 return KeyStroke.getKeyStroke(field.getInt(null), modifierMask);
154 } catch (SecurityException e) {
156 } catch (NoSuchFieldException e) {
158 } catch (IllegalArgumentException e) {
160 } catch (IllegalAccessException e) {
164 return KeyStroke.getKeyStroke(keyToken.charAt(0), modifierMask);
171 * Sets the current locale.
174 * The new locale to use
176 public static void setLocale(Locale newLocale) {
177 currentLocale = newLocale;
178 InputStream inputStream = null;
180 currentLanguage = new Properties(defaultLanguage);
181 inputStream = I18n.class.getResourceAsStream("jSite_" + currentLocale.toString() + ".properties");
182 if (inputStream != null) {
183 currentLanguage.load(inputStream);
186 } catch (MissingResourceException mre1) {
187 currentLocale = Locale.ENGLISH;
188 } catch (IOException ioe1) {
189 currentLocale = Locale.ENGLISH;
191 Closer.close(inputStream);
196 * Registers the given I18nable to be updated when the language is changed.
199 * The i18nable to register
201 public static void registerI18nable(I18nable i18nable) {
202 i18nables.add(i18nable);
206 * Deregisters the given I18nable to be updated when the language is
210 * The i18nable to register
212 public static void deregisterI18nable(I18nable i18nable) {
213 i18nables.remove(i18nable);
221 * Notifies all registered {@link I18nable}s that the language was changed.
223 private static void notifyI18nables() {
224 for (I18nable i18nable: i18nables) {
225 i18nable.updateI18n();