1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.util.StringUtil;
26 import com.liferay.portal.kernel.util.Validator;
27 import com.liferay.portal.kernel.webcache.WebCacheItem;
28 import com.liferay.portlet.translator.model.Translation;
29 import com.liferay.portlet.translator.util.TranslationWebCacheItem;
30 import com.liferay.util.FileUtil;
31
32 import java.io.BufferedReader;
33 import java.io.BufferedWriter;
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.FileWriter;
37 import java.io.IOException;
38 import java.io.StringReader;
39
40 import java.util.Properties;
41 import java.util.Set;
42 import java.util.TreeSet;
43
44
50 public class LangBuilder {
51
52 public static void main(String[] args) {
53 if (args.length == 2) {
54 new LangBuilder(args[0], args[1]);
55 }
56 else {
57 throw new IllegalArgumentException();
58 }
59 }
60
61 public LangBuilder(String langDir, String langFile) {
62 try {
63 _langDir = langDir;
64 _langFile = langFile;
65
66 String content = _orderProps(
67 new File(_langDir + "/" + _langFile + ".properties"));
68
69 _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, "fa"); _createProps(content, "pt"); _createProps(content, "ru"); _createProps(content, "es"); _createProps(content, "sv"); _createProps(content, "tr"); _createProps(content, "vi"); }
91 catch (Exception e) {
92 e.printStackTrace();
93 }
94 }
95
96 private void _createProps(String content, String languageId)
97 throws IOException {
98
99 File propsFile = new File(
100 _langDir + "/" + _langFile + "_" + languageId + ".properties");
101
102 Properties props = new Properties();
103
104 if (propsFile.exists()) {
105 props.load(new FileInputStream(propsFile));
106 }
107
108 File nativePropsFile = new File(
109 _langDir + "/" + _langFile + "_" + languageId +
110 ".properties.native");
111
112 Properties nativeProps = new Properties();
113
114 if (nativePropsFile.exists()) {
115 nativeProps.load(new FileInputStream(nativePropsFile));
116 }
117
118 String translationId = "en_" + languageId;
119
120 if (translationId.equals("en_zh_CN")) {
121 translationId = "en_zh";
122 }
123 else if (translationId.equals("en_zh_TW")) {
124 translationId = "en_zt";
125 }
126
127 BufferedReader br = new BufferedReader(new StringReader(content));
128 BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
129
130 String line = null;
131
132 while ((line = br.readLine()) != null) {
133 line = line.trim();
134
135 int pos = line.indexOf("=");
136
137 if (pos != -1) {
138 String key = line.substring(0, pos);
139 String value = line.substring(pos + 1, line.length());
140
141 String translatedText = props.getProperty(key);
142
143 if ((translatedText != null) &&
144 ((translatedText.indexOf("Babel Fish") != -1) ||
145 (translatedText.indexOf("Yahoo! - 999") != -1))) {
146
147 translatedText = "";
148 }
149
150 if ((translatedText == null) || translatedText.equals("")) {
151 if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
152 translatedText = value;
153 }
154 else if (key.equals("lang.dir")) {
155 translatedText = "ltr";
156 }
157 else if (key.equals("lang.line.begin")) {
158 translatedText = "left";
159 }
160 else if (key.equals("lang.line.end")) {
161 translatedText = "right";
162 }
163 else {
164 translatedText = _translate(translationId, value, 0);
165 }
166 }
167
168 if (Validator.isNotNull(translatedText)) {
169 if (translatedText.indexOf("'") != -1) {
170 translatedText =
171 StringUtil.replace(translatedText, "'", "\'");
172 }
173
174 bw.write(key + "=" + translatedText);
175
176 bw.newLine();
177 bw.flush();
178 }
179 else if (nativeProps.containsKey(key)) {
180 bw.write(key + "=");
181
182 bw.newLine();
183 bw.flush();
184 }
185 }
186 else {
187 bw.write(line);
188
189 bw.newLine();
190 bw.flush();
191 }
192 }
193
194 br.close();
195 bw.close();
196 }
197
198 private String _orderProps(File propsFile) throws IOException {
199 String content = FileUtil.read(propsFile);
200
201 BufferedReader br = new BufferedReader(new StringReader(content));
202 BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
203
204 Set messages = new TreeSet();
205
206 boolean begin = false;
207
208 String line = null;
209
210 while ((line = br.readLine()) != null) {
211 int pos = line.indexOf("=");
212
213 if (pos != -1) {
214 String key = line.substring(0, pos);
215 String value = line.substring(pos + 1, line.length());
216
217 messages.add(key + "=" + value);
218 }
219 else {
220 if (begin == true && line.equals("")) {
221 _sortAndWrite(bw, messages);
222 }
223
224 if (line.equals("")) {
225 begin = !begin;
226 }
227
228 bw.write(line);
229 bw.newLine();
230 }
231
232 bw.flush();
233 }
234
235 if (messages.size() > 0) {
236 _sortAndWrite(bw, messages);
237 }
238
239 br.close();
240 bw.close();
241
242 return FileUtil.read(propsFile);
243 }
244
245 private void _sortAndWrite(BufferedWriter bw, Set messages)
246 throws IOException {
247
248 String[] messagesArray = (String[])messages.toArray(new String[0]);
249
250 for (int i = 0; i < messagesArray.length; i++) {
251 bw.write(messagesArray[i]);
252 bw.newLine();
253 }
254
255 messages.clear();
256 }
257
258 private String _translate(
259 String translationId, String fromText, int limit) {
260
261 if (translationId.equals("en_ar") ||
262 translationId.equals("en_ca") ||
263 translationId.equals("en_cs") ||
264 translationId.equals("en_fi") ||
265 translationId.equals("en_hu") ||
266 translationId.equals("en_fa") ||
267 translationId.equals("en_ru") ||
268 translationId.equals("en_sv") ||
269 translationId.equals("en_tr") ||
270 translationId.equals("en_vi")) {
271
272
276 return null;
277 }
278
279
281 if (limit == 3) {
282 return null;
283 }
284
285 String toText = null;
286
287 try {
288 System.out.println("Translating " + translationId + " " + fromText);
289
290 WebCacheItem wci = new TranslationWebCacheItem(
291 translationId, fromText);
292
293 Translation translation = (Translation)wci.convert("");
294
295 toText = translation.getToText();
296
297 if ((toText != null) &&
298 (toText.indexOf("Babel Fish") != -1)) {
299
300 toText = null;
301 }
302 }
303 catch (Exception e) {
304 e.printStackTrace();
305 }
306
307
309 if (toText == null) {
310 return _translate(translationId, fromText, ++limit);
311 }
312
313 return toText;
314 }
315
316 private String _langDir;
317 private String _langFile;
318
319 }