1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
18  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedWriter;
19  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
20  import com.liferay.portal.kernel.util.FileUtil;
21  import com.liferay.portal.kernel.util.PropertiesUtil;
22  import com.liferay.portal.kernel.util.StringUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.kernel.webcache.WebCacheItem;
25  import com.liferay.portal.util.InitUtil;
26  import com.liferay.portlet.translator.model.Translation;
27  import com.liferay.portlet.translator.util.TranslationWebCacheItem;
28  
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.FileWriter;
32  import java.io.IOException;
33  
34  import java.util.Properties;
35  import java.util.Set;
36  import java.util.TreeSet;
37  
38  /**
39   * <a href="LangBuilder.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class LangBuilder {
44  
45      public static void main(String[] args) {
46          InitUtil.initWithSpring();
47  
48          if (args.length == 2) {
49              new LangBuilder(args[0], args[1], null);
50          }
51          else if (args.length == 3) {
52              new LangBuilder(args[0], args[1], args[2]);
53          }
54          else {
55              throw new IllegalArgumentException();
56          }
57      }
58  
59      public LangBuilder(String langDir, String langFile, String langCode) {
60          try {
61              _langDir = langDir;
62              _langFile = langFile;
63  
64              File renameKeysFile = new File(_langDir + "/rename.properties");
65  
66              if (renameKeysFile.exists()) {
67                  _renameKeys = PropertiesUtil.load(
68                      FileUtil.read(renameKeysFile));
69              }
70  
71              String content = _orderProps(
72                  new File(_langDir + "/" + _langFile + ".properties"));
73  
74              if (Validator.isNotNull(langCode) && !langCode.startsWith("$")) {
75                  _createProps(content, langCode);
76              }
77              else {
78                  _createProps(content, "ar"); // Arabic
79                  _createProps(content, "ca"); // Catalan
80                  _createProps(content, "zh_CN"); // Chinese (China)
81                  _createProps(content, "zh_TW"); // Chinese (Taiwan)
82                  _createProps(content, "cs"); // Czech
83                  _createProps(content, "nl"); // Dutch
84                  _createProps(content, "fi"); // Finnish
85                  _createProps(content, "fr"); // French
86                  _createProps(content, "de"); // German
87                  _createProps(content, "el"); // Greek
88                  _createProps(content, "hu"); // Hungarian
89                  _createProps(content, "it"); // Italian
90                  _createProps(content, "ja"); // Japanese
91                  _createProps(content, "ko"); // Korean
92                  _createProps(content, "nb"); // Norwegian Bokmål
93                  _createProps(content, "fa"); // Persian
94                  _createProps(content, "pt_BR"); // Brazilian Portuguese
95                  _createProps(content, "pt_PT"); // Portuguese
96                  _createProps(content, "ru"); // Russian
97                  _createProps(content, "es"); // Spanish
98                  _createProps(content, "sv"); // Swedish
99                  _createProps(content, "tr"); // Turkish
100                 _createProps(content, "vi"); // Vietnamese
101             }
102         }
103         catch (Exception e) {
104             e.printStackTrace();
105         }
106     }
107 
108     private void _createProps(String content, String languageId)
109         throws IOException {
110 
111         File propsFile = new File(
112             _langDir + "/" + _langFile + "_" + languageId + ".properties");
113 
114         Properties props = new Properties();
115 
116         if (propsFile.exists()) {
117             props.load(new FileInputStream(propsFile));
118         }
119 
120         File nativePropsFile = new File(
121             _langDir + "/" + _langFile + "_" + languageId +
122                 ".properties.native");
123 
124         Properties nativeProps = new Properties();
125 
126         if (nativePropsFile.exists()) {
127             nativeProps.load(new FileInputStream(nativePropsFile));
128         }
129 
130         String translationId = "en_" + languageId;
131 
132         if (translationId.equals("en_pt_BR")) {
133             translationId = "en_pt";
134         }
135         else if (translationId.equals("en_pt_PT")) {
136             translationId = "en_pt";
137         }
138         else if (translationId.equals("en_zh_CN")) {
139             translationId = "en_zh";
140         }
141         else if (translationId.equals("en_zh_TW")) {
142             translationId = "en_zt";
143         }
144 
145         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
146             new UnsyncStringReader(content));
147         UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter(
148             new FileWriter(nativePropsFile));
149 
150         String line = null;
151 
152         while ((line = unsyncBufferedReader.readLine()) != null) {
153             line = line.trim();
154 
155             int pos = line.indexOf("=");
156 
157             if (pos != -1) {
158                 String key = line.substring(0, pos);
159                 String value = line.substring(pos + 1, line.length());
160 
161                 String translatedText = props.getProperty(key);
162 
163                 if ((translatedText == null) && (_renameKeys != null)) {
164                     String renameKey = _renameKeys.getProperty(key);
165 
166                     if (renameKey != null) {
167                         translatedText = props.getProperty(renameKey);
168                     }
169                 }
170 
171                 if ((translatedText != null) &&
172                     ((translatedText.indexOf("Babel Fish") != -1) ||
173                      (translatedText.indexOf("Yahoo! - 999") != -1))) {
174 
175                     translatedText = "";
176                 }
177 
178                 if ((translatedText == null) || translatedText.equals("")) {
179                     if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
180                         translatedText = value;
181                     }
182                     else if (key.equals("lang.dir")) {
183                         translatedText = "ltr";
184                     }
185                     else if (key.equals("lang.line.begin")) {
186                         translatedText = "left";
187                     }
188                     else if (key.equals("lang.line.end")) {
189                         translatedText = "right";
190                     }
191                     else if (translationId.equals("en_el") &&
192                              (key.equals("enabled") || key.equals("on") ||
193                               key.equals("on-date"))) {
194 
195                         translatedText = "";
196                     }
197                     else if (translationId.equals("en_es") &&
198                              key.equals("am")) {
199 
200                         translatedText = "";
201                     }
202                     else if (translationId.equals("en_it") &&
203                              key.equals("am")) {
204 
205                         translatedText = "";
206                     }
207                     else if (translationId.equals("en_ja") &&
208                              (key.equals("any") || key.equals("anytime") ||
209                               key.equals("down") || key.equals("on") ||
210                               key.equals("on-date") || key.equals("the"))) {
211 
212                         translatedText = "";
213                     }
214                     else if (translationId.equals("en_ko") &&
215                              key.equals("the")) {
216 
217                         translatedText = "";
218                     }
219                     else {
220                         translatedText = _translate(
221                             translationId, key, value, 0);
222                     }
223                 }
224 
225                 if (Validator.isNotNull(translatedText)) {
226                     if ((translatedText.indexOf("Babel Fish") != -1) ||
227                         (translatedText.indexOf("Yahoo! - 999") != -1)) {
228 
229                         throw new IOException(
230                             "IP was blocked because of over usage. Please " +
231                                 "use another IP.");
232                     }
233 
234                     if (translatedText.indexOf("&#39;") != -1) {
235                         translatedText = StringUtil.replace(
236                             translatedText, "&#39;", "\'");
237                     }
238 
239                     unsyncBufferedWriter.write(key + "=" + translatedText);
240 
241                     unsyncBufferedWriter.newLine();
242                     unsyncBufferedWriter.flush();
243                 }
244                 else if (nativeProps.containsKey(key)) {
245                     unsyncBufferedWriter.write(key + "=");
246 
247                     unsyncBufferedWriter.newLine();
248                     unsyncBufferedWriter.flush();
249                 }
250             }
251             else {
252                 unsyncBufferedWriter.write(line);
253 
254                 unsyncBufferedWriter.newLine();
255                 unsyncBufferedWriter.flush();
256             }
257         }
258 
259         unsyncBufferedReader.close();
260         unsyncBufferedWriter.close();
261     }
262 
263     private String _orderProps(File propsFile) throws IOException {
264         String content = FileUtil.read(propsFile);
265 
266         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
267             new UnsyncStringReader(content));
268         UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter(
269             new FileWriter(propsFile));
270 
271         Set<String> messages = new TreeSet<String>();
272 
273         boolean begin = false;
274 
275         String line = null;
276 
277         while ((line = unsyncBufferedReader.readLine()) != null) {
278             int pos = line.indexOf("=");
279 
280             if (pos != -1) {
281                 String key = line.substring(0, pos);
282                 String value = line.substring(pos + 1, line.length());
283 
284                 messages.add(key + "=" + value);
285             }
286             else {
287                 if (begin == true && line.equals("")) {
288                     _sortAndWrite(unsyncBufferedWriter, messages);
289                 }
290 
291                 if (line.equals("")) {
292                     begin = !begin;
293                 }
294 
295                 unsyncBufferedWriter.write(line);
296                 unsyncBufferedWriter.newLine();
297             }
298 
299             unsyncBufferedWriter.flush();
300         }
301 
302         if (messages.size() > 0) {
303             _sortAndWrite(unsyncBufferedWriter, messages);
304         }
305 
306         unsyncBufferedReader.close();
307         unsyncBufferedWriter.close();
308 
309         return FileUtil.read(propsFile);
310     }
311 
312     private void _sortAndWrite(
313             UnsyncBufferedWriter unsyncBufferedWriter, Set<String> messages)
314         throws IOException {
315 
316         String[] messagesArray = messages.toArray(new String[messages.size()]);
317 
318         for (int i = 0; i < messagesArray.length; i++) {
319             unsyncBufferedWriter.write(messagesArray[i]);
320             unsyncBufferedWriter.newLine();
321         }
322 
323         messages.clear();
324     }
325 
326     private String _translate(
327         String translationId, String key, String fromText, int limit) {
328 
329         if (translationId.equals("en_ar") ||
330             translationId.equals("en_ca") ||
331             translationId.equals("en_cs") ||
332             translationId.equals("en_fi") ||
333             translationId.equals("en_hu") ||
334             translationId.equals("en_nb") ||
335             translationId.equals("en_fa") ||
336             translationId.equals("en_ru") ||
337             translationId.equals("en_sv") ||
338             translationId.equals("en_tr") ||
339             translationId.equals("en_vi")) {
340 
341             // Automatic translator does not support Arabic, Catalan, Czech,
342             // Finnish, Hungarian, Norwegian Bokmål, Persian, Russian, Swedish,
343             // Turkish, or Vietnamese
344 
345             return null;
346         }
347 
348         // Limit the number of retries to 3
349 
350         if (limit == 3) {
351             return null;
352         }
353 
354         String toText = null;
355 
356         try {
357             System.out.println(
358                 "Translating " + translationId + " " + key + " " + fromText);
359 
360             WebCacheItem wci = new TranslationWebCacheItem(
361                 translationId, fromText);
362 
363             Translation translation = (Translation)wci.convert("");
364 
365             toText = translation.getToText();
366 
367             if ((toText != null) &&
368                 (toText.indexOf("Babel Fish") != -1)) {
369 
370                 toText = null;
371             }
372         }
373         catch (Exception e) {
374             e.printStackTrace();
375         }
376 
377         // Keep trying
378 
379         if (toText == null) {
380             return _translate(translationId, key, fromText, ++limit);
381         }
382 
383         return toText;
384     }
385 
386     private String _langDir;
387     private String _langFile;
388     private Properties _renameKeys;
389 
390 }