1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.tools;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.kernel.webcache.WebCacheItem;
26  import com.liferay.portal.util.InitUtil;
27  import com.liferay.portlet.translator.model.Translation;
28  import com.liferay.portlet.translator.util.TranslationWebCacheItem;
29  
30  import java.io.BufferedReader;
31  import java.io.BufferedWriter;
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.io.FileWriter;
35  import java.io.IOException;
36  import java.io.StringReader;
37  
38  import java.util.Properties;
39  import java.util.Set;
40  import java.util.TreeSet;
41  
42  /**
43   * <a href="LangBuilder.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class LangBuilder {
49  
50      public static void main(String[] args) {
51          InitUtil.initWithSpring();
52  
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, "nb"); // Norwegian Bokmål
84              _createProps(content, "fa"); // Persian
85              _createProps(content, "pt_BR"); // Brazilian Portuguese
86              _createProps(content, "pt_PT"); // Portuguese
87              _createProps(content, "ru"); // Russian
88              _createProps(content, "es"); // Spanish
89              _createProps(content, "sv"); // Swedish
90              _createProps(content, "tr"); // Turkish
91              _createProps(content, "vi"); // Vietnamese
92          }
93          catch (Exception e) {
94              e.printStackTrace();
95          }
96      }
97  
98      private void _createProps(String content, String languageId)
99          throws IOException {
100 
101         File propsFile = new File(
102             _langDir + "/" + _langFile + "_" + languageId + ".properties");
103 
104         Properties props = new Properties();
105 
106         if (propsFile.exists()) {
107             props.load(new FileInputStream(propsFile));
108         }
109 
110         File nativePropsFile = new File(
111             _langDir + "/" + _langFile + "_" + languageId +
112                 ".properties.native");
113 
114         Properties nativeProps = new Properties();
115 
116         if (nativePropsFile.exists()) {
117             nativeProps.load(new FileInputStream(nativePropsFile));
118         }
119 
120         String translationId = "en_" + languageId;
121 
122         if (translationId.equals("en_pt_BR")) {
123             translationId = "en_pt";
124         }
125         else if (translationId.equals("en_pt_PT")) {
126             translationId = "en_pt";
127         }
128         else if (translationId.equals("en_zh_CN")) {
129             translationId = "en_zh";
130         }
131         else if (translationId.equals("en_zh_TW")) {
132             translationId = "en_zt";
133         }
134 
135         BufferedReader br = new BufferedReader(new StringReader(content));
136         BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
137 
138         String line = null;
139 
140         while ((line = br.readLine()) != null) {
141             line = line.trim();
142 
143             int pos = line.indexOf("=");
144 
145             if (pos != -1) {
146                 String key = line.substring(0, pos);
147                 String value = line.substring(pos + 1, line.length());
148 
149                 String translatedText = props.getProperty(key);
150 
151                 if ((translatedText != null) &&
152                     ((translatedText.indexOf("Babel Fish") != -1) ||
153                      (translatedText.indexOf("Yahoo! - 999") != -1))) {
154 
155                     translatedText = "";
156                 }
157 
158                 if ((translatedText == null) || translatedText.equals("")) {
159                     if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
160                         translatedText = value;
161                     }
162                     else if (key.equals("lang.dir")) {
163                         translatedText = "ltr";
164                     }
165                     else if (key.equals("lang.line.begin")) {
166                         translatedText = "left";
167                     }
168                     else if (key.equals("lang.line.end")) {
169                         translatedText = "right";
170                     }
171                     else {
172                         translatedText = _translate(translationId, value, 0);
173                     }
174                 }
175 
176                 if (Validator.isNotNull(translatedText)) {
177                     if (translatedText.indexOf("&#39;") != -1) {
178                         translatedText = StringUtil.replace(
179                             translatedText, "&#39;", "\'");
180                     }
181 
182                     bw.write(key + "=" + translatedText);
183 
184                     bw.newLine();
185                     bw.flush();
186                 }
187                 else if (nativeProps.containsKey(key)) {
188                     bw.write(key + "=");
189 
190                     bw.newLine();
191                     bw.flush();
192                 }
193             }
194             else {
195                 bw.write(line);
196 
197                 bw.newLine();
198                 bw.flush();
199             }
200         }
201 
202         br.close();
203         bw.close();
204     }
205 
206     private String _orderProps(File propsFile) throws IOException {
207         String content = FileUtil.read(propsFile);
208 
209         BufferedReader br = new BufferedReader(new StringReader(content));
210         BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
211 
212         Set<String> messages = new TreeSet<String>();
213 
214         boolean begin = false;
215 
216         String line = null;
217 
218         while ((line = br.readLine()) != null) {
219             int pos = line.indexOf("=");
220 
221             if (pos != -1) {
222                 String key = line.substring(0, pos);
223                 String value = line.substring(pos + 1, line.length());
224 
225                 messages.add(key + "=" + value);
226             }
227             else {
228                 if (begin == true && line.equals("")) {
229                     _sortAndWrite(bw, messages);
230                 }
231 
232                 if (line.equals("")) {
233                     begin = !begin;
234                 }
235 
236                 bw.write(line);
237                 bw.newLine();
238             }
239 
240             bw.flush();
241         }
242 
243         if (messages.size() > 0) {
244             _sortAndWrite(bw, messages);
245         }
246 
247         br.close();
248         bw.close();
249 
250         return FileUtil.read(propsFile);
251     }
252 
253     private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
254         throws IOException {
255 
256         String[] messagesArray = messages.toArray(new String[messages.size()]);
257 
258         for (int i = 0; i < messagesArray.length; i++) {
259             bw.write(messagesArray[i]);
260             bw.newLine();
261         }
262 
263         messages.clear();
264     }
265 
266     private String _translate(
267         String translationId, String fromText, int limit) {
268 
269         if (translationId.equals("en_ar") ||
270             translationId.equals("en_ca") ||
271             translationId.equals("en_cs") ||
272             translationId.equals("en_fi") ||
273             translationId.equals("en_hu") ||
274             translationId.equals("en_nb") ||
275             translationId.equals("en_fa") ||
276             translationId.equals("en_ru") ||
277             translationId.equals("en_sv") ||
278             translationId.equals("en_tr") ||
279             translationId.equals("en_vi")) {
280 
281             // Automatic translator does not support Arabic, Catalan, Czech,
282             // Finnish, Hungarian, Norwegian Bokmål, Persian, Russian, Swedish,
283             // Turkish, or Vietnamese
284 
285             return null;
286         }
287 
288         // Limit the number of retries to 3
289 
290         if (limit == 3) {
291             return null;
292         }
293 
294         String toText = null;
295 
296         try {
297             System.out.println("Translating " + translationId + " " + fromText);
298 
299             WebCacheItem wci = new TranslationWebCacheItem(
300                 translationId, fromText);
301 
302             Translation translation = (Translation)wci.convert("");
303 
304             toText = translation.getToText();
305 
306             if ((toText != null) &&
307                 (toText.indexOf("Babel Fish") != -1)) {
308 
309                 toText = null;
310             }
311         }
312         catch (Exception e) {
313             e.printStackTrace();
314         }
315 
316         // Keep trying
317 
318         if (toText == null) {
319             return _translate(translationId, fromText, ++limit);
320         }
321 
322         return toText;
323     }
324 
325     private String _langDir;
326     private String _langFile;
327 
328 }