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