Multilingual.java
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58: package xeij;
59:
60: import java.awt.*;
61: import java.awt.event.*;
62: import java.lang.*;
63: import java.util.*;
64: import javax.swing.*;
65: import javax.swing.border.*;
66:
67: public class Multilingual {
68:
69:
70: public static HashMap<AbstractButton,HashMap<String,String>> mlnButtonTextMap;
71: public static HashMap<JLabel,HashMap<String,String>> mlnLabelTextMap;
72: public static HashMap<Dialog,HashMap<String,String>> mlnDialogTitleMap;
73: public static HashMap<Frame,HashMap<String,String>> mlnFrameTitleMap;
74: public static HashMap<JComponent,HashMap<String,String>> mlnComponentToolTipTextMap;
75: public static HashMap<SpinnerListModel,HashMap<String,java.util.List<?>>> mlnSpinnerListModelListMap;
76: public static HashMap<JComponent,HashMap<String,String>> mlnComponentTitledBorderMap;
77:
78:
79: public static String mlnLang;
80: public static boolean mlnEnglish;
81: public static boolean mlnJapanese;
82:
83:
84:
85:
86: public static void mlnInit () {
87:
88: mlnButtonTextMap = new HashMap<AbstractButton,HashMap<String,String>> ();
89: mlnLabelTextMap = new HashMap<JLabel,HashMap<String,String>> ();
90: mlnDialogTitleMap = new HashMap<Dialog,HashMap<String,String>> ();
91: mlnFrameTitleMap = new HashMap<Frame,HashMap<String,String>> ();
92: mlnComponentToolTipTextMap = new HashMap<JComponent,HashMap<String,String>> ();
93: mlnSpinnerListModelListMap = new HashMap<SpinnerListModel,HashMap<String,java.util.List<?>>> ();
94: mlnComponentTitledBorderMap = new HashMap<JComponent,HashMap<String,String>> ();
95: mlnLang = "";
96: String defaultLang = Locale.getDefault ().getLanguage ();
97: mlnChange (defaultLang.equals ("ja") ? "ja" : "en");
98: }
99:
100:
101:
102:
103: public static void mlnChange (String lang) {
104: if (!mlnLang.equals (lang)) {
105:
106: if (lang.equals ("ja")) {
107: mlnLang = "ja";
108: mlnEnglish = false;
109: mlnJapanese = true;
110: System.out.println ("日本語が選択されました");
111: } else {
112: mlnLang = "en";
113: mlnEnglish = true;
114: mlnJapanese = false;
115: System.out.println ("English is selected");
116: }
117:
118: for (Map.Entry<AbstractButton,HashMap<String,String>> entry : mlnButtonTextMap.entrySet ()) {
119: mlnChangeButtonText (entry.getKey (), entry.getValue (), lang);
120: }
121:
122: for (Map.Entry<JLabel,HashMap<String,String>> entry : mlnLabelTextMap.entrySet ()) {
123: mlnChangeLabelText (entry.getKey (), entry.getValue (), lang);
124: }
125:
126: for (Map.Entry<Dialog,HashMap<String,String>> entry : mlnDialogTitleMap.entrySet ()) {
127: mlnChangeDialogTitle (entry.getKey (), entry.getValue (), lang);
128: }
129:
130: for (Map.Entry<Frame,HashMap<String,String>> entry : mlnFrameTitleMap.entrySet ()) {
131: mlnChangeFrameTitle (entry.getKey (), entry.getValue (), lang);
132: }
133:
134: for (Map.Entry<JComponent,HashMap<String,String>> entry : mlnComponentToolTipTextMap.entrySet ()) {
135: mlnChangeComponentToolTipText (entry.getKey (), entry.getValue (), lang);
136: }
137:
138: for (Map.Entry<SpinnerListModel,HashMap<String,java.util.List<?>>> entry : mlnSpinnerListModelListMap.entrySet ()) {
139: mlnChangeSpinnerListModelList (entry.getKey (), entry.getValue (), lang);
140: }
141:
142: for (Map.Entry<JComponent,HashMap<String,String>> entry : mlnComponentTitledBorderMap.entrySet ()) {
143: mlnChangeComponentTitledBorder (entry.getKey (), entry.getValue (), lang);
144: }
145: }
146: }
147:
148:
149:
150: public static void mlnChangeButtonText (AbstractButton button, HashMap<String,String> map, String lang) {
151: button.setText (map.containsKey (lang) ? map.get (lang) : map.get ("en"));
152: }
153:
154:
155:
156: public static void mlnChangeLabelText (JLabel label, HashMap<String,String> map, String lang) {
157: label.setText (map.containsKey (lang) ? map.get (lang) : map.get ("en"));
158: }
159:
160:
161:
162: public static void mlnChangeDialogTitle (Dialog dialog, HashMap<String,String> map, String lang) {
163: dialog.setTitle (map.containsKey (lang) ? map.get (lang) : map.get ("en"));
164: }
165:
166:
167:
168: public static void mlnChangeFrameTitle (Frame frame, HashMap<String,String> map, String lang) {
169: frame.setTitle (map.containsKey (lang) ? map.get (lang) : map.get ("en"));
170: }
171:
172:
173:
174: public static void mlnChangeComponentToolTipText (JComponent component, HashMap<String,String> map, String lang) {
175: component.setToolTipText (map.containsKey (lang) ? map.get (lang) : map.get ("en"));
176: }
177:
178:
179:
180: public static void mlnChangeSpinnerListModelList (SpinnerListModel model, HashMap<String,java.util.List<?>> map, String lang) {
181: int index = model.getList ().indexOf (model.getValue ());
182: java.util.List<?> list = map.containsKey (lang) ? map.get (lang) : map.get ("en");
183: model.setList (list);
184: model.setValue (list.get (index));
185: }
186:
187:
188:
189: public static void mlnChangeComponentTitledBorder (JComponent component, HashMap<String,String> map, String lang) {
190: TitledBorder titledBorder = (TitledBorder) component.getBorder ();
191: titledBorder.setTitle (map.containsKey (lang) ? map.get (lang) : map.get ("en"));
192: component.repaint ();
193: }
194:
195:
196:
197:
198:
199: public static <T extends AbstractButton> T mlnText (T button, String... langText) {
200: String enText = button.getActionCommand ();
201: int mnemonic = button.getMnemonic ();
202: String mnemonicText = KeyEvent.getKeyText (mnemonic);
203: String mnemonicLowerCaseText = mnemonicText.toLowerCase ();
204: HashMap<String,String> map = new HashMap<String,String> ();
205: for (int i = langText.length > 0 && langText[0].equals ("en") ? 0 : -2; i < langText.length; i += 2) {
206: String lang = i < 0 ? "en" : langText[i];
207: String text = i < 0 ? enText : langText[i + 1];
208: map.put (
209: lang,
210: mnemonic == KeyEvent.VK_UNDEFINED ||
211: text.toLowerCase ().indexOf (mnemonicLowerCaseText) >= 0 ? text :
212: text + "(" + mnemonicText + ")");
213: }
214: mlnButtonTextMap.put (button, map);
215: mlnChangeButtonText (button, map, mlnLang);
216: return button;
217: }
218:
219:
220:
221: public static <T extends JLabel> T mlnText (T label, String... langText) {
222: String enText = label.getText ();
223: HashMap<String,String> map = new HashMap<String,String> ();
224: for (int i = langText.length > 0 && langText[0].equals ("en") ? 0 : -2; i < langText.length; i += 2) {
225: String lang = i < 0 ? "en" : langText[i];
226: String text = i < 0 ? enText : langText[i + 1];
227: map.put (lang, text);
228: }
229: mlnLabelTextMap.put (label, map);
230: mlnChangeLabelText (label, map, mlnLang);
231: return label;
232: }
233:
234:
235:
236: public static <T extends Dialog> T mlnTitle (T dialog, String... langText) {
237: String enText = dialog.getTitle ();
238: HashMap<String,String> map = new HashMap<String,String> ();
239: for (int i = langText.length > 0 && langText[0].equals ("en") ? 0 : -2; i < langText.length; i += 2) {
240: String lang = i < 0 ? "en" : langText[i];
241: String text = i < 0 ? enText : langText[i + 1];
242: map.put (lang, text);
243: }
244: mlnDialogTitleMap.put (dialog, map);
245: mlnChangeDialogTitle (dialog, map, mlnLang);
246: return dialog;
247: }
248:
249:
250:
251: public static <T extends Frame> T mlnTitle (T frame, String... langText) {
252: String enText = frame.getTitle ();
253: HashMap<String,String> map = new HashMap<String,String> ();
254: for (int i = langText.length > 0 && langText[0].equals ("en") ? 0 : -2; i < langText.length; i += 2) {
255: String lang = i < 0 ? "en" : langText[i];
256: String text = i < 0 ? enText : langText[i + 1];
257: map.put (lang, text);
258: }
259: mlnFrameTitleMap.put (frame, map);
260: mlnChangeFrameTitle (frame, map, mlnLang);
261: return frame;
262: }
263:
264:
265:
266: public static <T extends JComponent> T mlnToolTipText (T component, String... langText) {
267: String enText = component.getToolTipText ();
268: HashMap<String,String> map = new HashMap<String,String> ();
269: for (int i = langText.length > 0 && langText[0].equals ("en") ? 0 : -2; i < langText.length; i += 2) {
270: String lang = i < 0 ? "en" : langText[i];
271: String text = i < 0 ? enText : langText[i + 1];
272: map.put (lang, text);
273: }
274: mlnComponentToolTipTextMap.put (component, map);
275: mlnChangeComponentToolTipText (component, map, mlnLang);
276: return component;
277: }
278:
279:
280:
281:
282: public static JSpinner mlnList (JSpinner spinner, Object... langList) {
283: mlnList ((SpinnerListModel) spinner.getModel (), langList);
284: return spinner;
285: }
286: public static SpinnerListModel mlnList (SpinnerListModel model, Object... langList) {
287: java.util.List<?> enList = model.getList ();
288: HashMap<String,java.util.List<?>> map = new HashMap<String,java.util.List<?>> ();
289: for (int i = langList.length > 0 && ((String) langList[0]).equals ("en") ? 0 : -2; i < langList.length; i += 2) {
290: String lang = i < 0 ? "en" : (String) langList[i];
291: java.util.List<?> list = i < 0 ? enList : (java.util.List<?>) langList[i + 1];
292: map.put (lang, list);
293: }
294: mlnSpinnerListModelListMap.put (model, map);
295: mlnChangeSpinnerListModelList (model, map, mlnLang);
296: return model;
297: }
298:
299:
300:
301: public static <T extends JComponent> T mlnTitledBorder (T component, String... langText) {
302: TitledBorder titledBorder = (TitledBorder) component.getBorder ();
303: String enText = titledBorder.getTitle ();
304: HashMap<String,String> map = new HashMap<String,String> ();
305: for (int i = langText.length > 0 && langText[0].equals ("en") ? 0 : -2; i < langText.length; i += 2) {
306: String lang = i < 0 ? "en" : langText[i];
307: String text = i < 0 ? enText : langText[i + 1];
308: map.put (lang, text);
309: }
310: mlnComponentTitledBorderMap.put (component, map);
311: mlnChangeComponentTitledBorder (component, map, mlnLang);
312: return component;
313: }
314:
315: }
316:
317:
318: