i18n
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 5 Apr 2008 02:18:57 +0000 (02:18 +0000)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 5 Apr 2008 02:18:57 +0000 (02:18 +0000)
git-svn-id: http://trooper/svn/projects/jSite/trunk@597 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/jsite/i18n/I18n.java [new file with mode: 0644]
src/net/pterodactylus/jsite/i18n/jSite.properties [new file with mode: 0644]

diff --git a/src/net/pterodactylus/jsite/i18n/I18n.java b/src/net/pterodactylus/jsite/i18n/I18n.java
new file mode 100644 (file)
index 0000000..4a244c5
--- /dev/null
@@ -0,0 +1,181 @@
+/*
+ * jSite2 - I18n.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.jsite.i18n;
+
+import java.awt.event.InputEvent;
+import java.awt.event.KeyEvent;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Field;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+import javax.swing.KeyStroke;
+
+import net.pterodactylus.util.io.Closer;
+
+/**
+ * Class that handles i18n.
+ * 
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @version $Id$
+ */
+public class I18n {
+
+       /** The current locale. */
+       private static Locale currentLocale;
+
+       /** The default language. */
+       private static Properties defaultLanguage;
+
+       /** The current language. */
+       private static Properties currentLanguage;
+
+       static {
+               defaultLanguage = new Properties();
+               InputStream inputStream = null;
+               try {
+                       inputStream = I18n.class.getResourceAsStream("jSite.properties");
+                       if (inputStream != null) {
+                               defaultLanguage.load(inputStream);
+                       }
+               } catch (IOException e) {
+                       /* something is fucked. */
+               }
+               setLocale(Locale.getDefault());
+       }
+
+       /**
+        * Returns the translated value for a key.
+        * 
+        * @param key
+        *            The key to get
+        * @return The translated message, or the key itself if no translation could
+        *         be found
+        */
+       public static String get(String key) {
+               String value = null;
+               value = currentLanguage.getProperty(key);
+               if (value == null) {
+                       return key;
+               }
+               return value;
+       }
+
+       /**
+        * Returns the keycode from the value of the given key. You can specify the
+        * constants in {@link KeyEvent} in the properties file, e.g. VK_S for the
+        * keycode ‘s’ when used for mnemonics.
+        * 
+        * @param key
+        *            The key under which the keycode is stored
+        * @return The keycode
+        */
+       public static int getKey(String key) {
+               String value = get(key);
+               if (value.startsWith("VK_")) {
+                       try {
+                               Field field = KeyEvent.class.getField(value);
+                               return field.getInt(null);
+                       } catch (SecurityException e) {
+                               /* ignore. */
+                       } catch (NoSuchFieldException e) {
+                               /* ignore. */
+                       } catch (IllegalArgumentException e) {
+                               /* ignore. */
+                       } catch (IllegalAccessException e) {
+                               /* ignore. */
+                       }
+               }
+               return value.toUpperCase().charAt(0);
+       }
+
+       /**
+        * Returns a key stroke for use with swing accelerators.
+        * 
+        * @param key
+        *            The key of the key stroke
+        * @return The key stroke, or <code>null</code> if no key stroke could be
+        *         created from the translated value
+        */
+       public static KeyStroke getKeyStroke(String key) {
+               String value = get(key);
+               if (value == null) {
+                       return null;
+               }
+               StringTokenizer keyTokens = new StringTokenizer(value, "+- ");
+               int modifierMask = 0;
+               while (keyTokens.hasMoreTokens()) {
+                       String keyToken = keyTokens.nextToken();
+                       if ("ctrl".equalsIgnoreCase(keyToken)) {
+                               modifierMask |= InputEvent.CTRL_DOWN_MASK;
+                       } else if ("alt".equalsIgnoreCase(keyToken)) {
+                               modifierMask |= InputEvent.ALT_DOWN_MASK;
+                       } else if ("shift".equalsIgnoreCase(keyToken)) {
+                               modifierMask |= InputEvent.SHIFT_DOWN_MASK;
+                       } else {
+                               if (keyToken.startsWith("VK_")) {
+                                       try {
+                                               Field field = KeyEvent.class.getField(keyToken);
+                                               System.out.println("asked for: “" + key + "”, will return " + field.getInt(null));
+                                               return KeyStroke.getKeyStroke(field.getInt(null), modifierMask);
+                                       } catch (SecurityException e) {
+                                               /* ignore. */
+                                       } catch (NoSuchFieldException e) {
+                                               /* ignore. */
+                                       } catch (IllegalArgumentException e) {
+                                               /* ignore. */
+                                       } catch (IllegalAccessException e) {
+                                               /* ignore. */
+                                       }
+                               }
+                               System.out.println("asked for: “" + key + "”, will return " + keyToken.charAt(0));
+                               return KeyStroke.getKeyStroke(keyToken.charAt(0), modifierMask);
+                       }
+               }
+               return null;
+       }
+
+       /**
+        * Sets the current locale.
+        * 
+        * @param newLocale
+        *            The new locale to use
+        */
+       public static void setLocale(Locale newLocale) {
+               currentLocale = newLocale;
+               InputStream inputStream = null;
+               try {
+                       currentLanguage = new Properties(defaultLanguage);
+                       inputStream = I18n.class.getResourceAsStream("jSite_" + currentLocale.toString() + ".properties");
+                       if (inputStream != null) {
+                               currentLanguage.load(inputStream);
+                       }
+               } catch (MissingResourceException mre1) {
+                       currentLocale = Locale.ENGLISH;
+               } catch (IOException ioe1) {
+                       currentLocale = Locale.ENGLISH;
+               } finally {
+                       Closer.close(inputStream);
+               }
+       }
+}
diff --git a/src/net/pterodactylus/jsite/i18n/jSite.properties b/src/net/pterodactylus/jsite/i18n/jSite.properties
new file mode 100644 (file)
index 0000000..b0bfe0f
--- /dev/null
@@ -0,0 +1,101 @@
+# jSite i18n data, English
+#
+# this file should be in US-ASCII format, with unicode characters outside US-ASCII range encoded
+# with the java-typical \u notation.
+
+# use small letters for mnemonics, use large letters for accelerators.
+
+# general gui components
+general.button.okay.name: Okay
+general.button.okay.mnemonic: o
+general.button.okay.accelerator: Ctrl-VK_O
+general.button.okay.shortDescription: Confirm Nodes
+general.button.okay.longDescription: Save Node Settings and Close Dialog
+
+general.button.cancel.name: Cancel
+general.button.cancel.mnemonic: c
+general.button.cancel.accelerator: VK_ESCAPE
+general.button.cancel.shortDescription: Cancel
+general.button.cancel.longDescription: Don\u2019t Save Node Settings and Close Dialog
+
+
+# main menus
+mainWindow.menu.node.name: Node
+mainWindow.menu.node.mnemonic: n
+
+#
+# the node menu
+#
+
+#manage nodes
+mainWindow.menu.node.item.manageNodes.name: Manage Nodes
+mainWindow.menu.node.item.manageNodes.mnemonic: m
+mainWindow.menu.node.item.manageNodes.accelerator: Ctrl-VK_M
+mainWindow.menu.node.item.manageNodes.shortDescription: Manage Nodes
+mainWindow.menu.node.item.manageNodes.longDescription: Manage Your Nodes
+
+# connect to node
+mainWindow.menu.node.item.connect.name: Connect
+mainWindow.menu.node.item.connect.mnemonic: c
+mainWindow.menu.node.item.connect.accelerator: Ctrl-VK_C
+mainWindow.menu.node.item.connect.shortDescription: Connect to Node
+mainWindow.menu.node.item.connect.longDescription: Connect to the Selected Node
+
+#disconnect from node
+mainWindow.menu.node.item.disconnect.name: Disconnect
+mainWindow.menu.node.item.disconnect.mnemonic: d
+mainWindow.menu.node.item.disconnect.accelerator: Ctrl-VK_D
+mainWindow.menu.node.item.disconnect.shortDescription: Disconnect from Node
+mainWindow.menu.node.item.disconnect.longDescription: Disconnect from the Currently Connected Node
+
+#
+# the "manage nodes" dialog
+#
+
+manageNodesDialog.title: Manage Nodes
+
+manageNodesDialog.button.addNode.name: Add Node
+manageNodesDialog.button.addNode.mnemonic: a
+manageNodesDialog.button.addNode.accelerator: Ctrl-VK_A
+manageNodesDialog.button.addNode.shortDescription: Add a Node
+manageNodesDialog.button.addNode.longDescription: Add a Node
+
+manageNodesDialog.button.editNode.name: Edit Node
+manageNodesDialog.button.editNode.mnemonic: e
+manageNodesDialog.button.editNode.accelerator: Ctrl-VK_E
+manageNodesDialog.button.editNode.shortDescription: Edit Selected Node
+
+manageNodesDialog.button.deleteNode.name: Delete Node
+manageNodesDialog.button.deleteNode.mnemonic: d
+manageNodesDialog.button.deleteNode.accelerator: Ctrl-VK_D
+manageNodesDialog.button.deleteNode.shortDescription: Delete Selected Node(s)
+
+manageNodesDialog.error.nodeListEmpty.title: Node List Empty
+manageNodesDialog.error.nodeListEmpty.message: The node list is empty.
+
+# the "edit node" dialog
+editNodeDialog.title: Edit Node
+
+editNodeDialog.label.name.name: Name
+editNodeDialog.label.name.mnemonic: n
+
+editNodeDialog.label.hostname.name: Hostname
+editNodeDialog.label.hostname.mnemonic: h
+
+editNodeDialog.label.port.name: Port
+editNodeDialog.label.port.mnemonic: p
+
+editNodeDialog.checkbox.sameMachine.name: Node on the same machine
+editNodeDialog.checkbox.sameMachine.mnemonic: s
+editNodeDialog.checkbox.sameMachine.accelerator: Alt-VK_S
+editNodeDialog.checkbox.sameMachine.shortDescription: Node is on the same machine
+editNodeDialog.checkbox.sameMachine.longDescription: The node is on the same machine as jSite and some optimizations can be used
+
+editNodeDialog.error.name.title: Wrong Node Name
+editNodeDialog.error.name.message: The name of the node is empty.
+
+editNodeDialog.error.hostname.title: Wrong Hostname
+editNodeDialog.error.hostname.message: The hostname is invalid.
+
+editNodeDialog.error.port.title: Wrong Port Number
+editNodeDialog.error.port.message: The port number is invalid.