001
014
015 package com.liferay.portal.tools;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedWriter;
019 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020 import com.liferay.portal.kernel.util.FileUtil;
021 import com.liferay.portal.kernel.util.PropertiesUtil;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.kernel.webcache.WebCacheItem;
026 import com.liferay.portal.util.InitUtil;
027 import com.liferay.portlet.translator.model.Translation;
028 import com.liferay.portlet.translator.util.TranslationWebCacheItem;
029
030 import java.io.File;
031 import java.io.FileInputStream;
032 import java.io.FileWriter;
033 import java.io.IOException;
034
035 import java.util.Properties;
036 import java.util.Set;
037 import java.util.TreeSet;
038
039
042 public class LangBuilder {
043
044 public static final String AUTOMATIC_COPY = " (Automatic Copy)";
045
046 public static final String AUTOMATIC_TRANSLATION =
047 " (Automatic Translation)";
048
049 public static void main(String[] args) {
050 InitUtil.initWithSpring();
051
052 if (args.length == 2) {
053 new LangBuilder(args[0], args[1], null);
054 }
055 else if (args.length == 3) {
056 new LangBuilder(args[0], args[1], args[2]);
057 }
058 else {
059 throw new IllegalArgumentException();
060 }
061 }
062
063 public LangBuilder(String langDir, String langFile, String langCode) {
064 try {
065 _langDir = langDir;
066 _langFile = langFile;
067
068 File renameKeysFile = new File(_langDir + "/rename.properties");
069
070 if (renameKeysFile.exists()) {
071 _renameKeys = PropertiesUtil.load(
072 FileUtil.read(renameKeysFile));
073 }
074
075 String content = _orderProperties(
076 new File(_langDir + "/" + _langFile + ".properties"));
077
078 if (Validator.isNotNull(langCode) && !langCode.startsWith("$")) {
079 _createProperties(content, langCode);
080 }
081 else {
082 _createProperties(content, "ar");
083 _createProperties(content, "eu");
084 _createProperties(content, "bg");
085 _createProperties(content, "ca");
086 _createProperties(content, "zh_CN");
087 _createProperties(content, "zh_TW");
088 _createProperties(content, "cs");
089 _createProperties(content, "nl");
090 _createProperties(content, "et");
091 _createProperties(content, "fi");
092 _createProperties(content, "fr");
093 _createProperties(content, "gl");
094 _createProperties(content, "de");
095 _createProperties(content, "el");
096 _createProperties(content, "iw");
097 _createProperties(content, "hi_IN");
098 _createProperties(content, "hu");
099 _createProperties(content, "in");
100 _createProperties(content, "it");
101 _createProperties(content, "ja");
102 _createProperties(content, "ko");
103 _createProperties(content, "nb");
104 _createProperties(content, "fa");
105 _createProperties(content, "pl");
106 _createProperties(content, "pt_BR");
107 _createProperties(content, "pt_PT");
108 _createProperties(content, "ru");
109 _createProperties(content, "sk");
110 _createProperties(content, "es");
111 _createProperties(content, "sv");
112 _createProperties(content, "tr");
113 _createProperties(content, "uk");
114 _createProperties(content, "vi");
115 }
116 }
117 catch (Exception e) {
118 e.printStackTrace();
119 }
120 }
121
122 private void _createProperties(String content, String languageId)
123 throws IOException {
124
125 File propertiesFile = new File(
126 _langDir + "/" + _langFile + "_" + languageId + ".properties");
127
128 Properties properties = new Properties();
129
130 if (propertiesFile.exists()) {
131 properties = PropertiesUtil.load(
132 new FileInputStream(propertiesFile), StringPool.UTF8);
133 }
134
135 String translationId = "en_" + languageId;
136
137 if (translationId.equals("en_pt_BR")) {
138 translationId = "en_pt";
139 }
140 else if (translationId.equals("en_pt_PT")) {
141 translationId = "en_pt";
142 }
143 else if (translationId.equals("en_zh_CN")) {
144 translationId = "en_zh";
145 }
146 else if (translationId.equals("en_zh_TW")) {
147 translationId = "en_zt";
148 }
149 else if (translationId.equals("en_hi_IN")) {
150 translationId = "en_hi";
151 }
152
153 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
154 new UnsyncStringReader(content));
155 UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter(
156 new FileWriter(propertiesFile));
157
158 String line = null;
159
160 while ((line = unsyncBufferedReader.readLine()) != null) {
161 line = line.trim();
162
163 int pos = line.indexOf("=");
164
165 if (pos != -1) {
166 String key = line.substring(0, pos);
167 String value = line.substring(pos + 1, line.length());
168
169 String translatedText = properties.getProperty(key);
170
171 if ((translatedText == null) && (_renameKeys != null)) {
172 String renameKey = _renameKeys.getProperty(key);
173
174 if (renameKey != null) {
175 translatedText = properties.getProperty(key);
176 }
177 }
178
179 if ((translatedText != null) &&
180 ((translatedText.indexOf("Babel Fish") != -1) ||
181 (translatedText.indexOf("Yahoo! - 999") != -1))) {
182
183 translatedText = "";
184 }
185
186 if ((translatedText == null) || translatedText.equals("")) {
187 if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
188 translatedText = value + AUTOMATIC_COPY;
189 }
190 else if (line.indexOf("[") != -1) {
191 pos = line.indexOf("[");
192
193 String baseKey = line.substring(0, pos);
194
195 translatedText =
196 properties.getProperty(baseKey) + AUTOMATIC_COPY;
197 }
198 else if (key.equals("lang.dir")) {
199 translatedText = "ltr";
200 }
201 else if (key.equals("lang.line.begin")) {
202 translatedText = "left";
203 }
204 else if (key.equals("lang.line.end")) {
205 translatedText = "right";
206 }
207 else if (translationId.equals("en_el") &&
208 (key.equals("enabled") || key.equals("on") ||
209 key.equals("on-date"))) {
210
211 translatedText = "";
212 }
213 else if (translationId.equals("en_es") &&
214 key.equals("am")) {
215
216 translatedText = "";
217 }
218 else if (translationId.equals("en_it") &&
219 key.equals("am")) {
220
221 translatedText = "";
222 }
223 else if (translationId.equals("en_ja") &&
224 (key.equals("any") || key.equals("anytime") ||
225 key.equals("down") || key.equals("on") ||
226 key.equals("on-date") || key.equals("the"))) {
227
228 translatedText = "";
229 }
230 else if (translationId.equals("en_ko") &&
231 key.equals("the")) {
232
233 translatedText = "";
234 }
235 else {
236 translatedText = _translate(
237 translationId, key, value, 0);
238
239 if (Validator.isNull(translatedText)) {
240 translatedText = value + AUTOMATIC_COPY;
241 }
242 else {
243 translatedText =
244 translatedText + AUTOMATIC_TRANSLATION;
245 }
246 }
247 }
248
249 if (Validator.isNotNull(translatedText)) {
250 if ((translatedText.indexOf("Babel Fish") != -1) ||
251 (translatedText.indexOf("Yahoo! - 999") != -1)) {
252
253 throw new IOException(
254 "IP was blocked because of over usage. Please " +
255 "use another IP.");
256 }
257
258 if (translatedText.indexOf("'") != -1) {
259 translatedText = StringUtil.replace(
260 translatedText, "'", "\'");
261 }
262
263 translatedText = StringUtil.replace(
264 translatedText.trim(), " ", " ");
265
266 unsyncBufferedWriter.write(key + "=" + translatedText);
267
268 unsyncBufferedWriter.newLine();
269 unsyncBufferedWriter.flush();
270 }
271 }
272 else {
273 unsyncBufferedWriter.write(line);
274
275 unsyncBufferedWriter.newLine();
276 unsyncBufferedWriter.flush();
277 }
278 }
279
280 unsyncBufferedReader.close();
281 unsyncBufferedWriter.close();
282 }
283
284 private String _orderProperties(File propertiesFile) throws IOException {
285 String content = FileUtil.read(propertiesFile);
286
287 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
288 new UnsyncStringReader(content));
289 UnsyncBufferedWriter unsyncBufferedWriter = new UnsyncBufferedWriter(
290 new FileWriter(propertiesFile));
291
292 Set<String> messages = new TreeSet<String>();
293
294 boolean begin = false;
295
296 String line = null;
297
298 while ((line = unsyncBufferedReader.readLine()) != null) {
299 int pos = line.indexOf("=");
300
301 if (pos != -1) {
302 String key = line.substring(0, pos);
303 String value = line.substring(pos + 1, line.length());
304
305 messages.add(key + "=" + value);
306 }
307 else {
308 if (begin == true && line.equals("")) {
309 _sortAndWrite(unsyncBufferedWriter, messages);
310 }
311
312 if (line.equals("")) {
313 begin = !begin;
314 }
315
316 unsyncBufferedWriter.write(line);
317 unsyncBufferedWriter.newLine();
318 }
319
320 unsyncBufferedWriter.flush();
321 }
322
323 if (messages.size() > 0) {
324 _sortAndWrite(unsyncBufferedWriter, messages);
325 }
326
327 unsyncBufferedReader.close();
328 unsyncBufferedWriter.close();
329
330 return FileUtil.read(propertiesFile);
331 }
332
333 private void _sortAndWrite(
334 UnsyncBufferedWriter unsyncBufferedWriter, Set<String> messages)
335 throws IOException {
336
337 String[] messagesArray = messages.toArray(new String[messages.size()]);
338
339 for (int i = 0; i < messagesArray.length; i++) {
340 unsyncBufferedWriter.write(messagesArray[i]);
341 unsyncBufferedWriter.newLine();
342 }
343
344 messages.clear();
345 }
346
347 private String _translate(
348 String translationId, String key, String fromText, int limit) {
349
350 if (translationId.equals("en_ar") ||
351 translationId.equals("en_eu") ||
352 translationId.equals("en_bg") ||
353 translationId.equals("en_ca") ||
354 translationId.equals("en_cs") ||
355 translationId.equals("en_fi") ||
356 translationId.equals("en_gl") ||
357 translationId.equals("en_iw") ||
358 translationId.equals("en_hi") ||
359 translationId.equals("en_hu") ||
360 translationId.equals("en_in") ||
361 translationId.equals("en_nb") ||
362 translationId.equals("en_fa") ||
363 translationId.equals("en_pl") ||
364 translationId.equals("en_ru") ||
365 translationId.equals("en_sk") ||
366 translationId.equals("en_sv") ||
367 translationId.equals("en_tr") ||
368 translationId.equals("en_uk") ||
369 translationId.equals("en_vi") ||
370 translationId.equals("en_et")) {
371
372
373
374
375
376
377 return null;
378 }
379
380
381
382 if (limit == 3) {
383 return null;
384 }
385
386 String toText = null;
387
388 try {
389 System.out.println(
390 "Translating " + translationId + " " + key + " " + fromText);
391
392 WebCacheItem wci = new TranslationWebCacheItem(
393 translationId, fromText);
394
395 Translation translation = (Translation)wci.convert("");
396
397 toText = translation.getToText();
398
399 if ((toText != null) &&
400 (toText.indexOf("Babel Fish") != -1)) {
401
402 toText = null;
403 }
404 }
405 catch (Exception e) {
406 e.printStackTrace();
407 }
408
409
410
411 if (toText == null) {
412 return _translate(translationId, key, fromText, ++limit);
413 }
414
415 return toText;
416 }
417
418 private String _langDir;
419 private String _langFile;
420 private Properties _renameKeys;
421
422 }