remove debug output
[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                                                 return KeyStroke.getKeyStroke(field.getInt(null), modifierMask);
140                                         } catch (SecurityException e) {
141                                                 /* ignore. */
142                                         } catch (NoSuchFieldException e) {
143                                                 /* ignore. */
144                                         } catch (IllegalArgumentException e) {
145                                                 /* ignore. */
146                                         } catch (IllegalAccessException e) {
147                                                 /* ignore. */
148                                         }
149                                 }
150                                 return KeyStroke.getKeyStroke(keyToken.charAt(0), modifierMask);
151                         }
152                 }
153                 return null;
154         }
155
156         /**
157          * Sets the current locale.
158          * 
159          * @param newLocale
160          *            The new locale to use
161          */
162         public static void setLocale(Locale newLocale) {
163                 currentLocale = newLocale;
164                 InputStream inputStream = null;
165                 try {
166                         currentLanguage = new Properties(defaultLanguage);
167                         inputStream = I18n.class.getResourceAsStream("jSite_" + currentLocale.toString() + ".properties");
168                         if (inputStream != null) {
169                                 currentLanguage.load(inputStream);
170                         }
171                 } catch (MissingResourceException mre1) {
172                         currentLocale = Locale.ENGLISH;
173                 } catch (IOException ioe1) {
174                         currentLocale = Locale.ENGLISH;
175                 } finally {
176                         Closer.close(inputStream);
177                 }
178         }
179 }