Initial revision
[old-projects.git] / ekit / com / hexidec / util / Translatrix.java
1 /*
2 GNU Lesser General Public License
3
4 Translatrix - General Access To Language Resource Bundles
5 Copyright (C) 2002 Howard A Kistler
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 package com.hexidec.util;
23
24 import java.util.Locale;
25 import java.util.MissingResourceException;
26 import java.util.ResourceBundle;
27
28 public class Translatrix
29 {
30 private static ResourceBundle langResources;
31 private static String bundleName;
32
33 public Translatrix(String bundle)
34 {
35 bundleName = bundle;
36 try
37 {
38 langResources = ResourceBundle.getBundle(bundleName);
39 }
40 catch(MissingResourceException mre)
41 {
42 logException("MissingResourceException while loading language file", mre);
43 }
44 }
45
46 public static void setBundleName(String bundle)
47 {
48 bundleName = bundle;
49 }
50
51 public static void setLocale(Locale locale)
52 {
53 if(bundleName == null)
54 {
55 return;
56 }
57 if(locale != null)
58 {
59 try
60 {
61 langResources = ResourceBundle.getBundle(bundleName, locale);
62 }
63 catch(MissingResourceException mre1)
64 {
65 try
66 {
67 langResources = ResourceBundle.getBundle(bundleName);
68 }
69 catch(MissingResourceException mre2)
70 {
71 logException("MissingResourceException while loading language file", mre2);
72 }
73 }
74 }
75 else
76 {
77 try
78 {
79 langResources = ResourceBundle.getBundle(bundleName);
80 }
81 catch(MissingResourceException mre)
82 {
83 logException("MissingResourceException while loading language file", mre);
84 }
85 }
86 }
87
88 public static void setLocale(String sLanguage, String sCountry)
89 {
90 if(sLanguage != null && sCountry != null)
91 {
92 setLocale(new Locale(sLanguage, sCountry));
93 }
94 }
95
96 public static String getTranslationString(String originalText)
97 {
98 if(bundleName == null)
99 {
100 return originalText;
101 }
102 return langResources.getString(originalText);
103 }
104
105 private static void logException(String internalMessage, Exception e)
106 {
107 System.err.println(internalMessage);
108 e.printStackTrace(System.err);
109 }
110
111 }