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