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"); // Portuguese
90              _createProps(content, "ru"); // Russian
91              _createProps(content, "es"); // Spanish
92              _createProps(content, "sv"); // Swedish
93              _createProps(content, "tr"); // Turkish
94              _createProps(content, "vi"); // Vietnamese
95          }
96          catch (Exception e) {
97              e.printStackTrace();
98          }
99      }
100 
101     private void _createProps(String content, String languageId)
102         throws IOException {
103 
104         File propsFile = new File(
105             _langDir + "/" + _langFile + "_" + languageId + ".properties");
106 
107         Properties props = new Properties();
108 
109         if (propsFile.exists()) {
110             props.load(new FileInputStream(propsFile));
111         }
112 
113         File nativePropsFile = new File(
114             _langDir + "/" + _langFile + "_" + languageId +
115                 ".properties.native");
116 
117         Properties nativeProps = new Properties();
118 
119         if (nativePropsFile.exists()) {
120             nativeProps.load(new FileInputStream(nativePropsFile));
121         }
122 
123         String translationId = "en_" + languageId;
124 
125         if (translationId.equals("en_zh_CN")) {
126             translationId = "en_zh";
127         }
128         else if (translationId.equals("en_zh_TW")) {
129             translationId = "en_zt";
130         }
131 
132         BufferedReader br = new BufferedReader(new StringReader(content));
133         BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
134 
135         String line = null;
136 
137         while ((line = br.readLine()) != null) {
138             line = line.trim();
139 
140             int pos = line.indexOf("=");
141 
142             if (pos != -1) {
143                 String key = line.substring(0, pos);
144                 String value = line.substring(pos + 1, line.length());
145 
146                 String translatedText = props.getProperty(key);
147 
148                 if ((translatedText != null) &&
149                     ((translatedText.indexOf("Babel Fish") != -1) ||
150                      (translatedText.indexOf("Yahoo! - 999") != -1))) {
151 
152                     translatedText = "";
153                 }
154 
155                 if ((translatedText == null) || translatedText.equals("")) {
156                     if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
157                         translatedText = value;
158                     }
159                     else if (key.equals("lang.dir")) {
160                         translatedText = "ltr";
161                     }
162                     else if (key.equals("lang.line.begin")) {
163                         translatedText = "left";
164                     }
165                     else if (key.equals("lang.line.end")) {
166                         translatedText = "right";
167                     }
168                     else {
169                         translatedText = _translate(translationId, value, 0);
170                     }
171                 }
172 
173                 if (Validator.isNotNull(translatedText)) {
174                     if (translatedText.indexOf("&#39;") != -1) {
175                         translatedText = StringUtil.replace(
176                             translatedText, "&#39;", "\'");
177                     }
178 
179                     bw.write(key + "=" + translatedText);
180 
181                     bw.newLine();
182                     bw.flush();
183                 }
184                 else if (nativeProps.containsKey(key)) {
185                     bw.write(key + "=");
186 
187                     bw.newLine();
188                     bw.flush();
189                 }
190             }
191             else {
192                 bw.write(line);
193 
194                 bw.newLine();
195                 bw.flush();
196             }
197         }
198 
199         br.close();
200         bw.close();
201     }
202 
203     private String _orderProps(File propsFile) throws IOException {
204         String content = FileUtil.read(propsFile);
205 
206         BufferedReader br = new BufferedReader(new StringReader(content));
207         BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
208 
209         Set<String> messages = new TreeSet<String>();
210 
211         boolean begin = false;
212 
213         String line = null;
214 
215         while ((line = br.readLine()) != null) {
216             int pos = line.indexOf("=");
217 
218             if (pos != -1) {
219                 String key = line.substring(0, pos);
220                 String value = line.substring(pos + 1, line.length());
221 
222                 messages.add(key + "=" + value);
223             }
224             else {
225                 if (begin == true && line.equals("")) {
226                     _sortAndWrite(bw, messages);
227                 }
228 
229                 if (line.equals("")) {
230                     begin = !begin;
231                 }
232 
233                 bw.write(line);
234                 bw.newLine();
235             }
236 
237             bw.flush();
238         }
239 
240         if (messages.size() > 0) {
241             _sortAndWrite(bw, messages);
242         }
243 
244         br.close();
245         bw.close();
246 
247         return FileUtil.read(propsFile);
248     }
249 
250     private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
251         throws IOException {
252 
253         String[] messagesArray = messages.toArray(new String[messages.size()]);
254 
255         for (int i = 0; i < messagesArray.length; i++) {
256             bw.write(messagesArray[i]);
257             bw.newLine();
258         }
259 
260         messages.clear();
261     }
262 
263     private String _translate(
264         String translationId, String fromText, int limit) {
265 
266         if (translationId.equals("en_ar") ||
267             translationId.equals("en_ca") ||
268             translationId.equals("en_cs") ||
269             translationId.equals("en_fi") ||
270             translationId.equals("en_hu") ||
271             translationId.equals("en_nb") ||
272             translationId.equals("en_fa") ||
273             translationId.equals("en_pl") ||
274             translationId.equals("en_ru") ||
275             translationId.equals("en_sv") ||
276             translationId.equals("en_tr") ||
277             translationId.equals("en_vi")) {
278 
279             // Automatic translator does not support Arabic, Catalan, Czech,
280             // Finnish, Hungarian, Norwegian Bokmål, Persian, Polish, Russian,
281             // Swedish, Turkish, or Vietnamese
282 
283             return null;
284         }
285 
286         // Limit the number of retries to 3
287 
288         if (limit == 3) {
289             return null;
290         }
291 
292         String toText = null;
293 
294         try {
295             System.out.println("Translating " + translationId + " " + fromText);
296 
297             WebCacheItem wci = new TranslationWebCacheItem(
298                 translationId, fromText);
299 
300             Translation translation = (Translation)wci.convert("");
301 
302             toText = translation.getToText();
303 
304             if ((toText != null) &&
305                 (toText.indexOf("Babel Fish") != -1)) {
306 
307                 toText = null;
308             }
309         }
310         catch (Exception e) {
311             e.printStackTrace();
312         }
313 
314         // Keep trying
315 
316         if (toText == null) {
317             return _translate(translationId, fromText, ++limit);
318         }
319 
320         return toText;
321     }
322 
323     private String _langDir;
324     private String _langFile;
325 
326 }