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