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