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