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.util.Locale;
28 import java.util.MissingResourceException;
29 import java.util.Properties;
30 import java.util.StringTokenizer;
32 import javax.swing.KeyStroke;
34 import net.pterodactylus.util.io.Closer;
37 * Class that handles i18n.
39 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
44 /** The current locale. */
45 private static Locale currentLocale;
47 /** The default language. */
48 private static Properties defaultLanguage;
50 /** The current language. */
51 private static Properties currentLanguage;
54 defaultLanguage = new Properties();
55 InputStream inputStream = null;
57 inputStream = I18n.class.getResourceAsStream("jSite.properties");
58 if (inputStream != null) {
59 defaultLanguage.load(inputStream);
61 } catch (IOException e) {
62 /* something is fucked. */
64 setLocale(Locale.getDefault());
68 * Returns the translated value for a key.
72 * @return The translated message, or the key itself if no translation could
75 public static String get(String key) {
77 value = currentLanguage.getProperty(key);
85 * Returns the keycode from the value of the given key. You can specify the
86 * constants in {@link KeyEvent} in the properties file, e.g. VK_S for the
87 * keycode ‘s’ when used for mnemonics.
90 * The key under which the keycode is stored
93 public static int getKey(String key) {
94 String value = get(key);
95 if (value.startsWith("VK_")) {
97 Field field = KeyEvent.class.getField(value);
98 return field.getInt(null);
99 } catch (SecurityException e) {
101 } catch (NoSuchFieldException e) {
103 } catch (IllegalArgumentException e) {
105 } catch (IllegalAccessException e) {
109 return value.toUpperCase().charAt(0);
113 * Returns a key stroke for use with swing accelerators.
116 * The key of the key stroke
117 * @return The key stroke, or <code>null</code> if no key stroke could be
118 * created from the translated value
120 public static KeyStroke getKeyStroke(String key) {
121 String value = get(key);
125 StringTokenizer keyTokens = new StringTokenizer(value, "+- ");
126 int modifierMask = 0;
127 while (keyTokens.hasMoreTokens()) {
128 String keyToken = keyTokens.nextToken();
129 if ("ctrl".equalsIgnoreCase(keyToken)) {
130 modifierMask |= InputEvent.CTRL_DOWN_MASK;
131 } else if ("alt".equalsIgnoreCase(keyToken)) {
132 modifierMask |= InputEvent.ALT_DOWN_MASK;
133 } else if ("shift".equalsIgnoreCase(keyToken)) {
134 modifierMask |= InputEvent.SHIFT_DOWN_MASK;
136 if (keyToken.startsWith("VK_")) {
138 Field field = KeyEvent.class.getField(keyToken);
139 System.out.println("asked for: “" + key + "”, will return " + field.getInt(null));
140 return KeyStroke.getKeyStroke(field.getInt(null), modifierMask);
141 } catch (SecurityException e) {
143 } catch (NoSuchFieldException e) {
145 } catch (IllegalArgumentException e) {
147 } catch (IllegalAccessException e) {
151 System.out.println("asked for: “" + key + "”, will return " + keyToken.charAt(0));
152 return KeyStroke.getKeyStroke(keyToken.charAt(0), modifierMask);
159 * Sets the current locale.
162 * The new locale to use
164 public static void setLocale(Locale newLocale) {
165 currentLocale = newLocale;
166 InputStream inputStream = null;
168 currentLanguage = new Properties(defaultLanguage);
169 inputStream = I18n.class.getResourceAsStream("jSite_" + currentLocale.toString() + ".properties");
170 if (inputStream != null) {
171 currentLanguage.load(inputStream);
173 } catch (MissingResourceException mre1) {
174 currentLocale = Locale.ENGLISH;
175 } catch (IOException ioe1) {
176 currentLocale = Locale.ENGLISH;
178 Closer.close(inputStream);