1 | /* |
---|
2 | * @Author(s) - Alexndre Correia |
---|
3 | * |
---|
4 | * This class is a wrapper for easier i18n of Jeti. |
---|
5 | * It uses the standard i18n mechanisms given by Java. |
---|
6 | * |
---|
7 | * Created on - 30/06/2009 |
---|
8 | */ |
---|
9 | |
---|
10 | package nu.fw.jeti.util; |
---|
11 | |
---|
12 | import java.util.*; |
---|
13 | |
---|
14 | import javax.swing.AbstractButton; |
---|
15 | import javax.swing.JLabel; |
---|
16 | import nu.fw.jeti.languages.*; |
---|
17 | |
---|
18 | public class I18N implements languages, languagesPlugins |
---|
19 | { |
---|
20 | private static HashMap messagesLang; |
---|
21 | private static HashMap messagesLangPlugins; |
---|
22 | |
---|
23 | public static String getText(String type, String messageID) |
---|
24 | { |
---|
25 | return getText(messageID); |
---|
26 | } |
---|
27 | |
---|
28 | public static String gettext(String messageID) |
---|
29 | { |
---|
30 | return getText(messageID); |
---|
31 | } |
---|
32 | |
---|
33 | private static String getText( String messageID ) |
---|
34 | { |
---|
35 | if ( messageID == null ) |
---|
36 | return null; |
---|
37 | |
---|
38 | String translation = getTextWithAmp(messageID); |
---|
39 | //remove & |
---|
40 | int i = translation.indexOf("&"); |
---|
41 | if (i > -1) |
---|
42 | { |
---|
43 | translation = translation.substring(0, i) + translation.substring(i + 1, translation.length()); |
---|
44 | } |
---|
45 | |
---|
46 | return translation; |
---|
47 | } |
---|
48 | |
---|
49 | public static String getTextWithAmp(String messageID) |
---|
50 | { |
---|
51 | return I18N.getText_WithAmp(messageID); |
---|
52 | } |
---|
53 | |
---|
54 | private static String getText_WithAmp( String messageID ) |
---|
55 | { |
---|
56 | String translation = (String)messagesLang.get(messageID); |
---|
57 | |
---|
58 | if( translation == null ) |
---|
59 | { |
---|
60 | int dotPos = messageID.lastIndexOf('.'); |
---|
61 | |
---|
62 | if ( dotPos > 0 ) |
---|
63 | messageID = messageID.substring(dotPos+1); |
---|
64 | |
---|
65 | return messageID.replace('_',' '); |
---|
66 | } |
---|
67 | |
---|
68 | return translation; |
---|
69 | } |
---|
70 | |
---|
71 | public static void setTextAndMnemonic(String messageID,JLabel label) |
---|
72 | { |
---|
73 | setTextAndMnemonic(messageID, label, false); |
---|
74 | } |
---|
75 | |
---|
76 | public static void setTextAndMnemonic(String messageID, JLabel label, boolean addDots ) |
---|
77 | { |
---|
78 | String mnemonicText = I18N.getTextWithAmp(messageID); |
---|
79 | String translation=mnemonicText; |
---|
80 | int pos = mnemonicText.indexOf("&"); |
---|
81 | |
---|
82 | if (pos > -1) |
---|
83 | translation = mnemonicText.substring(0, pos) + mnemonicText.substring(pos + 1, mnemonicText.length()); |
---|
84 | |
---|
85 | if (addDots) |
---|
86 | translation += "..."; |
---|
87 | |
---|
88 | label.setText(translation); |
---|
89 | pos = ( (int)mnemonicText.indexOf("&") >= 0 ) ? ((int)mnemonicText.indexOf("&")) + 1 : -1; |
---|
90 | |
---|
91 | if ( pos > -1) |
---|
92 | label.setDisplayedMnemonic(mnemonicText.charAt(pos)); |
---|
93 | |
---|
94 | if ( pos > 0 ) |
---|
95 | label.setDisplayedMnemonicIndex(pos-1); |
---|
96 | } |
---|
97 | |
---|
98 | public static void setMnemonic(String messageID, JLabel label) |
---|
99 | { |
---|
100 | String translation = I18N.getTextWithAmp(messageID); |
---|
101 | int pos = ( (int)translation.indexOf("&") >= 0 ) ? ((int)translation.indexOf("&")) + 1 : -1; |
---|
102 | |
---|
103 | if ( pos > -1) |
---|
104 | label.setDisplayedMnemonic(translation.charAt(pos)); |
---|
105 | |
---|
106 | if ( pos > 1) |
---|
107 | label.setDisplayedMnemonicIndex(pos-1); |
---|
108 | } |
---|
109 | |
---|
110 | public static void setMnemonic(String messageID, AbstractButton button) |
---|
111 | { |
---|
112 | String translation = I18N.getTextWithAmp(messageID); |
---|
113 | int pos = ( (int)translation.indexOf("&") >= 0 ) ? ((int)translation.indexOf("&")) + 1 : -1; |
---|
114 | |
---|
115 | if ( pos >- 1) |
---|
116 | button.setMnemonic(translation.charAt(pos)); |
---|
117 | if ( pos > 1) |
---|
118 | button.setDisplayedMnemonicIndex(pos-1); |
---|
119 | } |
---|
120 | |
---|
121 | public static void setTextAndMnemonic(String messageID,AbstractButton button) |
---|
122 | { |
---|
123 | setTextAndMnemonic(messageID, button, false); |
---|
124 | } |
---|
125 | |
---|
126 | public static void setTextAndMnemonic(String messageID,AbstractButton button, boolean addDots) |
---|
127 | { |
---|
128 | String mnemonicText = getTextWithAmp(messageID); |
---|
129 | String translation=mnemonicText; |
---|
130 | |
---|
131 | int pos = mnemonicText.indexOf("&"); |
---|
132 | |
---|
133 | if (pos > -1) |
---|
134 | translation = mnemonicText.substring(0, pos) + mnemonicText.substring(pos + 1, mnemonicText.length()); |
---|
135 | |
---|
136 | if (addDots) |
---|
137 | translation += "..."; |
---|
138 | |
---|
139 | button.setText(translation); |
---|
140 | |
---|
141 | pos = ( (int)mnemonicText.indexOf("&") >= 0 ) ? ((int)mnemonicText.indexOf("&")) + 1 : -1; |
---|
142 | |
---|
143 | if( pos > -1) |
---|
144 | button.setMnemonic(mnemonicText.charAt(pos)); |
---|
145 | |
---|
146 | if( pos > 0 ) |
---|
147 | button.setDisplayedMnemonicIndex(pos-1); |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | private static void loadLanguages(Locale myLocale) |
---|
152 | { |
---|
153 | if ( myLocale.toString().equals("pt_BR")) |
---|
154 | { |
---|
155 | for( int i = 0; i < language_pt_BR.length; i++ ) |
---|
156 | messagesLang.put(language_pt_BR[i][0].toString(), language_pt_BR[i][1].toString()); |
---|
157 | |
---|
158 | for( int i = 0 ; i < languagePlugins_pt_BR.length; i++) |
---|
159 | messagesLangPlugins.put(languagePlugins_pt_BR[i][0].toString(), languagePlugins_pt_BR[i][1].toString()); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | public static void init(String myLanguage, String myCountry) |
---|
164 | { |
---|
165 | messagesLang = new HashMap(); |
---|
166 | messagesLangPlugins = new HashMap(); |
---|
167 | |
---|
168 | // Preferences |
---|
169 | Locale myLocale = null; |
---|
170 | |
---|
171 | if( myLanguage != "" ) |
---|
172 | myLocale = new Locale(myLanguage, myCountry); |
---|
173 | else |
---|
174 | myLocale = new Locale("en", "US"); |
---|
175 | |
---|
176 | System.out.println("Valor de myLocale : " + myLocale); |
---|
177 | |
---|
178 | I18N.loadLanguages(myLocale); |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | /* |
---|
183 | * Overrides for emacs |
---|
184 | * Local variables: |
---|
185 | * tab-width: 4 |
---|
186 | * End: |
---|
187 | */ |
---|