1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.webcache.WebCacheItem;
29  import com.liferay.portal.util.InitUtil;
30  import com.liferay.portlet.translator.model.Translation;
31  import com.liferay.portlet.translator.util.TranslationWebCacheItem;
32  
33  import java.io.BufferedReader;
34  import java.io.BufferedWriter;
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.FileWriter;
38  import java.io.IOException;
39  import java.io.StringReader;
40  
41  import java.util.Properties;
42  import java.util.Set;
43  import java.util.TreeSet;
44  
45  /**
46   * <a href="LangBuilder.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class LangBuilder {
52  
53      public static void main(String[] args) {
54          InitUtil.initWithSpring();
55  
56          if (args.length == 2) {
57              new LangBuilder(args[0], args[1]);
58          }
59          else {
60              throw new IllegalArgumentException();
61          }
62      }
63  
64      public LangBuilder(String langDir, String langFile) {
65          try {
66              _langDir = langDir;
67              _langFile = langFile;
68  
69              String content = _orderProps(
70                  new File(_langDir + "/" + _langFile + ".properties"));
71  
72              _createProps(content, "ar"); // Arabic
73              _createProps(content, "ca"); // Catalan
74              _createProps(content, "zh_CN"); // Chinese (China)
75              _createProps(content, "zh_TW"); // Chinese (Taiwan)
76              _createProps(content, "cs"); // Czech
77              _createProps(content, "nl"); // Dutch
78              _createProps(content, "fi"); // Finnish
79              _createProps(content, "fr"); // French
80              _createProps(content, "de"); // German
81              _createProps(content, "el"); // Greek
82              _createProps(content, "hu"); // Hungarian
83              _createProps(content, "it"); // Italian
84              _createProps(content, "ja"); // Japanese
85              _createProps(content, "ko"); // Korean
86              _createProps(content, "nb"); // Norwegian Bokmål
87              _createProps(content, "fa"); // Persian
88              _createProps(content, "pl"); // Polish
89              _createProps(content, "pt_BR"); // Brazilian Portuguese
90              _createProps(content, "pt_PT"); // Portuguese
91              _createProps(content, "ru"); // Russian
92              _createProps(content, "es"); // Spanish
93              _createProps(content, "sv"); // Swedish
94              _createProps(content, "tr"); // Turkish
95              _createProps(content, "vi"); // Vietnamese
96          }
97          catch (Exception e) {
98              e.printStackTrace();
99          }
100     }
101 
102     private void _createProps(String content, String languageId)
103         throws IOException {
104 
105         File propsFile = new File(
106             _langDir + "/" + _langFile + "_" + languageId + ".properties");
107 
108         Properties props = new Properties();
109 
110         if (propsFile.exists()) {
111             props.load(new FileInputStream(propsFile));
112         }
113 
114         File nativePropsFile = new File(
115             _langDir + "/" + _langFile + "_" + languageId +
116                 ".properties.native");
117 
118         Properties nativeProps = new Properties();
119 
120         if (nativePropsFile.exists()) {
121             nativeProps.load(new FileInputStream(nativePropsFile));
122         }
123 
124         String translationId = "en_" + languageId;
125 
126         if (translationId.equals("en_pt_BR")) {
127             translationId = "en_pt";
128         }
129         else if (translationId.equals("en_pt_PT")) {
130             translationId = "en_pt";
131         }
132         else if (translationId.equals("en_zh_CN")) {
133             translationId = "en_zh";
134         }
135         else if (translationId.equals("en_zh_TW")) {
136             translationId = "en_zt";
137         }
138 
139         BufferedReader br = new BufferedReader(new StringReader(content));
140         BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
141 
142         String line = null;
143 
144         while ((line = br.readLine()) != null) {
145             line = line.trim();
146 
147             int pos = line.indexOf("=");
148 
149             if (pos != -1) {
150                 String key = line.substring(0, pos);
151                 String value = line.substring(pos + 1, line.length());
152 
153                 String translatedText = props.getProperty(key);
154 
155                 if ((translatedText != null) &&
156                     ((translatedText.indexOf("Babel Fish") != -1) ||
157                      (translatedText.indexOf("Yahoo! - 999") != -1))) {
158 
159                     translatedText = "";
160                 }
161 
162                 if ((translatedText == null) || translatedText.equals("")) {
163                     if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
164                         translatedText = value;
165                     }
166                     else if (key.equals("lang.dir")) {
167                         translatedText = "ltr";
168                     }
169                     else if (key.equals("lang.line.begin")) {
170                         translatedText = "left";
171                     }
172                     else if (key.equals("lang.line.end")) {
173                         translatedText = "right";
174                     }
175                     else {
176                         translatedText = _translate(translationId, value, 0);
177                     }
178                 }
179 
180                 if (Validator.isNotNull(translatedText)) {
181                     if (translatedText.indexOf("&#39;") != -1) {
182                         translatedText = StringUtil.replace(
183                             translatedText, "&#39;", "\'");
184                     }
185 
186                     bw.write(key + "=" + translatedText);
187 
188                     bw.newLine();
189                     bw.flush();
190                 }
191                 else if (nativeProps.containsKey(key)) {
192                     bw.write(key + "=");
193 
194                     bw.newLine();
195                     bw.flush();
196                 }
197             }
198             else {
199                 bw.write(line);
200 
201                 bw.newLine();
202                 bw.flush();
203             }
204         }
205 
206         br.close();
207         bw.close();
208     }
209 
210     private String _orderProps(File propsFile) throws IOException {
211         String content = FileUtil.read(propsFile);
212 
213         BufferedReader br = new BufferedReader(new StringReader(content));
214         BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
215 
216         Set<String> messages = new TreeSet<String>();
217 
218         boolean begin = false;
219 
220         String line = null;
221 
222         while ((line = br.readLine()) != null) {
223             int pos = line.indexOf("=");
224 
225             if (pos != -1) {
226                 String key = line.substring(0, pos);
227                 String value = line.substring(pos + 1, line.length());
228 
229                 messages.add(key + "=" + value);
230             }
231             else {
232                 if (begin == true && line.equals("")) {
233                     _sortAndWrite(bw, messages);
234                 }
235 
236                 if (line.equals("")) {
237                     begin = !begin;
238                 }
239 
240                 bw.write(line);
241                 bw.newLine();
242             }
243 
244             bw.flush();
245         }
246 
247         if (messages.size() > 0) {
248             _sortAndWrite(bw, messages);
249         }
250 
251         br.close();
252         bw.close();
253 
254         return FileUtil.read(propsFile);
255     }
256 
257     private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
258         throws IOException {
259 
260         String[] messagesArray = messages.toArray(new String[messages.size()]);
261 
262         for (int i = 0; i < messagesArray.length; i++) {
263             bw.write(messagesArray[i]);
264             bw.newLine();
265         }
266 
267         messages.clear();
268     }
269 
270     private String _translate(
271         String translationId, String fromText, int limit) {
272 
273         if (translationId.equals("en_ar") ||
274             translationId.equals("en_ca") ||
275             translationId.equals("en_cs") ||
276             translationId.equals("en_fi") ||
277             translationId.equals("en_hu") ||
278             translationId.equals("en_nb") ||
279             translationId.equals("en_fa") ||
280             translationId.equals("en_pl") ||
281             translationId.equals("en_ru") ||
282             translationId.equals("en_sv") ||
283             translationId.equals("en_tr") ||
284             translationId.equals("en_vi")) {
285 
286             // Automatic translator does not support Arabic, Catalan, Czech,
287             // Finnish, Hungarian, Norwegian Bokmål, Persian, Polish, Russian,
288             // Swedish, Turkish, or Vietnamese
289 
290             return null;
291         }
292 
293         // Limit the number of retries to 3
294 
295         if (limit == 3) {
296             return null;
297         }
298 
299         String toText = null;
300 
301         try {
302             System.out.println("Translating " + translationId + " " + fromText);
303 
304             WebCacheItem wci = new TranslationWebCacheItem(
305                 translationId, fromText);
306 
307             Translation translation = (Translation)wci.convert("");
308 
309             toText = translation.getToText();
310 
311             if ((toText != null) &&
312                 (toText.indexOf("Babel Fish") != -1)) {
313 
314                 toText = null;
315             }
316         }
317         catch (Exception e) {
318             e.printStackTrace();
319         }
320 
321         // Keep trying
322 
323         if (toText == null) {
324             return _translate(translationId, fromText, ++limit);
325         }
326 
327         return toText;
328     }
329 
330     private String _langDir;
331     private String _langFile;
332 
333 }