Initial revision
[old-projects.git] / ekit / com / hexidec / util / Translatrix.java
diff --git a/ekit/com/hexidec/util/Translatrix.java b/ekit/com/hexidec/util/Translatrix.java
new file mode 100644 (file)
index 0000000..a428d4e
--- /dev/null
@@ -0,0 +1,111 @@
+/*\r
+GNU Lesser General Public License\r
+\r
+Translatrix - General Access To Language Resource Bundles\r
+Copyright (C) 2002  Howard A Kistler\r
+\r
+This library is free software; you can redistribute it and/or\r
+modify it under the terms of the GNU Lesser General Public\r
+License as published by the Free Software Foundation; either\r
+version 2.1 of the License, or (at your option) any later version.\r
+\r
+This library is distributed in the hope that it will be useful,\r
+but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+Lesser General Public License for more details.\r
+\r
+You should have received a copy of the GNU Lesser General Public\r
+License along with this library; if not, write to the Free Software\r
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+*/\r
+\r
+package com.hexidec.util;\r
+\r
+import java.util.Locale;\r
+import java.util.MissingResourceException;\r
+import java.util.ResourceBundle;\r
+\r
+public class Translatrix\r
+{\r
+       private static ResourceBundle langResources;\r
+       private static String bundleName;\r
+\r
+       public Translatrix(String bundle)\r
+       {\r
+               bundleName = bundle;\r
+               try\r
+               {\r
+                       langResources = ResourceBundle.getBundle(bundleName);\r
+               }\r
+               catch(MissingResourceException mre)\r
+               {\r
+                       logException("MissingResourceException while loading language file", mre);\r
+               }\r
+       }\r
+\r
+       public static void setBundleName(String bundle)\r
+       {\r
+               bundleName = bundle;\r
+       }\r
+\r
+       public static void setLocale(Locale locale)\r
+       {\r
+               if(bundleName == null)\r
+               {\r
+                       return;\r
+               }\r
+               if(locale != null)\r
+               {\r
+                       try\r
+                       {\r
+                               langResources = ResourceBundle.getBundle(bundleName, locale);\r
+                       }\r
+                       catch(MissingResourceException mre1)\r
+                       {\r
+                               try\r
+                               {\r
+                                       langResources = ResourceBundle.getBundle(bundleName);\r
+                               }\r
+                               catch(MissingResourceException mre2)\r
+                               {\r
+                                       logException("MissingResourceException while loading language file", mre2);\r
+                               }\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       try\r
+                       {\r
+                               langResources = ResourceBundle.getBundle(bundleName);\r
+                       }\r
+                       catch(MissingResourceException mre)\r
+                       {\r
+                               logException("MissingResourceException while loading language file", mre);\r
+                       }\r
+               }\r
+       }\r
+\r
+       public static void setLocale(String sLanguage, String sCountry)\r
+       {\r
+               if(sLanguage != null && sCountry != null)\r
+               {\r
+                       setLocale(new Locale(sLanguage, sCountry));\r
+               }\r
+       }\r
+\r
+       public static String getTranslationString(String originalText)\r
+       {\r
+               if(bundleName == null)\r
+               {\r
+                       return originalText;\r
+               }\r
+               return langResources.getString(originalText);\r
+       }\r
+\r
+       private static void logException(String internalMessage, Exception e)\r
+       {\r
+               System.err.println(internalMessage);\r
+               e.printStackTrace(System.err);\r
+       }\r
+\r
+}
\ No newline at end of file