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.util.WebCacheable;
28 import com.liferay.portlet.translator.model.Translation;
29 import com.liferay.portlet.translator.util.TranslationConverter;
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 String translationId = "en_" + languageId;
109
110 if (translationId.equals("en_zh_CN")) {
111 translationId = "en_zh";
112 }
113 else if (translationId.equals("en_zh_TW")) {
114 translationId = "en_zt";
115 }
116
117 BufferedReader br = new BufferedReader(new StringReader(content));
118 BufferedWriter bw = new BufferedWriter(new FileWriter(
119 _langDir + "/" + _langFile + "_" + languageId +
120 ".properties.native"));
121
122 String line = null;
123
124 while ((line = br.readLine()) != null) {
125 line = line.trim();
126
127 int pos = line.indexOf("=");
128
129 if (pos != -1) {
130 String key = line.substring(0, pos);
131 String value = line.substring(pos + 1, line.length());
132
133 String translatedText = props.getProperty(key);
134
135 if ((translatedText != null) &&
136 (translatedText.indexOf("Babel Fish") != -1)) {
137
138 translatedText = "";
139 }
140
141 if (translatedText == null || translatedText.equals("")) {
142 if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
143 translatedText = value;
144 }
145 else if (key.equals("lang.dir")) {
146 translatedText = "ltr";
147 }
148 else if (key.equals("lang.line.begin")) {
149 translatedText = "left";
150 }
151 else if (key.equals("lang.line.end")) {
152 translatedText = "right";
153 }
154 else {
155 translatedText = _translate(translationId, value, 0);
156 }
157 }
158
159 if (Validator.isNotNull(translatedText)) {
160 if (translatedText.indexOf("'") != -1) {
161 translatedText =
162 StringUtil.replace(translatedText, "'", "\'");
163 }
164
165 bw.write(key + "=" + translatedText);
166
167 bw.newLine();
168 bw.flush();
169 }
170 }
171 else {
172 bw.write(line);
173
174 bw.newLine();
175 bw.flush();
176 }
177 }
178
179 br.close();
180 bw.close();
181 }
182
183 private String _orderProps(File propsFile) throws IOException {
184 String content = FileUtil.read(propsFile);
185
186 BufferedReader br = new BufferedReader(new StringReader(content));
187 BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
188
189 Set messages = new TreeSet();
190
191 boolean begin = false;
192
193 String line = null;
194
195 while ((line = br.readLine()) != null) {
196 int pos = line.indexOf("=");
197
198 if (pos != -1) {
199 String key = line.substring(0, pos);
200 String value = line.substring(pos + 1, line.length());
201
202 messages.add(key + "=" + value);
203 }
204 else {
205 if (begin == true && line.equals("")) {
206 _sortAndWrite(bw, messages);
207 }
208
209 if (line.equals("")) {
210 begin = !begin;
211 }
212
213 bw.write(line);
214 bw.newLine();
215 }
216
217 bw.flush();
218 }
219
220 if (messages.size() > 0) {
221 _sortAndWrite(bw, messages);
222 }
223
224 br.close();
225 bw.close();
226
227 return FileUtil.read(propsFile);
228 }
229
230 private void _sortAndWrite(BufferedWriter bw, Set messages)
231 throws IOException {
232
233 String[] messagesArray = (String[])messages.toArray(new String[0]);
234
235 for (int i = 0; i < messagesArray.length; i++) {
236 bw.write(messagesArray[i]);
237 bw.newLine();
238 }
239
240 messages.clear();
241 }
242
243 private String _translate(
244 String translationId, String fromText, int limit) {
245
246 if (translationId.equals("en_ar") ||
247 translationId.equals("en_ca") ||
248 translationId.equals("en_cs") ||
249 translationId.equals("en_fi") ||
250 translationId.equals("en_hu") ||
251 translationId.equals("en_fa") ||
252 translationId.equals("en_ru") ||
253 translationId.equals("en_sv") ||
254 translationId.equals("en_tr") ||
255 translationId.equals("en_vi")) {
256
257
261 return null;
262 }
263
264
266 if (limit == 3) {
267 return null;
268 }
269
270 String toText = null;
271
272 try {
273 System.out.println("Translating " + translationId + " " + fromText);
274
275 WebCacheable wc =
276 new TranslationConverter(translationId, fromText);
277
278 Translation translation = (Translation)wc.convert("");
279
280 toText = translation.getToText();
281
282 if ((toText != null) &&
283 (toText.indexOf("Babel Fish") != -1)) {
284
285 toText = null;
286 }
287 }
288 catch (Exception e) {
289 e.printStackTrace();
290 }
291
292
294 if (toText == null) {
295 return _translate(translationId, fromText, ++limit);
296 }
297
298 return toText;
299 }
300
301 private String _langDir;
302 private String _langFile;
303
304 }