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, "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"); }
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("'") != -1) {
192 translatedText = StringUtil.replace(
193 translatedText, "'", "\'");
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
302 return null;
303 }
304
305
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
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 }