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, "pl"); _createProps(content, "pt_BR"); _createProps(content, "pt_PT"); _createProps(content, "ru"); _createProps(content, "es"); _createProps(content, "sv"); _createProps(content, "tr"); _createProps(content, "vi"); }
97 catch (Exception e) {
98 e.printStackTrace();
99 }
100 }
101
102 private void _createProps(String content, String languageId)
103 throws IOException {
104
105 File propsFile = new File(
106 _langDir + "/" + _langFile + "_" + languageId + ".properties");
107
108 Properties props = new Properties();
109
110 if (propsFile.exists()) {
111 props.load(new FileInputStream(propsFile));
112 }
113
114 File nativePropsFile = new File(
115 _langDir + "/" + _langFile + "_" + languageId +
116 ".properties.native");
117
118 Properties nativeProps = new Properties();
119
120 if (nativePropsFile.exists()) {
121 nativeProps.load(new FileInputStream(nativePropsFile));
122 }
123
124 String translationId = "en_" + languageId;
125
126 if (translationId.equals("en_pt_BR")) {
127 translationId = "en_pt";
128 }
129 else if (translationId.equals("en_pt_PT")) {
130 translationId = "en_pt";
131 }
132 else if (translationId.equals("en_zh_CN")) {
133 translationId = "en_zh";
134 }
135 else if (translationId.equals("en_zh_TW")) {
136 translationId = "en_zt";
137 }
138
139 BufferedReader br = new BufferedReader(new StringReader(content));
140 BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
141
142 String line = null;
143
144 while ((line = br.readLine()) != null) {
145 line = line.trim();
146
147 int pos = line.indexOf("=");
148
149 if (pos != -1) {
150 String key = line.substring(0, pos);
151 String value = line.substring(pos + 1, line.length());
152
153 String translatedText = props.getProperty(key);
154
155 if ((translatedText != null) &&
156 ((translatedText.indexOf("Babel Fish") != -1) ||
157 (translatedText.indexOf("Yahoo! - 999") != -1))) {
158
159 translatedText = "";
160 }
161
162 if ((translatedText == null) || translatedText.equals("")) {
163 if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
164 translatedText = value;
165 }
166 else if (key.equals("lang.dir")) {
167 translatedText = "ltr";
168 }
169 else if (key.equals("lang.line.begin")) {
170 translatedText = "left";
171 }
172 else if (key.equals("lang.line.end")) {
173 translatedText = "right";
174 }
175 else {
176 translatedText = _translate(translationId, value, 0);
177 }
178 }
179
180 if (Validator.isNotNull(translatedText)) {
181 if (translatedText.indexOf("'") != -1) {
182 translatedText = StringUtil.replace(
183 translatedText, "'", "\'");
184 }
185
186 bw.write(key + "=" + translatedText);
187
188 bw.newLine();
189 bw.flush();
190 }
191 else if (nativeProps.containsKey(key)) {
192 bw.write(key + "=");
193
194 bw.newLine();
195 bw.flush();
196 }
197 }
198 else {
199 bw.write(line);
200
201 bw.newLine();
202 bw.flush();
203 }
204 }
205
206 br.close();
207 bw.close();
208 }
209
210 private String _orderProps(File propsFile) throws IOException {
211 String content = FileUtil.read(propsFile);
212
213 BufferedReader br = new BufferedReader(new StringReader(content));
214 BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
215
216 Set<String> messages = new TreeSet<String>();
217
218 boolean begin = false;
219
220 String line = null;
221
222 while ((line = br.readLine()) != null) {
223 int pos = line.indexOf("=");
224
225 if (pos != -1) {
226 String key = line.substring(0, pos);
227 String value = line.substring(pos + 1, line.length());
228
229 messages.add(key + "=" + value);
230 }
231 else {
232 if (begin == true && line.equals("")) {
233 _sortAndWrite(bw, messages);
234 }
235
236 if (line.equals("")) {
237 begin = !begin;
238 }
239
240 bw.write(line);
241 bw.newLine();
242 }
243
244 bw.flush();
245 }
246
247 if (messages.size() > 0) {
248 _sortAndWrite(bw, messages);
249 }
250
251 br.close();
252 bw.close();
253
254 return FileUtil.read(propsFile);
255 }
256
257 private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
258 throws IOException {
259
260 String[] messagesArray = messages.toArray(new String[messages.size()]);
261
262 for (int i = 0; i < messagesArray.length; i++) {
263 bw.write(messagesArray[i]);
264 bw.newLine();
265 }
266
267 messages.clear();
268 }
269
270 private String _translate(
271 String translationId, String fromText, int limit) {
272
273 if (translationId.equals("en_ar") ||
274 translationId.equals("en_ca") ||
275 translationId.equals("en_cs") ||
276 translationId.equals("en_fi") ||
277 translationId.equals("en_hu") ||
278 translationId.equals("en_nb") ||
279 translationId.equals("en_fa") ||
280 translationId.equals("en_pl") ||
281 translationId.equals("en_ru") ||
282 translationId.equals("en_sv") ||
283 translationId.equals("en_tr") ||
284 translationId.equals("en_vi")) {
285
286
290 return null;
291 }
292
293
295 if (limit == 3) {
296 return null;
297 }
298
299 String toText = null;
300
301 try {
302 System.out.println("Translating " + translationId + " " + fromText);
303
304 WebCacheItem wci = new TranslationWebCacheItem(
305 translationId, fromText);
306
307 Translation translation = (Translation)wci.convert("");
308
309 toText = translation.getToText();
310
311 if ((toText != null) &&
312 (toText.indexOf("Babel Fish") != -1)) {
313
314 toText = null;
315 }
316 }
317 catch (Exception e) {
318 e.printStackTrace();
319 }
320
321
323 if (toText == null) {
324 return _translate(translationId, fromText, ++limit);
325 }
326
327 return toText;
328 }
329
330 private String _langDir;
331 private String _langFile;
332
333 }