1   /**
2    * Copyright (c) 2000-2008 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.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.kernel.webcache.WebCacheItem;
28  import com.liferay.portal.util.InitUtil;
29  import com.liferay.portlet.translator.model.Translation;
30  import com.liferay.portlet.translator.util.TranslationWebCacheItem;
31  import com.liferay.util.FileUtil;
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      static {
54          InitUtil.init();
55      }
56  
57      public static void main(String[] args) {
58          if (args.length == 2) {
59              new LangBuilder(args[0], args[1]);
60          }
61          else {
62              throw new IllegalArgumentException();
63          }
64      }
65  
66      public LangBuilder(String langDir, String langFile) {
67          try {
68              _langDir = langDir;
69              _langFile = langFile;
70  
71              String content = _orderProps(
72                  new File(_langDir + "/" + _langFile + ".properties"));
73  
74              _createProps(content, "ar"); // Arabic
75              _createProps(content, "ca"); // Catalan
76              _createProps(content, "zh_CN"); // Chinese (China)
77              _createProps(content, "zh_TW"); // Chinese (Taiwan)
78              _createProps(content, "cs"); // Czech
79              _createProps(content, "nl"); // Dutch
80              _createProps(content, "fi"); // Finnish
81              _createProps(content, "fr"); // French
82              _createProps(content, "de"); // German
83              _createProps(content, "el"); // Greek
84              _createProps(content, "hu"); // Hungarian
85              _createProps(content, "it"); // Italian
86              _createProps(content, "ja"); // Japanese
87              _createProps(content, "ko"); // Korean
88              _createProps(content, "nb"); // Norwegian Bokmål
89              _createProps(content, "fa"); // Persian
90              _createProps(content, "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_zh_CN")) {
127             translationId = "en_zh";
128         }
129         else if (translationId.equals("en_zh_TW")) {
130             translationId = "en_zt";
131         }
132 
133         BufferedReader br = new BufferedReader(new StringReader(content));
134         BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
135 
136         String line = null;
137 
138         while ((line = br.readLine()) != null) {
139             line = line.trim();
140 
141             int pos = line.indexOf("=");
142 
143             if (pos != -1) {
144                 String key = line.substring(0, pos);
145                 String value = line.substring(pos + 1, line.length());
146 
147                 String translatedText = props.getProperty(key);
148 
149                 if ((translatedText != null) &&
150                     ((translatedText.indexOf("Babel Fish") != -1) ||
151                      (translatedText.indexOf("Yahoo! - 999") != -1))) {
152 
153                     translatedText = "";
154                 }
155 
156                 if ((translatedText == null) || translatedText.equals("")) {
157                     if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
158                         translatedText = value;
159                     }
160                     else if (key.equals("lang.dir")) {
161                         translatedText = "ltr";
162                     }
163                     else if (key.equals("lang.line.begin")) {
164                         translatedText = "left";
165                     }
166                     else if (key.equals("lang.line.end")) {
167                         translatedText = "right";
168                     }
169                     else {
170                         translatedText = _translate(translationId, value, 0);
171                     }
172                 }
173 
174                 if (Validator.isNotNull(translatedText)) {
175                     if (translatedText.indexOf("&#39;") != -1) {
176                         translatedText =
177                             StringUtil.replace(translatedText, "&#39;", "\'");
178                     }
179 
180                     bw.write(key + "=" + translatedText);
181 
182                     bw.newLine();
183                     bw.flush();
184                 }
185                 else if (nativeProps.containsKey(key)) {
186                     bw.write(key + "=");
187 
188                     bw.newLine();
189                     bw.flush();
190                 }
191             }
192             else {
193                 bw.write(line);
194 
195                 bw.newLine();
196                 bw.flush();
197             }
198         }
199 
200         br.close();
201         bw.close();
202     }
203 
204     private String _orderProps(File propsFile) throws IOException {
205         String content = FileUtil.read(propsFile);
206 
207         BufferedReader br = new BufferedReader(new StringReader(content));
208         BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
209 
210         Set<String> messages = new TreeSet<String>();
211 
212         boolean begin = false;
213 
214         String line = null;
215 
216         while ((line = br.readLine()) != null) {
217             int pos = line.indexOf("=");
218 
219             if (pos != -1) {
220                 String key = line.substring(0, pos);
221                 String value = line.substring(pos + 1, line.length());
222 
223                 messages.add(key + "=" + value);
224             }
225             else {
226                 if (begin == true && line.equals("")) {
227                     _sortAndWrite(bw, messages);
228                 }
229 
230                 if (line.equals("")) {
231                     begin = !begin;
232                 }
233 
234                 bw.write(line);
235                 bw.newLine();
236             }
237 
238             bw.flush();
239         }
240 
241         if (messages.size() > 0) {
242             _sortAndWrite(bw, messages);
243         }
244 
245         br.close();
246         bw.close();
247 
248         return FileUtil.read(propsFile);
249     }
250 
251     private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
252         throws IOException {
253 
254         String[] messagesArray = messages.toArray(new String[messages.size()]);
255 
256         for (int i = 0; i < messagesArray.length; i++) {
257             bw.write(messagesArray[i]);
258             bw.newLine();
259         }
260 
261         messages.clear();
262     }
263 
264     private String _translate(
265         String translationId, String fromText, int limit) {
266 
267         if (translationId.equals("en_ar") ||
268             translationId.equals("en_ca") ||
269             translationId.equals("en_cs") ||
270             translationId.equals("en_fi") ||
271             translationId.equals("en_hu") ||
272             translationId.equals("en_nb") ||
273             translationId.equals("en_fa") ||
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, Russian, Swedish,
281             // 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 }