1
14
15 package com.liferay.portal.tools;
16
17 import com.liferay.portal.kernel.io.OutputStreamWriter;
18 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
19 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedWriter;
20 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
21 import com.liferay.portal.kernel.util.FileUtil;
22 import com.liferay.portal.kernel.util.PropertiesUtil;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.portal.kernel.util.StringUtil;
25 import com.liferay.portal.kernel.util.Validator;
26 import com.liferay.portal.kernel.webcache.WebCacheItem;
27 import com.liferay.portal.util.InitUtil;
28 import com.liferay.portlet.translator.model.Translation;
29 import com.liferay.portlet.translator.util.TranslationWebCacheItem;
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.FileOutputStream;
34 import java.io.FileWriter;
35 import java.io.IOException;
36
37 import java.util.Properties;
38 import java.util.Set;
39 import java.util.TreeSet;
40
41
46 public class LangBuilder {
47
48 public static final String AUTOMATIC_COPY = " (Automatic Copy)";
49
50 public static final String AUTOMATIC_TRANSLATION =
51 " (Automatic Translation)";
52
53 public static void main(String[] args) {
54 InitUtil.initWithSpring();
55
56 if (args.length == 2) {
57 new LangBuilder(args[0], args[1], null);
58 }
59 else if (args.length == 3) {
60 new LangBuilder(args[0], args[1], args[2]);
61 }
62 else {
63 throw new IllegalArgumentException();
64 }
65 }
66
67 public LangBuilder(String langDir, String langFile, String langCode) {
68 try {
69 _langDir = langDir;
70 _langFile = langFile;
71
72 File renameKeysFile = new File(_langDir + "/rename.properties");
73
74 if (renameKeysFile.exists()) {
75 _renameKeys = PropertiesUtil.load(
76 FileUtil.read(renameKeysFile));
77 }
78
79 String content = _orderProperties(
80 new File(_langDir + "/" + _langFile + ".properties"));
81
82 if (Validator.isNotNull(langCode) && !langCode.startsWith("$")) {
83 _createProperties(content, langCode);
84 }
85 else {
86 _createProperties(content, "ar"); _createProperties(content, "eu"); _createProperties(content, "ca"); _createProperties(content, "zh_CN"); _createProperties(content, "zh_TW"); _createProperties(content, "cs"); _createProperties(content, "nl"); _createProperties(content, "fi"); _createProperties(content, "fr"); _createProperties(content, "de"); _createProperties(content, "el"); _createProperties(content, "hu"); _createProperties(content, "it"); _createProperties(content, "ja"); _createProperties(content, "ko"); _createProperties(content, "nb"); _createProperties(content, "fa"); _createProperties(content, "pl"); _createProperties(content, "pt_BR"); _createProperties(content, "pt_PT"); _createProperties(content, "ru"); _createProperties(content, "sk"); _createProperties(content, "es"); _createProperties(content, "sv"); _createProperties(content, "tr"); _createProperties(content, "vi"); }
113 }
114 catch (Exception e) {
115 e.printStackTrace();
116 }
117 }
118
119 private void _createProperties(String content, String languageId)
120 throws IOException {
121
122 File propertiesFile = new File(
123 _langDir + "/" + _langFile + "_" + languageId + ".properties");
124
125 File nativePropertiesFile = new File(
126 _langDir + "/" + _langFile + "_" + languageId +
127 ".properties.native");
128
129 if (nativePropertiesFile.exists()) {
130 propertiesFile.delete();
131
132 nativePropertiesFile.renameTo(propertiesFile);
133 }
134
135 Properties properties = new Properties();
136
137 if (propertiesFile.exists()) {
138 properties = PropertiesUtil.load(
139 new FileInputStream(propertiesFile), StringPool.UTF8);
140 }
141
142 String translationId = "en_" + languageId;
143
144 if (translationId.equals("en_pt_BR")) {
145 translationId = "en_pt";
146 }
147 else if (translationId.equals("en_pt_PT")) {
148 translationId = "en_pt";
149 }
150 else if (translationId.equals("en_zh_CN")) {
151 translationId = "en_zh";
152 }
153 else if (translationId.equals("en_zh_TW")) {
154 translationId = "en_zt";
155 }
156
157 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
158 new UnsyncStringReader(content));
159 UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter(
160 new OutputStreamWriter(
161 new FileOutputStream(propertiesFile), StringPool.UTF8));
162
163 String line = null;
164
165 while ((line = unsyncBufferedReader.readLine()) != null) {
166 line = line.trim();
167
168 int pos = line.indexOf("=");
169
170 if (pos != -1) {
171 String key = line.substring(0, pos);
172 String value = line.substring(pos + 1, line.length());
173
174 String translatedText = properties.getProperty(key);
175
176 if ((translatedText == null) && (_renameKeys != null)) {
177 String renameKey = _renameKeys.getProperty(key);
178
179 if (renameKey != null) {
180 translatedText = properties.getProperty(key);
181 }
182 }
183
184 if ((translatedText != null) &&
185 ((translatedText.indexOf("Babel Fish") != -1) ||
186 (translatedText.indexOf("Yahoo! - 999") != -1))) {
187
188 translatedText = "";
189 }
190
191 if ((translatedText == null) || translatedText.equals("")) {
192 if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
193 translatedText = value + AUTOMATIC_COPY;
194 }
195 else if (line.indexOf("[") != -1) {
196 pos = line.indexOf("[");
197
198 String baseKey = line.substring(0, pos);
199
200 translatedText =
201 properties.getProperty(baseKey) + AUTOMATIC_COPY;
202 }
203 else if (key.equals("lang.dir")) {
204 translatedText = "ltr";
205 }
206 else if (key.equals("lang.line.begin")) {
207 translatedText = "left";
208 }
209 else if (key.equals("lang.line.end")) {
210 translatedText = "right";
211 }
212 else if (translationId.equals("en_el") &&
213 (key.equals("enabled") || key.equals("on") ||
214 key.equals("on-date"))) {
215
216 translatedText = "";
217 }
218 else if (translationId.equals("en_es") &&
219 key.equals("am")) {
220
221 translatedText = "";
222 }
223 else if (translationId.equals("en_it") &&
224 key.equals("am")) {
225
226 translatedText = "";
227 }
228 else if (translationId.equals("en_ja") &&
229 (key.equals("any") || key.equals("anytime") ||
230 key.equals("down") || key.equals("on") ||
231 key.equals("on-date") || key.equals("the"))) {
232
233 translatedText = "";
234 }
235 else if (translationId.equals("en_ko") &&
236 key.equals("the")) {
237
238 translatedText = "";
239 }
240 else {
241 translatedText = _translate(
242 translationId, key, value, 0);
243
244 if (Validator.isNull(translatedText)) {
245 translatedText = value + AUTOMATIC_COPY;
246 }
247 else {
248 translatedText =
249 translatedText + AUTOMATIC_TRANSLATION;
250 }
251 }
252 }
253
254 if (Validator.isNotNull(translatedText)) {
255 if ((translatedText.indexOf("Babel Fish") != -1) ||
256 (translatedText.indexOf("Yahoo! - 999") != -1)) {
257
258 throw new IOException(
259 "IP was blocked because of over usage. Please " +
260 "use another IP.");
261 }
262
263 if (translatedText.indexOf("'") != -1) {
264 translatedText = StringUtil.replace(
265 translatedText, "'", "\'");
266 }
267
268 translatedText = StringUtil.replace(
269 translatedText.trim(), " ", " ");
270
271 unsyncBufferedWriter.write(key + "=" + translatedText);
272
273 unsyncBufferedWriter.newLine();
274 unsyncBufferedWriter.flush();
275 }
276 }
277 else {
278 unsyncBufferedWriter.write(line);
279
280 unsyncBufferedWriter.newLine();
281 unsyncBufferedWriter.flush();
282 }
283 }
284
285 unsyncBufferedReader.close();
286 unsyncBufferedWriter.close();
287 }
288
289 private String _orderProperties(File propertiesFile) throws IOException {
290 String content = FileUtil.read(propertiesFile);
291
292 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
293 new UnsyncStringReader(content));
294 UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter(
295 new FileWriter(propertiesFile));
296
297 Set<String> messages = new TreeSet<String>();
298
299 boolean begin = false;
300
301 String line = null;
302
303 while ((line = unsyncBufferedReader.readLine()) != null) {
304 int pos = line.indexOf("=");
305
306 if (pos != -1) {
307 String key = line.substring(0, pos);
308 String value = line.substring(pos + 1, line.length());
309
310 messages.add(key + "=" + value);
311 }
312 else {
313 if (begin == true && line.equals("")) {
314 _sortAndWrite(unsyncBufferedWriter, messages);
315 }
316
317 if (line.equals("")) {
318 begin = !begin;
319 }
320
321 unsyncBufferedWriter.write(line);
322 unsyncBufferedWriter.newLine();
323 }
324
325 unsyncBufferedWriter.flush();
326 }
327
328 if (messages.size() > 0) {
329 _sortAndWrite(unsyncBufferedWriter, messages);
330 }
331
332 unsyncBufferedReader.close();
333 unsyncBufferedWriter.close();
334
335 return FileUtil.read(propertiesFile);
336 }
337
338 private void _sortAndWrite(
339 UnsyncBufferedWriter unsyncBufferedWriter, Set<String> messages)
340 throws IOException {
341
342 String[] messagesArray = messages.toArray(new String[messages.size()]);
343
344 for (int i = 0; i < messagesArray.length; i++) {
345 unsyncBufferedWriter.write(messagesArray[i]);
346 unsyncBufferedWriter.newLine();
347 }
348
349 messages.clear();
350 }
351
352 private String _translate(
353 String translationId, String key, String fromText, int limit) {
354
355 if (translationId.equals("en_ar") ||
356 translationId.equals("en_eu") ||
357 translationId.equals("en_ca") ||
358 translationId.equals("en_cs") ||
359 translationId.equals("en_fi") ||
360 translationId.equals("en_hu") ||
361 translationId.equals("en_nb") ||
362 translationId.equals("en_fa") ||
363 translationId.equals("en_pl") ||
364 translationId.equals("en_ru") ||
365 translationId.equals("en_sk") ||
366 translationId.equals("en_sv") ||
367 translationId.equals("en_tr") ||
368 translationId.equals("en_vi")) {
369
370
374 return null;
375 }
376
377
379 if (limit == 3) {
380 return null;
381 }
382
383 String toText = null;
384
385 try {
386 System.out.println(
387 "Translating " + translationId + " " + key + " " + fromText);
388
389 WebCacheItem wci = new TranslationWebCacheItem(
390 translationId, fromText);
391
392 Translation translation = (Translation)wci.convert("");
393
394 toText = translation.getToText();
395
396 if ((toText != null) &&
397 (toText.indexOf("Babel Fish") != -1)) {
398
399 toText = null;
400 }
401 }
402 catch (Exception e) {
403 e.printStackTrace();
404 }
405
406
408 if (toText == null) {
409 return _translate(translationId, key, fromText, ++limit);
410 }
411
412 return toText;
413 }
414
415 private String _langDir;
416 private String _langFile;
417 private Properties _renameKeys;
418
419 }