1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.util.FileUtil;
26 import com.liferay.portal.kernel.util.PropertiesUtil;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.kernel.webcache.WebCacheItem;
30 import com.liferay.portal.util.InitUtil;
31 import com.liferay.portlet.translator.model.Translation;
32 import com.liferay.portlet.translator.util.TranslationWebCacheItem;
33
34 import java.io.BufferedReader;
35 import java.io.BufferedWriter;
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.FileWriter;
39 import java.io.IOException;
40 import java.io.StringReader;
41
42 import java.util.Properties;
43 import java.util.Set;
44 import java.util.TreeSet;
45
46
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], null);
58 }
59 else if (args.length == 3) {
60 new LangBuilder(args[0], args[1], args[2]);
61 }
62 else {
63 throw new IllegalArgumentException();
64 }
65 }
66
67 public LangBuilder(String langDir, String langFile, String langCode) {
68 try {
69 _langDir = langDir;
70 _langFile = langFile;
71
72 File renameKeysFile = new File(_langDir + "/rename.properties");
73
74 if (renameKeysFile.exists()) {
75 _renameKeys = PropertiesUtil.load(
76 FileUtil.read(renameKeysFile));
77 }
78
79 String content = _orderProps(
80 new File(_langDir + "/" + _langFile + ".properties"));
81
82 if (Validator.isNotNull(langCode) && !langCode.startsWith("$")) {
83 _createProps(content, langCode);
84 }
85 else {
86 _createProps(content, "ar"); _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, "pt_BR"); _createProps(content, "pt_PT"); _createProps(content, "ru"); _createProps(content, "es"); _createProps(content, "sv"); _createProps(content, "tr"); _createProps(content, "vi"); }
110 }
111 catch (Exception e) {
112 e.printStackTrace();
113 }
114 }
115
116 private void _createProps(String content, String languageId)
117 throws IOException {
118
119 File propsFile = new File(
120 _langDir + "/" + _langFile + "_" + languageId + ".properties");
121
122 Properties props = new Properties();
123
124 if (propsFile.exists()) {
125 props.load(new FileInputStream(propsFile));
126 }
127
128 File nativePropsFile = new File(
129 _langDir + "/" + _langFile + "_" + languageId +
130 ".properties.native");
131
132 Properties nativeProps = new Properties();
133
134 if (nativePropsFile.exists()) {
135 nativeProps.load(new FileInputStream(nativePropsFile));
136 }
137
138 String translationId = "en_" + languageId;
139
140 if (translationId.equals("en_pt_BR")) {
141 translationId = "en_pt";
142 }
143 else if (translationId.equals("en_pt_PT")) {
144 translationId = "en_pt";
145 }
146 else if (translationId.equals("en_zh_CN")) {
147 translationId = "en_zh";
148 }
149 else if (translationId.equals("en_zh_TW")) {
150 translationId = "en_zt";
151 }
152
153 BufferedReader br = new BufferedReader(new StringReader(content));
154 BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
155
156 String line = null;
157
158 while ((line = br.readLine()) != null) {
159 line = line.trim();
160
161 int pos = line.indexOf("=");
162
163 if (pos != -1) {
164 String key = line.substring(0, pos);
165 String value = line.substring(pos + 1, line.length());
166
167 String translatedText = props.getProperty(key);
168
169 if ((translatedText == null) && (_renameKeys != null)) {
170 String renameKey = _renameKeys.getProperty(key);
171
172 if (renameKey != null) {
173 translatedText = props.getProperty(renameKey);
174 }
175 }
176
177 if ((translatedText != null) &&
178 ((translatedText.indexOf("Babel Fish") != -1) ||
179 (translatedText.indexOf("Yahoo! - 999") != -1))) {
180
181 translatedText = "";
182 }
183
184 if ((translatedText == null) || translatedText.equals("")) {
185 if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
186 translatedText = value;
187 }
188 else if (key.equals("lang.dir")) {
189 translatedText = "ltr";
190 }
191 else if (key.equals("lang.line.begin")) {
192 translatedText = "left";
193 }
194 else if (key.equals("lang.line.end")) {
195 translatedText = "right";
196 }
197 else {
198 translatedText = _translate(translationId, value, 0);
199 }
200 }
201
202 if (Validator.isNotNull(translatedText)) {
203 if ((translatedText.indexOf("Babel Fish") != -1) ||
204 (translatedText.indexOf("Yahoo! - 999") != -1)) {
205
206 throw new IOException(
207 "IP was blocked because of over usage. Please " +
208 "use another IP.");
209 }
210
211 if (translatedText.indexOf("'") != -1) {
212 translatedText = StringUtil.replace(
213 translatedText, "'", "\'");
214 }
215
216 bw.write(key + "=" + translatedText);
217
218 bw.newLine();
219 bw.flush();
220 }
221 else if (nativeProps.containsKey(key)) {
222 bw.write(key + "=");
223
224 bw.newLine();
225 bw.flush();
226 }
227 }
228 else {
229 bw.write(line);
230
231 bw.newLine();
232 bw.flush();
233 }
234 }
235
236 br.close();
237 bw.close();
238 }
239
240 private String _orderProps(File propsFile) throws IOException {
241 String content = FileUtil.read(propsFile);
242
243 BufferedReader br = new BufferedReader(new StringReader(content));
244 BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
245
246 Set<String> messages = new TreeSet<String>();
247
248 boolean begin = false;
249
250 String line = null;
251
252 while ((line = br.readLine()) != null) {
253 int pos = line.indexOf("=");
254
255 if (pos != -1) {
256 String key = line.substring(0, pos);
257 String value = line.substring(pos + 1, line.length());
258
259 messages.add(key + "=" + value);
260 }
261 else {
262 if (begin == true && line.equals("")) {
263 _sortAndWrite(bw, messages);
264 }
265
266 if (line.equals("")) {
267 begin = !begin;
268 }
269
270 bw.write(line);
271 bw.newLine();
272 }
273
274 bw.flush();
275 }
276
277 if (messages.size() > 0) {
278 _sortAndWrite(bw, messages);
279 }
280
281 br.close();
282 bw.close();
283
284 return FileUtil.read(propsFile);
285 }
286
287 private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
288 throws IOException {
289
290 String[] messagesArray = messages.toArray(new String[messages.size()]);
291
292 for (int i = 0; i < messagesArray.length; i++) {
293 bw.write(messagesArray[i]);
294 bw.newLine();
295 }
296
297 messages.clear();
298 }
299
300 private String _translate(
301 String translationId, String fromText, int limit) {
302
303 if (translationId.equals("en_ar") ||
304 translationId.equals("en_ca") ||
305 translationId.equals("en_cs") ||
306 translationId.equals("en_fi") ||
307 translationId.equals("en_hu") ||
308 translationId.equals("en_nb") ||
309 translationId.equals("en_fa") ||
310 translationId.equals("en_ru") ||
311 translationId.equals("en_sv") ||
312 translationId.equals("en_tr") ||
313 translationId.equals("en_vi")) {
314
315
319 return null;
320 }
321
322
324 if (limit == 3) {
325 return null;
326 }
327
328 String toText = null;
329
330 try {
331 System.out.println("Translating " + translationId + " " + fromText);
332
333 WebCacheItem wci = new TranslationWebCacheItem(
334 translationId, fromText);
335
336 Translation translation = (Translation)wci.convert("");
337
338 toText = translation.getToText();
339
340 if ((toText != null) &&
341 (toText.indexOf("Babel Fish") != -1)) {
342
343 toText = null;
344 }
345 }
346 catch (Exception e) {
347 e.printStackTrace();
348 }
349
350
352 if (toText == null) {
353 return _translate(translationId, fromText, ++limit);
354 }
355
356 return toText;
357 }
358
359 private String _langDir;
360 private String _langFile;
361 private Properties _renameKeys;
362
363 }