1
14
15 package com.liferay.portal.tools;
16
17 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
18 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedWriter;
19 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
20 import com.liferay.portal.kernel.util.FileUtil;
21 import com.liferay.portal.kernel.util.PropertiesUtil;
22 import com.liferay.portal.kernel.util.StringUtil;
23 import com.liferay.portal.kernel.util.Validator;
24 import com.liferay.portal.kernel.webcache.WebCacheItem;
25 import com.liferay.portal.util.InitUtil;
26 import com.liferay.portlet.translator.model.Translation;
27 import com.liferay.portlet.translator.util.TranslationWebCacheItem;
28
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileWriter;
32 import java.io.IOException;
33
34 import java.util.Properties;
35 import java.util.Set;
36 import java.util.TreeSet;
37
38
43 public class LangBuilder {
44
45 public static void main(String[] args) {
46 InitUtil.initWithSpring();
47
48 if (args.length == 2) {
49 new LangBuilder(args[0], args[1], null);
50 }
51 else if (args.length == 3) {
52 new LangBuilder(args[0], args[1], args[2]);
53 }
54 else {
55 throw new IllegalArgumentException();
56 }
57 }
58
59 public LangBuilder(String langDir, String langFile, String langCode) {
60 try {
61 _langDir = langDir;
62 _langFile = langFile;
63
64 File renameKeysFile = new File(_langDir + "/rename.properties");
65
66 if (renameKeysFile.exists()) {
67 _renameKeys = PropertiesUtil.load(
68 FileUtil.read(renameKeysFile));
69 }
70
71 String content = _orderProps(
72 new File(_langDir + "/" + _langFile + ".properties"));
73
74 if (Validator.isNotNull(langCode) && !langCode.startsWith("$")) {
75 _createProps(content, langCode);
76 }
77 else {
78 _createProps(content, "ar"); _createProps(content, "eu"); _createProps(content, "ca"); _createProps(content, "zh_CN"); _createProps(content, "zh_TW"); _createProps(content, "cs"); _createProps(content, "nl"); _createProps(content, "fi"); _createProps(content, "fr"); _createProps(content, "de"); _createProps(content, "el"); _createProps(content, "hu"); _createProps(content, "it"); _createProps(content, "ja"); _createProps(content, "ko"); _createProps(content, "nb"); _createProps(content, "fa"); _createProps(content, "pl"); _createProps(content, "pt_BR"); _createProps(content, "pt_PT"); _createProps(content, "ru"); _createProps(content, "sk"); _createProps(content, "es"); _createProps(content, "sv"); _createProps(content, "tr"); _createProps(content, "vi"); }
105 }
106 catch (Exception e) {
107 e.printStackTrace();
108 }
109 }
110
111 private void _createProps(String content, String languageId)
112 throws IOException {
113
114 File propsFile = new File(
115 _langDir + "/" + _langFile + "_" + languageId + ".properties");
116
117 Properties props = new Properties();
118
119 if (propsFile.exists()) {
120 props.load(new FileInputStream(propsFile));
121 }
122
123 File nativePropsFile = new File(
124 _langDir + "/" + _langFile + "_" + languageId +
125 ".properties.native");
126
127 Properties nativeProps = new Properties();
128
129 if (nativePropsFile.exists()) {
130 nativeProps.load(new FileInputStream(nativePropsFile));
131 }
132
133 String translationId = "en_" + languageId;
134
135 if (translationId.equals("en_pt_BR")) {
136 translationId = "en_pt";
137 }
138 else if (translationId.equals("en_pt_PT")) {
139 translationId = "en_pt";
140 }
141 else if (translationId.equals("en_zh_CN")) {
142 translationId = "en_zh";
143 }
144 else if (translationId.equals("en_zh_TW")) {
145 translationId = "en_zt";
146 }
147
148 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
149 new UnsyncStringReader(content));
150 UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter(
151 new FileWriter(nativePropsFile));
152
153 String line = null;
154
155 while ((line = unsyncBufferedReader.readLine()) != null) {
156 line = line.trim();
157
158 int pos = line.indexOf("=");
159
160 if (pos != -1) {
161 String key = line.substring(0, pos);
162 String value = line.substring(pos + 1, line.length());
163
164 String translatedText = props.getProperty(key);
165
166 if ((translatedText == null) && (_renameKeys != null)) {
167 String renameKey = _renameKeys.getProperty(key);
168
169 if (renameKey != null) {
170 translatedText = props.getProperty(renameKey);
171 }
172 }
173
174 if ((translatedText != null) &&
175 ((translatedText.indexOf("Babel Fish") != -1) ||
176 (translatedText.indexOf("Yahoo! - 999") != -1))) {
177
178 translatedText = "";
179 }
180
181 if ((translatedText == null) || translatedText.equals("")) {
182 if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
183 translatedText = value;
184 }
185 else if (key.equals("lang.dir")) {
186 translatedText = "ltr";
187 }
188 else if (key.equals("lang.line.begin")) {
189 translatedText = "left";
190 }
191 else if (key.equals("lang.line.end")) {
192 translatedText = "right";
193 }
194 else if (translationId.equals("en_el") &&
195 (key.equals("enabled") || key.equals("on") ||
196 key.equals("on-date"))) {
197
198 translatedText = "";
199 }
200 else if (translationId.equals("en_es") &&
201 key.equals("am")) {
202
203 translatedText = "";
204 }
205 else if (translationId.equals("en_it") &&
206 key.equals("am")) {
207
208 translatedText = "";
209 }
210 else if (translationId.equals("en_ja") &&
211 (key.equals("any") || key.equals("anytime") ||
212 key.equals("down") || key.equals("on") ||
213 key.equals("on-date") || key.equals("the"))) {
214
215 translatedText = "";
216 }
217 else if (translationId.equals("en_ko") &&
218 key.equals("the")) {
219
220 translatedText = "";
221 }
222 else {
223 translatedText = _translate(
224 translationId, key, value, 0);
225 }
226 }
227
228 if (Validator.isNotNull(translatedText)) {
229 if ((translatedText.indexOf("Babel Fish") != -1) ||
230 (translatedText.indexOf("Yahoo! - 999") != -1)) {
231
232 throw new IOException(
233 "IP was blocked because of over usage. Please " +
234 "use another IP.");
235 }
236
237 if (translatedText.indexOf("'") != -1) {
238 translatedText = StringUtil.replace(
239 translatedText, "'", "\'");
240 }
241
242 unsyncBufferedWriter.write(key + "=" + translatedText);
243
244 unsyncBufferedWriter.newLine();
245 unsyncBufferedWriter.flush();
246 }
247 else if (nativeProps.containsKey(key)) {
248 unsyncBufferedWriter.write(key + "=");
249
250 unsyncBufferedWriter.newLine();
251 unsyncBufferedWriter.flush();
252 }
253 }
254 else {
255 unsyncBufferedWriter.write(line);
256
257 unsyncBufferedWriter.newLine();
258 unsyncBufferedWriter.flush();
259 }
260 }
261
262 unsyncBufferedReader.close();
263 unsyncBufferedWriter.close();
264 }
265
266 private String _orderProps(File propsFile) throws IOException {
267 String content = FileUtil.read(propsFile);
268
269 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
270 new UnsyncStringReader(content));
271 UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter(
272 new FileWriter(propsFile));
273
274 Set<String> messages = new TreeSet<String>();
275
276 boolean begin = false;
277
278 String line = null;
279
280 while ((line = unsyncBufferedReader.readLine()) != null) {
281 int pos = line.indexOf("=");
282
283 if (pos != -1) {
284 String key = line.substring(0, pos);
285 String value = line.substring(pos + 1, line.length());
286
287 messages.add(key + "=" + value);
288 }
289 else {
290 if (begin == true && line.equals("")) {
291 _sortAndWrite(unsyncBufferedWriter, messages);
292 }
293
294 if (line.equals("")) {
295 begin = !begin;
296 }
297
298 unsyncBufferedWriter.write(line);
299 unsyncBufferedWriter.newLine();
300 }
301
302 unsyncBufferedWriter.flush();
303 }
304
305 if (messages.size() > 0) {
306 _sortAndWrite(unsyncBufferedWriter, messages);
307 }
308
309 unsyncBufferedReader.close();
310 unsyncBufferedWriter.close();
311
312 return FileUtil.read(propsFile);
313 }
314
315 private void _sortAndWrite(
316 UnsyncBufferedWriter unsyncBufferedWriter, Set<String> messages)
317 throws IOException {
318
319 String[] messagesArray = messages.toArray(new String[messages.size()]);
320
321 for (int i = 0; i < messagesArray.length; i++) {
322 unsyncBufferedWriter.write(messagesArray[i]);
323 unsyncBufferedWriter.newLine();
324 }
325
326 messages.clear();
327 }
328
329 private String _translate(
330 String translationId, String key, String fromText, int limit) {
331
332 if (translationId.equals("en_ar") ||
333 translationId.equals("en_eu") ||
334 translationId.equals("en_ca") ||
335 translationId.equals("en_cs") ||
336 translationId.equals("en_fi") ||
337 translationId.equals("en_hu") ||
338 translationId.equals("en_nb") ||
339 translationId.equals("en_fa") ||
340 translationId.equals("en_pl") ||
341 translationId.equals("en_ru") ||
342 translationId.equals("en_sk") ||
343 translationId.equals("en_sv") ||
344 translationId.equals("en_tr") ||
345 translationId.equals("en_vi")) {
346
347
351 return null;
352 }
353
354
356 if (limit == 3) {
357 return null;
358 }
359
360 String toText = null;
361
362 try {
363 System.out.println(
364 "Translating " + translationId + " " + key + " " + fromText);
365
366 WebCacheItem wci = new TranslationWebCacheItem(
367 translationId, fromText);
368
369 Translation translation = (Translation)wci.convert("");
370
371 toText = translation.getToText();
372
373 if ((toText != null) &&
374 (toText.indexOf("Babel Fish") != -1)) {
375
376 toText = null;
377 }
378 }
379 catch (Exception e) {
380 e.printStackTrace();
381 }
382
383
385 if (toText == null) {
386 return _translate(translationId, key, fromText, ++limit);
387 }
388
389 return toText;
390 }
391
392 private String _langDir;
393 private String _langFile;
394 private Properties _renameKeys;
395
396 }