1
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
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"); _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"); }
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("Babel Fish") != -1) ||
181 (translatedText.indexOf("Yahoo! - 999") != -1)) {
182
183 throw new IOException(
184 "IP was blocked because of over usage. Please " +
185 "use another IP.");
186 }
187
188 if (translatedText.indexOf("'") != -1) {
189 translatedText = StringUtil.replace(
190 translatedText, "'", "\'");
191 }
192
193 bw.write(key + "=" + translatedText);
194
195 bw.newLine();
196 bw.flush();
197 }
198 else if (nativeProps.containsKey(key)) {
199 bw.write(key + "=");
200
201 bw.newLine();
202 bw.flush();
203 }
204 }
205 else {
206 bw.write(line);
207
208 bw.newLine();
209 bw.flush();
210 }
211 }
212
213 br.close();
214 bw.close();
215 }
216
217 private String _orderProps(File propsFile) throws IOException {
218 String content = FileUtil.read(propsFile);
219
220 BufferedReader br = new BufferedReader(new StringReader(content));
221 BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
222
223 Set<String> messages = new TreeSet<String>();
224
225 boolean begin = false;
226
227 String line = null;
228
229 while ((line = br.readLine()) != null) {
230 int pos = line.indexOf("=");
231
232 if (pos != -1) {
233 String key = line.substring(0, pos);
234 String value = line.substring(pos + 1, line.length());
235
236 messages.add(key + "=" + value);
237 }
238 else {
239 if (begin == true && line.equals("")) {
240 _sortAndWrite(bw, messages);
241 }
242
243 if (line.equals("")) {
244 begin = !begin;
245 }
246
247 bw.write(line);
248 bw.newLine();
249 }
250
251 bw.flush();
252 }
253
254 if (messages.size() > 0) {
255 _sortAndWrite(bw, messages);
256 }
257
258 br.close();
259 bw.close();
260
261 return FileUtil.read(propsFile);
262 }
263
264 private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
265 throws IOException {
266
267 String[] messagesArray = messages.toArray(new String[messages.size()]);
268
269 for (int i = 0; i < messagesArray.length; i++) {
270 bw.write(messagesArray[i]);
271 bw.newLine();
272 }
273
274 messages.clear();
275 }
276
277 private String _translate(
278 String translationId, String fromText, int limit) {
279
280 if (translationId.equals("en_ar") ||
281 translationId.equals("en_ca") ||
282 translationId.equals("en_cs") ||
283 translationId.equals("en_fi") ||
284 translationId.equals("en_hu") ||
285 translationId.equals("en_nb") ||
286 translationId.equals("en_fa") ||
287 translationId.equals("en_ru") ||
288 translationId.equals("en_sv") ||
289 translationId.equals("en_tr") ||
290 translationId.equals("en_vi")) {
291
292
296 return null;
297 }
298
299
301 if (limit == 3) {
302 return null;
303 }
304
305 String toText = null;
306
307 try {
308 System.out.println("Translating " + translationId + " " + fromText);
309
310 WebCacheItem wci = new TranslationWebCacheItem(
311 translationId, fromText);
312
313 Translation translation = (Translation)wci.convert("");
314
315 toText = translation.getToText();
316
317 if ((toText != null) &&
318 (toText.indexOf("Babel Fish") != -1)) {
319
320 toText = null;
321 }
322 }
323 catch (Exception e) {
324 e.printStackTrace();
325 }
326
327
329 if (toText == null) {
330 return _translate(translationId, fromText, ++limit);
331 }
332
333 return toText;
334 }
335
336 private String _langDir;
337 private String _langFile;
338
339 }