2 * Copyright 2010 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing;
19 import java.io.BufferedReader;
20 import java.io.Closeable;
22 import java.io.FileFilter;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.io.OutputStreamWriter;
28 import java.io.Reader;
29 import java.io.Writer;
31 import java.net.URLEncoder;
32 import java.nio.charset.Charset;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.HashMap;
37 import java.util.SortedMap;
38 import java.util.TreeMap;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
43 * <p>A utility which auto-translates English strings in Android string resources using
44 * Google Translate.</p>
46 * <p>Pass the Android client res/ directory as first argument, and optionally message keys
47 * who should be forced to retranslate.
48 * Usage: <code>StringsResourceTranslator android/res/ [key_1 ...]</p>
52 public final class StringsResourceTranslator {
54 private static final Charset UTF8 = Charset.forName("UTF-8");
55 private static final Pattern ENTRY_PATTERN = Pattern.compile("<string name=\"([^\"]+)\">([^<]+)</string>");
56 private static final Pattern STRINGS_FILE_NAME_PATTERN = Pattern.compile("values-(.+)");
57 private static final Pattern TRANSLATE_RESPONSE_PATTERN = Pattern.compile(
58 "\\{\"translatedText\":\"([^\"]+)\"\\}");
60 private static final String APACHE_2_LICENSE =
62 " Copyright (C) 2010 ZXing authors\n" +
64 " Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
65 " you may not use this file except in compliance with the License.\n" +
66 " You may obtain a copy of the License at\n" +
68 " http://www.apache.org/licenses/LICENSE-2.0\n" +
70 " Unless required by applicable law or agreed to in writing, software\n" +
71 " distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
72 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
73 " See the License for the specific language governing permissions and\n" +
74 " limitations under the License.\n" +
77 private static final Map<String,String> LANGUAGE_CODE_MASSAGINGS = new HashMap<String,String>(4);
79 LANGUAGE_CODE_MASSAGINGS.put("ja-rJP", "ja");
80 LANGUAGE_CODE_MASSAGINGS.put("zh-rCN", "zh-cn");
81 LANGUAGE_CODE_MASSAGINGS.put("zh-rTW", "zh-tw");
84 private StringsResourceTranslator() {}
86 public static void main(String[] args) throws IOException {
88 File resDir = new File(args[0]);
89 File valueDir = new File(resDir, "values");
90 File stringsFile = new File(valueDir, "strings.xml");
91 Collection<String> forceRetranslation = Arrays.asList(args).subList(1, args.length);
93 File[] translatedValuesDirs = resDir.listFiles(new FileFilter() {
94 public boolean accept(File file) {
95 return file.isDirectory() && file.getName().startsWith("values-");
98 for (File translatedValuesDir : translatedValuesDirs) {
99 File translatedStringsFile = new File(translatedValuesDir, "strings.xml");
100 translate(stringsFile, translatedStringsFile, forceRetranslation);
105 private static void translate(File englishFile, File translatedFile, Collection<String> forceRetranslation)
108 SortedMap<String,String> english = readLines(englishFile);
109 SortedMap<String,String> translated = readLines(translatedFile);
110 String parentName = translatedFile.getParentFile().getName();
112 Matcher stringsFileNameMatcher = STRINGS_FILE_NAME_PATTERN.matcher(parentName);
113 stringsFileNameMatcher.find();
114 String language = stringsFileNameMatcher.group(1);
115 String massagedLanguage = LANGUAGE_CODE_MASSAGINGS.get(language);
116 if (massagedLanguage != null) {
117 language = massagedLanguage;
120 System.out.println("Translating " + language);
122 File resultTempFile = File.createTempFile(parentName, ".xml");
124 boolean anyChange = false;
127 out = new OutputStreamWriter(new FileOutputStream(resultTempFile), UTF8);
128 out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
129 out.write(APACHE_2_LICENSE);
130 out.write("<resources>\n");
132 for (Map.Entry<String,String> englishEntry : english.entrySet()) {
133 String key = englishEntry.getKey();
134 out.write(" <string name=\"");
138 String translatedString = translated.get(key);
139 if (translatedString == null || forceRetranslation.contains(key)) {
141 translatedString = translateString(englishEntry.getValue(), language);
143 out.write(translatedString);
145 out.write("</string>\n");
148 out.write("</resources>\n");
156 System.out.println(" Writing translations");
157 translatedFile.delete();
158 resultTempFile.renameTo(translatedFile);
162 private static String translateString(String english, String language) throws IOException {
163 System.out.println(" Need translation for " + english);
164 URL translateURL = new URL(
165 "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" +
166 URLEncoder.encode(english, "UTF-8") +
167 "&langpair=en%7C" + language);
168 StringBuilder translateResult = new StringBuilder();
171 in = new InputStreamReader(translateURL.openStream(), UTF8);
172 char[] buffer = new char[1024];
174 while ((charsRead = in.read(buffer)) > 0) {
175 translateResult.append(buffer, 0, charsRead);
180 Matcher m = TRANSLATE_RESPONSE_PATTERN.matcher(translateResult);
182 throw new IOException("No translate result");
184 String translation = m.group(1);
185 System.out.println(" Got translation " + translation);
189 private static SortedMap<String,String> readLines(File file) throws IOException {
190 SortedMap<String,String> entries = new TreeMap<String,String>();
191 BufferedReader reader = null;
193 reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF8));
195 while ((line = reader.readLine()) != null) {
196 Matcher m = ENTRY_PATTERN.matcher(line);
198 String key = m.group(1);
199 String value = m.group(2);
200 entries.put(key, value);
209 private static void quietClose(Closeable closeable) {
210 if (closeable != null) {
213 } catch (IOException ioe) {