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.portlet.translator.model.Translation;
29  import com.liferay.portlet.translator.util.TranslationWebCacheItem;
30  import com.liferay.util.FileUtil;
31  
32  import java.io.BufferedReader;
33  import java.io.BufferedWriter;
34  import java.io.File;
35  import java.io.FileInputStream;
36  import java.io.FileWriter;
37  import java.io.IOException;
38  import java.io.StringReader;
39  
40  import java.util.Properties;
41  import java.util.Set;
42  import java.util.TreeSet;
43  
44  /**
45   * <a href="LangBuilder.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class LangBuilder {
51  
52      public static void main(String[] args) {
53          if (args.length == 2) {
54              new LangBuilder(args[0], args[1]);
55          }
56          else {
57              throw new IllegalArgumentException();
58          }
59      }
60  
61      public LangBuilder(String langDir, String langFile) {
62          try {
63              _langDir = langDir;
64              _langFile = langFile;
65  
66              String content = _orderProps(
67                  new File(_langDir + "/" + _langFile + ".properties"));
68  
69              _createProps(content, "ar"); // Arabic
70              _createProps(content, "ca"); // Catalan
71              _createProps(content, "zh_CN"); // Chinese (China)
72              _createProps(content, "zh_TW"); // Chinese (Taiwan)
73              _createProps(content, "cs"); // Czech
74              _createProps(content, "nl"); // Dutch
75              _createProps(content, "fi"); // Finnish
76              _createProps(content, "fr"); // French
77              _createProps(content, "de"); // German
78              _createProps(content, "el"); // Greek
79              _createProps(content, "hu"); // Hungarian
80              _createProps(content, "it"); // Italian
81              _createProps(content, "ja"); // Japanese
82              _createProps(content, "ko"); // Korean
83              _createProps(content, "fa"); // Persian
84              _createProps(content, "pt"); // Portuguese
85              _createProps(content, "ru"); // Russian
86              _createProps(content, "es"); // Spanish
87              _createProps(content, "sv"); // Swedish
88              _createProps(content, "tr"); // Turkish
89              _createProps(content, "vi"); // Vietnamese
90          }
91          catch (Exception e) {
92              e.printStackTrace();
93          }
94      }
95  
96      private void _createProps(String content, String languageId)
97          throws IOException {
98  
99          File propsFile = new File(
100             _langDir + "/" + _langFile + "_" + languageId + ".properties");
101 
102         Properties props = new Properties();
103 
104         if (propsFile.exists()) {
105             props.load(new FileInputStream(propsFile));
106         }
107 
108         File nativePropsFile = new File(
109             _langDir + "/" + _langFile + "_" + languageId +
110                 ".properties.native");
111 
112         Properties nativeProps = new Properties();
113 
114         if (nativePropsFile.exists()) {
115             nativeProps.load(new FileInputStream(nativePropsFile));
116         }
117 
118         String translationId = "en_" + languageId;
119 
120         if (translationId.equals("en_zh_CN")) {
121             translationId = "en_zh";
122         }
123         else if (translationId.equals("en_zh_TW")) {
124             translationId = "en_zt";
125         }
126 
127         BufferedReader br = new BufferedReader(new StringReader(content));
128         BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
129 
130         String line = null;
131 
132         while ((line = br.readLine()) != null) {
133             line = line.trim();
134 
135             int pos = line.indexOf("=");
136 
137             if (pos != -1) {
138                 String key = line.substring(0, pos);
139                 String value = line.substring(pos + 1, line.length());
140 
141                 String translatedText = props.getProperty(key);
142 
143                 if ((translatedText != null) &&
144                     ((translatedText.indexOf("Babel Fish") != -1) ||
145                      (translatedText.indexOf("Yahoo! - 999") != -1))) {
146 
147                     translatedText = "";
148                 }
149 
150                 if ((translatedText == null) || translatedText.equals("")) {
151                     if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
152                         translatedText = value;
153                     }
154                     else if (key.equals("lang.dir")) {
155                         translatedText = "ltr";
156                     }
157                     else if (key.equals("lang.line.begin")) {
158                         translatedText = "left";
159                     }
160                     else if (key.equals("lang.line.end")) {
161                         translatedText = "right";
162                     }
163                     else {
164                         translatedText = _translate(translationId, value, 0);
165                     }
166                 }
167 
168                 if (Validator.isNotNull(translatedText)) {
169                     if (translatedText.indexOf("&#39;") != -1) {
170                         translatedText =
171                             StringUtil.replace(translatedText, "&#39;", "\'");
172                     }
173 
174                     bw.write(key + "=" + translatedText);
175 
176                     bw.newLine();
177                     bw.flush();
178                 }
179                 else if (nativeProps.containsKey(key)) {
180                     bw.write(key + "=");
181 
182                     bw.newLine();
183                     bw.flush();
184                 }
185             }
186             else {
187                 bw.write(line);
188 
189                 bw.newLine();
190                 bw.flush();
191             }
192         }
193 
194         br.close();
195         bw.close();
196     }
197 
198     private String _orderProps(File propsFile) throws IOException {
199         String content = FileUtil.read(propsFile);
200 
201         BufferedReader br = new BufferedReader(new StringReader(content));
202         BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
203 
204         Set messages = new TreeSet();
205 
206         boolean begin = false;
207 
208         String line = null;
209 
210         while ((line = br.readLine()) != null) {
211             int pos = line.indexOf("=");
212 
213             if (pos != -1) {
214                 String key = line.substring(0, pos);
215                 String value = line.substring(pos + 1, line.length());
216 
217                 messages.add(key + "=" + value);
218             }
219             else {
220                 if (begin == true && line.equals("")) {
221                     _sortAndWrite(bw, messages);
222                 }
223 
224                 if (line.equals("")) {
225                     begin = !begin;
226                 }
227 
228                 bw.write(line);
229                 bw.newLine();
230             }
231 
232             bw.flush();
233         }
234 
235         if (messages.size() > 0) {
236             _sortAndWrite(bw, messages);
237         }
238 
239         br.close();
240         bw.close();
241 
242         return FileUtil.read(propsFile);
243     }
244 
245     private void _sortAndWrite(BufferedWriter bw, Set messages)
246         throws IOException {
247 
248         String[] messagesArray = (String[])messages.toArray(new String[0]);
249 
250         for (int i = 0; i < messagesArray.length; i++) {
251             bw.write(messagesArray[i]);
252             bw.newLine();
253         }
254 
255         messages.clear();
256     }
257 
258     private String _translate(
259         String translationId, String fromText, int limit) {
260 
261         if (translationId.equals("en_ar") ||
262             translationId.equals("en_ca") ||
263             translationId.equals("en_cs") ||
264             translationId.equals("en_fi") ||
265             translationId.equals("en_hu") ||
266             translationId.equals("en_fa") ||
267             translationId.equals("en_ru") ||
268             translationId.equals("en_sv") ||
269             translationId.equals("en_tr") ||
270             translationId.equals("en_vi")) {
271 
272             // Automatic translator does not support Arabic, Catalan, Czech,
273             // Finnish, Hungarian, Persian, Russian, Swedish, Turkish,
274             // or Vietnamese
275 
276             return null;
277         }
278 
279         // Limit the number of retries to 3
280 
281         if (limit == 3) {
282             return null;
283         }
284 
285         String toText = null;
286 
287         try {
288             System.out.println("Translating " + translationId + " " + fromText);
289 
290             WebCacheItem wci = new TranslationWebCacheItem(
291                 translationId, fromText);
292 
293             Translation translation = (Translation)wci.convert("");
294 
295             toText = translation.getToText();
296 
297             if ((toText != null) &&
298                 (toText.indexOf("Babel Fish") != -1)) {
299 
300                 toText = null;
301             }
302         }
303         catch (Exception e) {
304             e.printStackTrace();
305         }
306 
307         // Keep trying
308 
309         if (toText == null) {
310             return _translate(translationId, fromText, ++limit);
311         }
312 
313         return toText;
314     }
315 
316     private String _langDir;
317     private String _langFile;
318 
319 }