001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.tools;
022:
023: import com.liferay.portal.kernel.util.StringUtil;
024: import com.liferay.portal.kernel.util.Validator;
025: import com.liferay.portal.kernel.webcache.WebCacheItem;
026: import com.liferay.portlet.translator.model.Translation;
027: import com.liferay.portlet.translator.util.TranslationWebCacheItem;
028: import com.liferay.util.FileUtil;
029:
030: import java.io.BufferedReader;
031: import java.io.BufferedWriter;
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.FileWriter;
035: import java.io.IOException;
036: import java.io.StringReader;
037:
038: import java.util.Properties;
039: import java.util.Set;
040: import java.util.TreeSet;
041:
042: /**
043: * <a href="LangBuilder.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Brian Wing Shun Chan
046: *
047: */
048: public class LangBuilder {
049:
050: public static void main(String[] args) {
051: if (args.length == 2) {
052: new LangBuilder(args[0], args[1]);
053: } else {
054: throw new IllegalArgumentException();
055: }
056: }
057:
058: public LangBuilder(String langDir, String langFile) {
059: try {
060: _langDir = langDir;
061: _langFile = langFile;
062:
063: String content = _orderProps(new File(_langDir + "/"
064: + _langFile + ".properties"));
065:
066: _createProps(content, "ar"); // Arabic
067: _createProps(content, "ca"); // Catalan
068: _createProps(content, "zh_CN"); // Chinese (China)
069: _createProps(content, "zh_TW"); // Chinese (Taiwan)
070: _createProps(content, "cs"); // Czech
071: _createProps(content, "nl"); // Dutch
072: _createProps(content, "fi"); // Finnish
073: _createProps(content, "fr"); // French
074: _createProps(content, "de"); // German
075: _createProps(content, "el"); // Greek
076: _createProps(content, "hu"); // Hungarian
077: _createProps(content, "it"); // Italian
078: _createProps(content, "ja"); // Japanese
079: _createProps(content, "ko"); // Korean
080: _createProps(content, "fa"); // Persian
081: _createProps(content, "pt"); // Portuguese
082: _createProps(content, "ru"); // Russian
083: _createProps(content, "es"); // Spanish
084: _createProps(content, "sv"); // Swedish
085: _createProps(content, "tr"); // Turkish
086: _createProps(content, "vi"); // Vietnamese
087: } catch (Exception e) {
088: e.printStackTrace();
089: }
090: }
091:
092: private void _createProps(String content, String languageId)
093: throws IOException {
094:
095: File propsFile = new File(_langDir + "/" + _langFile + "_"
096: + languageId + ".properties");
097:
098: Properties props = new Properties();
099:
100: if (propsFile.exists()) {
101: props.load(new FileInputStream(propsFile));
102: }
103:
104: File nativePropsFile = new File(_langDir + "/" + _langFile
105: + "_" + languageId + ".properties.native");
106:
107: Properties nativeProps = new Properties();
108:
109: if (nativePropsFile.exists()) {
110: nativeProps.load(new FileInputStream(nativePropsFile));
111: }
112:
113: String translationId = "en_" + languageId;
114:
115: if (translationId.equals("en_zh_CN")) {
116: translationId = "en_zh";
117: } else if (translationId.equals("en_zh_TW")) {
118: translationId = "en_zt";
119: }
120:
121: BufferedReader br = new BufferedReader(
122: new StringReader(content));
123: BufferedWriter bw = new BufferedWriter(new FileWriter(
124: nativePropsFile));
125:
126: String line = null;
127:
128: while ((line = br.readLine()) != null) {
129: line = line.trim();
130:
131: int pos = line.indexOf("=");
132:
133: if (pos != -1) {
134: String key = line.substring(0, pos);
135: String value = line.substring(pos + 1, line.length());
136:
137: String translatedText = props.getProperty(key);
138:
139: if ((translatedText != null)
140: && ((translatedText.indexOf("Babel Fish") != -1) || (translatedText
141: .indexOf("Yahoo! - 999") != -1))) {
142:
143: translatedText = "";
144: }
145:
146: if ((translatedText == null)
147: || translatedText.equals("")) {
148: if (line.indexOf("{") != -1
149: || line.indexOf("<") != -1) {
150: translatedText = value;
151: } else if (key.equals("lang.dir")) {
152: translatedText = "ltr";
153: } else if (key.equals("lang.line.begin")) {
154: translatedText = "left";
155: } else if (key.equals("lang.line.end")) {
156: translatedText = "right";
157: } else {
158: translatedText = _translate(translationId,
159: value, 0);
160: }
161: }
162:
163: if (Validator.isNotNull(translatedText)) {
164: if (translatedText.indexOf("'") != -1) {
165: translatedText = StringUtil.replace(
166: translatedText, "'", "\'");
167: }
168:
169: bw.write(key + "=" + translatedText);
170:
171: bw.newLine();
172: bw.flush();
173: } else if (nativeProps.containsKey(key)) {
174: bw.write(key + "=");
175:
176: bw.newLine();
177: bw.flush();
178: }
179: } else {
180: bw.write(line);
181:
182: bw.newLine();
183: bw.flush();
184: }
185: }
186:
187: br.close();
188: bw.close();
189: }
190:
191: private String _orderProps(File propsFile) throws IOException {
192: String content = FileUtil.read(propsFile);
193:
194: BufferedReader br = new BufferedReader(
195: new StringReader(content));
196: BufferedWriter bw = new BufferedWriter(
197: new FileWriter(propsFile));
198:
199: Set messages = new TreeSet();
200:
201: boolean begin = false;
202:
203: String line = null;
204:
205: while ((line = br.readLine()) != null) {
206: int pos = line.indexOf("=");
207:
208: if (pos != -1) {
209: String key = line.substring(0, pos);
210: String value = line.substring(pos + 1, line.length());
211:
212: messages.add(key + "=" + value);
213: } else {
214: if (begin == true && line.equals("")) {
215: _sortAndWrite(bw, messages);
216: }
217:
218: if (line.equals("")) {
219: begin = !begin;
220: }
221:
222: bw.write(line);
223: bw.newLine();
224: }
225:
226: bw.flush();
227: }
228:
229: if (messages.size() > 0) {
230: _sortAndWrite(bw, messages);
231: }
232:
233: br.close();
234: bw.close();
235:
236: return FileUtil.read(propsFile);
237: }
238:
239: private void _sortAndWrite(BufferedWriter bw, Set messages)
240: throws IOException {
241:
242: String[] messagesArray = (String[]) messages
243: .toArray(new String[0]);
244:
245: for (int i = 0; i < messagesArray.length; i++) {
246: bw.write(messagesArray[i]);
247: bw.newLine();
248: }
249:
250: messages.clear();
251: }
252:
253: private String _translate(String translationId, String fromText,
254: int limit) {
255:
256: if (translationId.equals("en_ar")
257: || translationId.equals("en_ca")
258: || translationId.equals("en_cs")
259: || translationId.equals("en_fi")
260: || translationId.equals("en_hu")
261: || translationId.equals("en_fa")
262: || translationId.equals("en_ru")
263: || translationId.equals("en_sv")
264: || translationId.equals("en_tr")
265: || translationId.equals("en_vi")) {
266:
267: // Automatic translator does not support Arabic, Catalan, Czech,
268: // Finnish, Hungarian, Persian, Russian, Swedish, Turkish,
269: // or Vietnamese
270:
271: return null;
272: }
273:
274: // Limit the number of retries to 3
275:
276: if (limit == 3) {
277: return null;
278: }
279:
280: String toText = null;
281:
282: try {
283: System.out.println("Translating " + translationId + " "
284: + fromText);
285:
286: WebCacheItem wci = new TranslationWebCacheItem(
287: translationId, fromText);
288:
289: Translation translation = (Translation) wci.convert("");
290:
291: toText = translation.getToText();
292:
293: if ((toText != null)
294: && (toText.indexOf("Babel Fish") != -1)) {
295:
296: toText = null;
297: }
298: } catch (Exception e) {
299: e.printStackTrace();
300: }
301:
302: // Keep trying
303:
304: if (toText == null) {
305: return _translate(translationId, fromText, ++limit);
306: }
307:
308: return toText;
309: }
310:
311: private String _langDir;
312: private String _langFile;
313:
314: }
|