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