1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.PropertiesUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.kernel.webcache.WebCacheItem;
30  import com.liferay.portal.util.InitUtil;
31  import com.liferay.portlet.translator.model.Translation;
32  import com.liferay.portlet.translator.util.TranslationWebCacheItem;
33  
34  import java.io.BufferedReader;
35  import java.io.BufferedWriter;
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.FileWriter;
39  import java.io.IOException;
40  import java.io.StringReader;
41  
42  import java.util.Properties;
43  import java.util.Set;
44  import java.util.TreeSet;
45  
46  /**
47   * <a href="LangBuilder.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
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], null);
58          }
59          else if (args.length == 3) {
60              new LangBuilder(args[0], args[1], args[2]);
61          }
62          else {
63              throw new IllegalArgumentException();
64          }
65      }
66  
67      public LangBuilder(String langDir, String langFile, String langCode) {
68          try {
69              _langDir = langDir;
70              _langFile = langFile;
71  
72              File renameKeysFile = new File(_langDir + "/rename.properties");
73  
74              if (renameKeysFile.exists()) {
75                  _renameKeys = PropertiesUtil.load(
76                      FileUtil.read(renameKeysFile));
77              }
78  
79              String content = _orderProps(
80                  new File(_langDir + "/" + _langFile + ".properties"));
81  
82              if (Validator.isNotNull(langCode) && !langCode.startsWith("$")) {
83                  _createProps(content, langCode);
84              }
85              else {
86                  _createProps(content, "ar"); // Arabic
87                  _createProps(content, "ca"); // Catalan
88                  _createProps(content, "zh_CN"); // Chinese (China)
89                  _createProps(content, "zh_TW"); // Chinese (Taiwan)
90                  _createProps(content, "cs"); // Czech
91                  _createProps(content, "nl"); // Dutch
92                  _createProps(content, "fi"); // Finnish
93                  _createProps(content, "fr"); // French
94                  _createProps(content, "de"); // German
95                  _createProps(content, "el"); // Greek
96                  _createProps(content, "hu"); // Hungarian
97                  _createProps(content, "it"); // Italian
98                  _createProps(content, "ja"); // Japanese
99                  _createProps(content, "ko"); // Korean
100                 _createProps(content, "nb"); // Norwegian Bokmål
101                 _createProps(content, "fa"); // Persian
102                 _createProps(content, "pt_BR"); // Brazilian Portuguese
103                 _createProps(content, "pt_PT"); // Portuguese
104                 _createProps(content, "ru"); // Russian
105                 _createProps(content, "es"); // Spanish
106                 _createProps(content, "sv"); // Swedish
107                 _createProps(content, "tr"); // Turkish
108                 _createProps(content, "vi"); // Vietnamese
109             }
110         }
111         catch (Exception e) {
112             e.printStackTrace();
113         }
114     }
115 
116     private void _createProps(String content, String languageId)
117         throws IOException {
118 
119         File propsFile = new File(
120             _langDir + "/" + _langFile + "_" + languageId + ".properties");
121 
122         Properties props = new Properties();
123 
124         if (propsFile.exists()) {
125             props.load(new FileInputStream(propsFile));
126         }
127 
128         File nativePropsFile = new File(
129             _langDir + "/" + _langFile + "_" + languageId +
130                 ".properties.native");
131 
132         Properties nativeProps = new Properties();
133 
134         if (nativePropsFile.exists()) {
135             nativeProps.load(new FileInputStream(nativePropsFile));
136         }
137 
138         String translationId = "en_" + languageId;
139 
140         if (translationId.equals("en_pt_BR")) {
141             translationId = "en_pt";
142         }
143         else if (translationId.equals("en_pt_PT")) {
144             translationId = "en_pt";
145         }
146         else if (translationId.equals("en_zh_CN")) {
147             translationId = "en_zh";
148         }
149         else if (translationId.equals("en_zh_TW")) {
150             translationId = "en_zt";
151         }
152 
153         BufferedReader br = new BufferedReader(new StringReader(content));
154         BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
155 
156         String line = null;
157 
158         while ((line = br.readLine()) != null) {
159             line = line.trim();
160 
161             int pos = line.indexOf("=");
162 
163             if (pos != -1) {
164                 String key = line.substring(0, pos);
165                 String value = line.substring(pos + 1, line.length());
166 
167                 String translatedText = props.getProperty(key);
168 
169                 if ((translatedText == null) && (_renameKeys != null)) {
170                     String renameKey = _renameKeys.getProperty(key);
171 
172                     if (renameKey != null) {
173                         translatedText = props.getProperty(renameKey);
174                     }
175                 }
176 
177                 if ((translatedText != null) &&
178                     ((translatedText.indexOf("Babel Fish") != -1) ||
179                      (translatedText.indexOf("Yahoo! - 999") != -1))) {
180 
181                     translatedText = "";
182                 }
183 
184                 if ((translatedText == null) || translatedText.equals("")) {
185                     if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
186                         translatedText = value;
187                     }
188                     else if (key.equals("lang.dir")) {
189                         translatedText = "ltr";
190                     }
191                     else if (key.equals("lang.line.begin")) {
192                         translatedText = "left";
193                     }
194                     else if (key.equals("lang.line.end")) {
195                         translatedText = "right";
196                     }
197                     else {
198                         translatedText = _translate(translationId, value, 0);
199                     }
200                 }
201 
202                 if (Validator.isNotNull(translatedText)) {
203                     if ((translatedText.indexOf("Babel Fish") != -1) ||
204                         (translatedText.indexOf("Yahoo! - 999") != -1)) {
205 
206                         throw new IOException(
207                             "IP was blocked because of over usage. Please " +
208                                 "use another IP.");
209                     }
210 
211                     if (translatedText.indexOf("&#39;") != -1) {
212                         translatedText = StringUtil.replace(
213                             translatedText, "&#39;", "\'");
214                     }
215 
216                     bw.write(key + "=" + translatedText);
217 
218                     bw.newLine();
219                     bw.flush();
220                 }
221                 else if (nativeProps.containsKey(key)) {
222                     bw.write(key + "=");
223 
224                     bw.newLine();
225                     bw.flush();
226                 }
227             }
228             else {
229                 bw.write(line);
230 
231                 bw.newLine();
232                 bw.flush();
233             }
234         }
235 
236         br.close();
237         bw.close();
238     }
239 
240     private String _orderProps(File propsFile) throws IOException {
241         String content = FileUtil.read(propsFile);
242 
243         BufferedReader br = new BufferedReader(new StringReader(content));
244         BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
245 
246         Set<String> messages = new TreeSet<String>();
247 
248         boolean begin = false;
249 
250         String line = null;
251 
252         while ((line = br.readLine()) != null) {
253             int pos = line.indexOf("=");
254 
255             if (pos != -1) {
256                 String key = line.substring(0, pos);
257                 String value = line.substring(pos + 1, line.length());
258 
259                 messages.add(key + "=" + value);
260             }
261             else {
262                 if (begin == true && line.equals("")) {
263                     _sortAndWrite(bw, messages);
264                 }
265 
266                 if (line.equals("")) {
267                     begin = !begin;
268                 }
269 
270                 bw.write(line);
271                 bw.newLine();
272             }
273 
274             bw.flush();
275         }
276 
277         if (messages.size() > 0) {
278             _sortAndWrite(bw, messages);
279         }
280 
281         br.close();
282         bw.close();
283 
284         return FileUtil.read(propsFile);
285     }
286 
287     private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
288         throws IOException {
289 
290         String[] messagesArray = messages.toArray(new String[messages.size()]);
291 
292         for (int i = 0; i < messagesArray.length; i++) {
293             bw.write(messagesArray[i]);
294             bw.newLine();
295         }
296 
297         messages.clear();
298     }
299 
300     private String _translate(
301         String translationId, String fromText, int limit) {
302 
303         if (translationId.equals("en_ar") ||
304             translationId.equals("en_ca") ||
305             translationId.equals("en_cs") ||
306             translationId.equals("en_fi") ||
307             translationId.equals("en_hu") ||
308             translationId.equals("en_nb") ||
309             translationId.equals("en_fa") ||
310             translationId.equals("en_ru") ||
311             translationId.equals("en_sv") ||
312             translationId.equals("en_tr") ||
313             translationId.equals("en_vi")) {
314 
315             // Automatic translator does not support Arabic, Catalan, Czech,
316             // Finnish, Hungarian, Norwegian Bokmål, Persian, Russian, Swedish,
317             // Turkish, or Vietnamese
318 
319             return null;
320         }
321 
322         // Limit the number of retries to 3
323 
324         if (limit == 3) {
325             return null;
326         }
327 
328         String toText = null;
329 
330         try {
331             System.out.println("Translating " + translationId + " " + fromText);
332 
333             WebCacheItem wci = new TranslationWebCacheItem(
334                 translationId, fromText);
335 
336             Translation translation = (Translation)wci.convert("");
337 
338             toText = translation.getToText();
339 
340             if ((toText != null) &&
341                 (toText.indexOf("Babel Fish") != -1)) {
342 
343                 toText = null;
344             }
345         }
346         catch (Exception e) {
347             e.printStackTrace();
348         }
349 
350         // Keep trying
351 
352         if (toText == null) {
353             return _translate(translationId, fromText, ++limit);
354         }
355 
356         return toText;
357     }
358 
359     private String _langDir;
360     private String _langFile;
361     private Properties _renameKeys;
362 
363 }