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.portal.util.InitUtil;
29 import com.liferay.portlet.translator.model.Translation;
30 import com.liferay.portlet.translator.util.TranslationWebCacheItem;
31 import com.liferay.util.FileUtil;
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 static {
54 InitUtil.init();
55 }
56
57 public static void main(String[] args) {
58 if (args.length == 2) {
59 new LangBuilder(args[0], args[1]);
60 }
61 else {
62 throw new IllegalArgumentException();
63 }
64 }
65
66 public LangBuilder(String langDir, String langFile) {
67 try {
68 _langDir = langDir;
69 _langFile = langFile;
70
71 String content = _orderProps(
72 new File(_langDir + "/" + _langFile + ".properties"));
73
74 _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"); }
97 catch (Exception e) {
98 e.printStackTrace();
99 }
100 }
101
102 private void _createProps(String content, String languageId)
103 throws IOException {
104
105 File propsFile = new File(
106 _langDir + "/" + _langFile + "_" + languageId + ".properties");
107
108 Properties props = new Properties();
109
110 if (propsFile.exists()) {
111 props.load(new FileInputStream(propsFile));
112 }
113
114 File nativePropsFile = new File(
115 _langDir + "/" + _langFile + "_" + languageId +
116 ".properties.native");
117
118 Properties nativeProps = new Properties();
119
120 if (nativePropsFile.exists()) {
121 nativeProps.load(new FileInputStream(nativePropsFile));
122 }
123
124 String translationId = "en_" + languageId;
125
126 if (translationId.equals("en_zh_CN")) {
127 translationId = "en_zh";
128 }
129 else if (translationId.equals("en_zh_TW")) {
130 translationId = "en_zt";
131 }
132
133 BufferedReader br = new BufferedReader(new StringReader(content));
134 BufferedWriter bw = new BufferedWriter(new FileWriter(nativePropsFile));
135
136 String line = null;
137
138 while ((line = br.readLine()) != null) {
139 line = line.trim();
140
141 int pos = line.indexOf("=");
142
143 if (pos != -1) {
144 String key = line.substring(0, pos);
145 String value = line.substring(pos + 1, line.length());
146
147 String translatedText = props.getProperty(key);
148
149 if ((translatedText != null) &&
150 ((translatedText.indexOf("Babel Fish") != -1) ||
151 (translatedText.indexOf("Yahoo! - 999") != -1))) {
152
153 translatedText = "";
154 }
155
156 if ((translatedText == null) || translatedText.equals("")) {
157 if (line.indexOf("{") != -1 || line.indexOf("<") != -1) {
158 translatedText = value;
159 }
160 else if (key.equals("lang.dir")) {
161 translatedText = "ltr";
162 }
163 else if (key.equals("lang.line.begin")) {
164 translatedText = "left";
165 }
166 else if (key.equals("lang.line.end")) {
167 translatedText = "right";
168 }
169 else {
170 translatedText = _translate(translationId, value, 0);
171 }
172 }
173
174 if (Validator.isNotNull(translatedText)) {
175 if (translatedText.indexOf("'") != -1) {
176 translatedText =
177 StringUtil.replace(translatedText, "'", "\'");
178 }
179
180 bw.write(key + "=" + translatedText);
181
182 bw.newLine();
183 bw.flush();
184 }
185 else if (nativeProps.containsKey(key)) {
186 bw.write(key + "=");
187
188 bw.newLine();
189 bw.flush();
190 }
191 }
192 else {
193 bw.write(line);
194
195 bw.newLine();
196 bw.flush();
197 }
198 }
199
200 br.close();
201 bw.close();
202 }
203
204 private String _orderProps(File propsFile) throws IOException {
205 String content = FileUtil.read(propsFile);
206
207 BufferedReader br = new BufferedReader(new StringReader(content));
208 BufferedWriter bw = new BufferedWriter(new FileWriter(propsFile));
209
210 Set<String> messages = new TreeSet<String>();
211
212 boolean begin = false;
213
214 String line = null;
215
216 while ((line = br.readLine()) != null) {
217 int pos = line.indexOf("=");
218
219 if (pos != -1) {
220 String key = line.substring(0, pos);
221 String value = line.substring(pos + 1, line.length());
222
223 messages.add(key + "=" + value);
224 }
225 else {
226 if (begin == true && line.equals("")) {
227 _sortAndWrite(bw, messages);
228 }
229
230 if (line.equals("")) {
231 begin = !begin;
232 }
233
234 bw.write(line);
235 bw.newLine();
236 }
237
238 bw.flush();
239 }
240
241 if (messages.size() > 0) {
242 _sortAndWrite(bw, messages);
243 }
244
245 br.close();
246 bw.close();
247
248 return FileUtil.read(propsFile);
249 }
250
251 private void _sortAndWrite(BufferedWriter bw, Set<String> messages)
252 throws IOException {
253
254 String[] messagesArray = messages.toArray(new String[messages.size()]);
255
256 for (int i = 0; i < messagesArray.length; i++) {
257 bw.write(messagesArray[i]);
258 bw.newLine();
259 }
260
261 messages.clear();
262 }
263
264 private String _translate(
265 String translationId, String fromText, int limit) {
266
267 if (translationId.equals("en_ar") ||
268 translationId.equals("en_ca") ||
269 translationId.equals("en_cs") ||
270 translationId.equals("en_fi") ||
271 translationId.equals("en_hu") ||
272 translationId.equals("en_nb") ||
273 translationId.equals("en_fa") ||
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 }