1   /**
2    * Copyright (c) 2000-2007 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.util.WebCacheable;
28  import com.liferay.portlet.translator.model.Translation;
29  import com.liferay.portlet.translator.util.TranslationConverter;
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 
146                     translatedText = "";
147                 }
148 
149                 if (translatedText == null || translatedText.equals("")) {
150                     if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
151                         translatedText = value;
152                     }
153                     else if (key.equals("lang.dir")) {
154                         translatedText = "ltr";
155                     }
156                     else if (key.equals("lang.line.begin")) {
157                         translatedText = "left";
158                     }
159                     else if (key.equals("lang.line.end")) {
160                         translatedText = "right";
161                     }
162                     else {
163                         translatedText = _translate(translationId, value, 0);
164                     }
165                 }
166 
167                 if (Validator.isNotNull(translatedText)) {
168                     if (translatedText.indexOf("&#39;") != -1) {
169                         translatedText =
170                             StringUtil.replace(translatedText, "&#39;", "\'");
171                     }
172 
173                     bw.write(key + "=" + translatedText);
174 
175                     bw.newLine();
176                     bw.flush();
177                 }
178                 else if (nativeProps.containsKey(key)) {
179                     bw.write(key + "=");
180 
181                     bw.newLine();
182                     bw.flush();
183                 }
184             }
185             else {
186                 bw.write(line);
187 
188                 bw.newLine();
189                 bw.flush();
190             }
191         }
192 
193         br.close();
194         bw.close();
195     }
196 
197     private String _orderProps(File propsFile) throws IOException {
198         String content = FileUtil.read(propsFile);
199 
200         BufferedReader br = new BufferedReader(new StringReader(content));
201         BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
202 
203         Set messages = new TreeSet();
204 
205         boolean begin = false;
206 
207         String line = null;
208 
209         while ((line = br.readLine()) != null) {
210             int pos = line.indexOf("=");
211 
212             if (pos != -1) {
213                 String key = line.substring(0, pos);
214                 String value = line.substring(pos + 1, line.length());
215 
216                 messages.add(key + "=" + value);
217             }
218             else {
219                 if (begin == true && line.equals("")) {
220                     _sortAndWrite(bw, messages);
221                 }
222 
223                 if (line.equals("")) {
224                     begin = !begin;
225                 }
226 
227                 bw.write(line);
228                 bw.newLine();
229             }
230 
231             bw.flush();
232         }
233 
234         if (messages.size() > 0) {
235             _sortAndWrite(bw, messages);
236         }
237 
238         br.close();
239         bw.close();
240 
241         return FileUtil.read(propsFile);
242     }
243 
244     private void _sortAndWrite(BufferedWriter bw, Set messages)
245         throws IOException {
246 
247         String[] messagesArray = (String[])messages.toArray(new String[0]);
248 
249         for (int i = 0; i < messagesArray.length; i++) {
250             bw.write(messagesArray[i]);
251             bw.newLine();
252         }
253 
254         messages.clear();
255     }
256 
257     private String _translate(
258         String translationId, String fromText, int limit) {
259 
260         if (translationId.equals("en_ar") ||
261             translationId.equals("en_ca") ||
262             translationId.equals("en_cs") ||
263             translationId.equals("en_fi") ||
264             translationId.equals("en_hu") ||
265             translationId.equals("en_fa") ||
266             translationId.equals("en_ru") ||
267             translationId.equals("en_sv") ||
268             translationId.equals("en_tr") ||
269             translationId.equals("en_vi")) {
270 
271             // Automatic translator does not support Arabic, Catalan, Czech,
272             // Finnish, Hungarian, Persian, Russian, Swedish, Turkish,
273             // or Vietnamese
274 
275             return null;
276         }
277 
278         // Limit the number of retries to 3
279 
280         if (limit == 3) {
281             return null;
282         }
283 
284         String toText = null;
285 
286         try {
287             System.out.println("Translating " + translationId + " " + fromText);
288 
289             WebCacheable wc =
290                 new TranslationConverter(translationId, fromText);
291 
292             Translation translation = (Translation)wc.convert("");
293 
294             toText = translation.getToText();
295 
296             if ((toText != null) &&
297                 (toText.indexOf("Babel Fish") != -1)) {
298 
299                 toText = null;
300             }
301         }
302         catch (Exception e) {
303             e.printStackTrace();
304         }
305 
306         // Keep trying
307 
308         if (toText == null) {
309             return _translate(translationId, fromText, ++limit);
310         }
311 
312         return toText;
313     }
314 
315     private String _langDir;
316     private String _langFile;
317 
318 }