1
19
20 package com.liferay.portal.tools;
21
22 import com.liferay.portal.kernel.util.FileUtil;
23 import com.liferay.portal.kernel.util.StringUtil;
24 import com.liferay.portal.kernel.util.Validator;
25 import com.liferay.portal.kernel.webcache.WebCacheItem;
26 import com.liferay.portal.util.InitUtil;
27 import com.liferay.portlet.translator.model.Translation;
28 import com.liferay.portlet.translator.util.TranslationWebCacheItem;
29
30 import java.io.BufferedReader;
31 import java.io.BufferedWriter;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileWriter;
35 import java.io.IOException;
36 import java.io.StringReader;
37
38 import java.util.Properties;
39 import java.util.Set;
40 import java.util.TreeSet;
41
42
48 public class LangBuilder {
49
50 public static void main(String[] args) {
51 InitUtil.initWithSpring();
52
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, "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"); }
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("'") != -1) {
181 translatedText = StringUtil.replace(
182 translatedText, "'", "\'");
183 }
184
185 bw.write(key + "=" + translatedText);
186
187 bw.newLine();
188 bw.flush();
189 }
190 else if (nativeProps.containsKey(key)) {
191 bw.write(key + "=");
192
193 bw.newLine();
194 bw.flush();
195 }
196 }
197 else {
198 bw.write(line);
199
200 bw.newLine();
201 bw.flush();
202 }
203 }
204
205 br.close();
206 bw.close();
207 }
208
209 private String _orderProps(File propsFile) throws IOException {
210 String content = FileUtil.read(propsFile);
211
212 BufferedReader br = new BufferedReader(new StringReader(content));
213 BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
214
215 Set<String> messages = new TreeSet<String>();
216
217 boolean begin = false;
218
219 String line = null;
220
221 while ((line = br.readLine()) != null) {
222 int pos = line.indexOf("=");
223
224 if (pos != -1) {
225 String key = line.substring(0, pos);
226 String value = line.substring(pos + 1, line.length());
227
228 messages.add(key + "=" + value);
229 }
230 else {
231 if (begin == true && line.equals("")) {
232 _sortAndWrite(bw, messages);
233 }
234
235 if (line.equals("")) {
236 begin = !begin;
237 }
238
239 bw.write(line);
240 bw.newLine();
241 }
242
243 bw.flush();
244 }
245
246 if (messages.size() > 0) {
247 _sortAndWrite(bw, messages);
248 }
249
250 br.close();
251 bw.close();
252
253 return FileUtil.read(propsFile);
254 }
255
256 private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
257 throws IOException {
258
259 String[] messagesArray = messages.toArray(new String[messages.size()]);
260
261 for (int i = 0; i < messagesArray.length; i++) {
262 bw.write(messagesArray[i]);
263 bw.newLine();
264 }
265
266 messages.clear();
267 }
268
269 private String _translate(
270 String translationId, String fromText, int limit) {
271
272 if (translationId.equals("en_ar") ||
273 translationId.equals("en_eu") ||
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_sk") ||
283 translationId.equals("en_sv") ||
284 translationId.equals("en_tr") ||
285 translationId.equals("en_vi")) {
286
287
291 return null;
292 }
293
294
296 if (limit == 3) {
297 return null;
298 }
299
300 String toText = null;
301
302 try {
303 System.out.println("Translating " + translationId + " " + fromText);
304
305 WebCacheItem wci = new TranslationWebCacheItem(
306 translationId, fromText);
307
308 Translation translation = (Translation)wci.convert("");
309
310 toText = translation.getToText();
311
312 if ((toText != null) &&
313 (toText.indexOf("Babel Fish") != -1)) {
314
315 toText = null;
316 }
317 }
318 catch (Exception e) {
319 e.printStackTrace();
320 }
321
322
324 if (toText == null) {
325 return _translate(translationId, fromText, ++limit);
326 }
327
328 return toText;
329 }
330
331 private String _langDir;
332 private String _langFile;
333
334 }