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