i18n
[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.util.Locale;
28 import java.util.MissingResourceException;
29 import java.util.Properties;
30 import java.util.StringTokenizer;
31
32 import javax.swing.KeyStroke;
33
34 import net.pterodactylus.util.io.Closer;
35
36 /**
37  * Class that handles i18n.
38  * 
39  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
40  * @version $Id$
41  */
42 public class I18n {
43
44         /** The current locale. */
45         private static Locale currentLocale;
46
47         /** The default language. */
48         private static Properties defaultLanguage;
49
50         /** The current language. */
51         private static Properties currentLanguage;
52
53         static {
54                 defaultLanguage = new Properties();
55                 InputStream inputStream = null;
56                 try {
57                         inputStream = I18n.class.getResourceAsStream("jSite.properties");
58                         if (inputStream != null) {
59                                 defaultLanguage.load(inputStream);
60                         }
61                 } catch (IOException e) {
62                         /* something is fucked. */
63                 }
64                 setLocale(Locale.getDefault());
65         }
66
67         /**
68          * Returns the translated value for a key.
69          * 
70          * @param key
71          *            The key to get
72          * @return The translated message, or the key itself if no translation could
73          *         be found
74          */
75         public static String get(String key) {
76                 String value = null;
77                 value = currentLanguage.getProperty(key);
78                 if (value == null) {
79                         return key;
80                 }
81                 return value;
82         }
83
84         /**
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.
88          * 
89          * @param key
90          *            The key under which the keycode is stored
91          * @return The keycode
92          */
93         public static int getKey(String key) {
94                 String value = get(key);
95                 if (value.startsWith("VK_")) {
96                         try {
97                                 Field field = KeyEvent.class.getField(value);
98                                 return field.getInt(null);
99                         } catch (SecurityException e) {
100                                 /* ignore. */
101                         } catch (NoSuchFieldException e) {
102                                 /* ignore. */
103                         } catch (IllegalArgumentException e) {
104                                 /* ignore. */
105                         } catch (IllegalAccessException e) {
106                                 /* ignore. */
107                         }
108                 }
109                 return value.toUpperCase().charAt(0);
110         }
111
112         /**
113          * Returns a key stroke for use with swing accelerators.
114          * 
115          * @param key
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
119          */
120         public static KeyStroke getKeyStroke(String key) {
121                 String value = get(key);
122                 if (value == null) {
123                         return null;
124                 }
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;
135                         } else {
136                                 if (keyToken.startsWith("VK_")) {
137                                         try {
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) {
142                                                 /* ignore. */
143                                         } catch (NoSuchFieldException e) {
144                                                 /* ignore. */
145                                         } catch (IllegalArgumentException e) {
146                                                 /* ignore. */
147                                         } catch (IllegalAccessException e) {
148                                                 /* ignore. */
149                                         }
150                                 }
151                                 System.out.println("asked for: “" + key + "”, will return " + keyToken.charAt(0));
152                                 return KeyStroke.getKeyStroke(keyToken.charAt(0), modifierMask);
153                         }
154                 }
155                 return null;
156         }
157
158         /**
159          * Sets the current locale.
160          * 
161          * @param newLocale
162          *            The new locale to use
163          */
164         public static void setLocale(Locale newLocale) {
165                 currentLocale = newLocale;
166                 InputStream inputStream = null;
167                 try {
168                         currentLanguage = new Properties(defaultLanguage);
169                         inputStream = I18n.class.getResourceAsStream("jSite_" + currentLocale.toString() + ".properties");
170                         if (inputStream != null) {
171                                 currentLanguage.load(inputStream);
172                         }
173                 } catch (MissingResourceException mre1) {
174                         currentLocale = Locale.ENGLISH;
175                 } catch (IOException ioe1) {
176                         currentLocale = Locale.ENGLISH;
177                 } finally {
178                         Closer.close(inputStream);
179                 }
180         }
181 }