1
19
20 package com.liferay.portal.tools;
21
22 import com.liferay.portal.kernel.util.FileUtil;
23 import com.liferay.portal.kernel.util.StringUtil;
24 import com.liferay.portal.kernel.util.Validator;
25 import com.liferay.portal.kernel.webcache.WebCacheItem;
26 import com.liferay.portal.util.InitUtil;
27 import com.liferay.portlet.translator.model.Translation;
28 import com.liferay.portlet.translator.util.TranslationWebCacheItem;
29
30 import java.io.BufferedReader;
31 import java.io.BufferedWriter;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.io.StringReader;
37
38 import java.util.Properties;
39 import java.util.Set;
40 import java.util.TreeSet;
41
42
48 public class LangBuilder {
49
50 public static void main(String[] args) {
51 InitUtil.initWithSpring();
52
53 if (args.length == 2) {
54 new LangBuilder(args[0], args[1]);
55 }
56 else {
57 throw new IllegalArgumentException();
58 }
59 }
60
61 public LangBuilder(String langDir, String langFile) {
62 try {
63 _langDir = langDir;
64 _langFile = langFile;
65
66 String content = _orderProps(
67 new File(_langDir + "/" + _langFile + ".properties"));
68
69 _createProps(content, "ar"); _createProps(content, "ca"); _createProps(content, "zh_CN"); _createProps(content, "zh_TW"); _createProps(content, "cs"); _createProps(content, "nl"); _createProps(content, "fi"); _createProps(content, "fr"); _createProps(content, "de"); _createProps(content, "el"); _createProps(content, "hu"); _createProps(content, "it"); _createProps(content, "ja"); _createProps(content, "ko"); _createProps(content, "nb"); _createProps(content, "fa"); _createProps(content, "pt_BR"); _createProps(content, "pt_PT"); _createProps(content, "ru"); _createProps(content, "es"); _createProps(content, "sv"); _createProps(content, "tr"); _createProps(content, "vi"); }
93 catch (Exception e) {
94 e.printStackTrace();
95 }
96 }
97
98 private void _createProps(String content, String languageId)
99 throws IOException {
100
101 File propsFile = new File(
102 _langDir + "/" + _langFile + "_" + languageId + ".properties");
103
104 Properties props = new Properties();
105
106 if (propsFile.exists()) {
107 props.load(new FileInputStream(propsFile));
108 }
109
110 File nativePropsFile = new File(
111 _langDir + "/" + _langFile + "_" + languageId +
112 ".properties.native");
113
114 Properties nativeProps = new Properties();
115
116 if (nativePropsFile.exists()) {
117 nativeProps.load(new FileInputStream(nativePropsFile));
118 }
119
120 String translationId = "en_" + languageId;
121
122 if (translationId.equals("en_pt_BR")) {
123 translationId = "en_pt";
124 }
125 else if (translationId.equals("en_pt_PT")) {
126 translationId = "en_pt";
127 }
128 else if (translationId.equals("en_zh_CN")) {
129 translationId = "en_zh";
130 }
131 else if (translationId.equals("en_zh_TW")) {
132 translationId = "en_zt";
133 }
134
135 BufferedReader br = new BufferedReader(new StringReader(content));
136 BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
137
138 String line = null;
139
140 while ((line = br.readLine()) != null) {
141 line = line.trim();
142
143 int pos = line.indexOf("=");
144
145 if (pos != -1) {
146 String key = line.substring(0, pos);
147 String value = line.substring(pos + 1, line.length());
148
149 String translatedText = props.getProperty(key);
150
151 if ((translatedText != null) &&
152 ((translatedText.indexOf("Babel Fish") != -1) ||
153 (translatedText.indexOf("Yahoo! - 999") != -1))) {
154
155 translatedText = "";
156 }
157
158 if ((translatedText == null) || translatedText.equals("")) {
159 if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
160 translatedText = value;
161 }
162 else if (key.equals("lang.dir")) {
163 translatedText = "ltr";
164 }
165 else if (key.equals("lang.line.begin")) {
166 translatedText = "left";
167 }
168 else if (key.equals("lang.line.end")) {
169 translatedText = "right";
170 }
171 else {
172 translatedText = _translate(translationId, value, 0);
173 }
174 }
175
176 if (Validator.isNotNull(translatedText)) {
177 if (translatedText.indexOf("'") != -1) {
178 translatedText = StringUtil.replace(
179 translatedText, "'", "\'");
180 }
181
182 bw.write(key + "=" + translatedText);
183
184 bw.newLine();
185 bw.flush();
186 }
187 else if (nativeProps.containsKey(key)) {
188 bw.write(key + "=");
189
190 bw.newLine();
191 bw.flush();
192 }
193 }
194 else {
195 bw.write(line);
196
197 bw.newLine();
198 bw.flush();
199 }
200 }
201
202 br.close();
203 bw.close();
204 }
205
206 private String _orderProps(File propsFile) throws IOException {
207 String content = FileUtil.read(propsFile);
208
209 BufferedReader br = new BufferedReader(new StringReader(content));
210 BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
211
212 Set<String> messages = new TreeSet<String>();
213
214 boolean begin = false;
215
216 String line = null;
217
218 while ((line = br.readLine()) != null) {
219 int pos = line.indexOf("=");
220
221 if (pos != -1) {
222 String key = line.substring(0, pos);
223 String value = line.substring(pos + 1, line.length());
224
225 messages.add(key + "=" + value);
226 }
227 else {
228 if (begin == true && line.equals("")) {
229 _sortAndWrite(bw, messages);
230 }
231
232 if (line.equals("")) {
233 begin = !begin;
234 }
235
236 bw.write(line);
237 bw.newLine();
238 }
239
240 bw.flush();
241 }
242
243 if (messages.size() > 0) {
244 _sortAndWrite(bw, messages);
245 }
246
247 br.close();
248 bw.close();
249
250 return FileUtil.read(propsFile);
251 }
252
253 private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
254 throws IOException {
255
256 String[] messagesArray = messages.toArray(new String[messages.size()]);
257
258 for (int i = 0; i < messagesArray.length; i++) {
259 bw.write(messagesArray[i]);
260 bw.newLine();
261 }
262
263 messages.clear();
264 }
265
266 private String _translate(
267 String translationId, String fromText, int limit) {
268
269 if (translationId.equals("en_ar") ||
270 translationId.equals("en_ca") ||
271 translationId.equals("en_cs") ||
272 translationId.equals("en_fi") ||
273 translationId.equals("en_hu") ||
274 translationId.equals("en_nb") ||
275 translationId.equals("en_fa") ||
276 translationId.equals("en_ru") ||
277 translationId.equals("en_sv") ||
278 translationId.equals("en_tr") ||
279 translationId.equals("en_vi")) {
280
281
285 return null;
286 }
287
288
290 if (limit == 3) {
291 return null;
292 }
293
294 String toText = null;
295
296 try {
297 System.out.println("Translating " + translationId + " " + fromText);
298
299 WebCacheItem wci = new TranslationWebCacheItem(
300 translationId, fromText);
301
302 Translation translation = (Translation)wci.convert("");
303
304 toText = translation.getToText();
305
306 if ((toText != null) &&
307 (toText.indexOf("Babel Fish") != -1)) {
308
309 toText = null;
310 }
311 }
312 catch (Exception e) {
313 e.printStackTrace();
314 }
315
316
318 if (toText == null) {
319 return _translate(translationId, fromText, ++limit);
320 }
321
322 return toText;
323 }
324
325 private String _langDir;
326 private String _langFile;
327
328 }