Copy translations from AOSP

This commit is contained in:
serso
2016-02-16 15:33:10 +01:00
parent 57cdbad079
commit 7e41f30e06
28 changed files with 355 additions and 116 deletions

View File

@@ -0,0 +1,67 @@
package org.solovyev.android.translations;
import org.apache.http.util.TextUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Android {
private static class TranslationLink {
public final String inName;
public final String outName;
private TranslationLink(String inName, String outName) {
this.inName = inName;
this.outName = outName;
}
}
public static void main(String... args) throws Exception {
final File inDir =
new File("/home/serso/projects/java/aosp/platform/packages-apps-settings/res");
final File outDir = new File("build/translations/res");
Utils.delete(outDir);
outDir.mkdirs();
final List<TranslationLink> translationLinks = new ArrayList<>();
translationLinks.add(new TranslationLink("haptic_feedback_enable_title", "cpp_prefs_vibrate_on_keypress"));
translationLinks.add(new TranslationLink("accelerometer_title", "cpp_prefs_auto_rotate_screen"));
translationLinks.add(new TranslationLink("phone_language", "cpp_prefs_language"));
translationLinks.add(new TranslationLink("night_mode_title", "cpp_prefs_theme"));
translationLinks.add(new TranslationLink("keep_screen_on", "cpp_prefs_keep_screen_on"));
List<String> languageLocales = new ArrayList<>(Utils.languageLocales);
languageLocales.add("");
for (String languageLocale : languageLocales) {
final File inLanguageDir = new File(inDir, Utils.valuesFolderName(languageLocale));
final File inFile = new File(inLanguageDir, "strings.xml");
final Resources resources = Utils.persister.read(Resources.class, inFile);
Resources translations = new Resources();
for (TranslationLink translationLink : translationLinks) {
String translation = translate(resources, translationLink);
if(!TextUtils.isBlank(translation)) {
translations.strings.add(new ResourceString(translationLink.outName, translation));
}
}
Utils.saveTranslations(translations, languageLocale, outDir, "text_imported.xml");
}
}
private static String translate(Resources resources, TranslationLink translationLink) {
for (ResourceString string: resources.strings) {
if (string.name.equals(translationLink.inName)) {
if(TextUtils.isBlank(string.value)) {
return null;
}
if (string.value.length() >= 2 && string.value.startsWith("\"") && string.value
.endsWith("\"")) {
return string.value.substring(1, string.value.length() - 1);
}
return string.value;
}
}
return null;
}
}

View File

@@ -0,0 +1,22 @@
package org.solovyev.android.translations;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Text;
@SuppressWarnings("unused")
@Root(name = "string", strict = false)
public class ResourceString {
@Attribute
public String name;
@Text(required = false)
public String value;
public ResourceString() {
}
ResourceString(String name, String value) {
this.name = name;
this.value = value;
}
}

View File

@@ -0,0 +1,16 @@
package org.solovyev.android.translations;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import java.util.ArrayList;
import java.util.List;
@Root(strict = false)
public class Resources {
@ElementList(inline = true)
public List<ResourceString> strings = new ArrayList<>();
public Resources() {
}
}

View File

@@ -0,0 +1,90 @@
package org.solovyev.android.translations;
import org.apache.http.util.TextUtils;
import org.simpleframework.xml.core.Persister;
import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Utils {
static final List<String> languageLocales = new ArrayList<>();
static final Persister persister = new Persister();
static void saveTranslations(Resources translations, String language, File outDir,
String fileName) {
final File dir = new File(outDir, valuesFolderName(language));
dir.mkdirs();
FileWriter out = null;
try {
out = new FileWriter(new File(dir, fileName));
out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
persister.write(translations, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(out);
}
}
static void close(Closeable closeable) {
if (closeable == null) {
return;
}
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
static boolean delete(File file) {
if(!file.exists()) {
return true;
}
if (file.isFile()) {
return file.delete();
}
boolean deleted = true;
final File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
deleted &= delete(child);
}
}
return deleted && file.delete();
}
static String valuesFolderName(String languageLocale) {
if(TextUtils.isEmpty(languageLocale)) {
return "values";
}
return "values-" + languageLocale;
}
static {
languageLocales.add("ar");
languageLocales.add("cs");
languageLocales.add("es");
languageLocales.add("de");
languageLocales.add("fi");
languageLocales.add("fr");
languageLocales.add("it");
languageLocales.add("nl");
languageLocales.add("pl");
languageLocales.add("pt-rBR");
languageLocales.add("pt-rPT");
languageLocales.add("ru");
languageLocales.add("tr");
languageLocales.add("vi");
languageLocales.add("ja");
languageLocales.add("ja");
languageLocales.add("zh-rCN");
languageLocales.add("zh-rTW");
}
}

View File

@@ -0,0 +1,106 @@
package org.solovyev.android.translations;
import org.apache.commons.codec.Charsets;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.util.TextUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class Wiki {
public static void main(String... args) throws Exception {
final String inFileName = "app/src/main/res/values/text_converter.xml";
final File inFile = new File(inFileName);
final File outDir = new File("build/translations/res");
Utils.delete(outDir);
outDir.mkdirs();
final Resources resources = Utils.persister.read(Resources.class, inFile);
final CloseableHttpClient client = HttpClients.createDefault();
try {
final Map<String, Resources> allTranslations = new HashMap<>();
for (String languageLocale : Utils.languageLocales) {
final String language = toLanguage(languageLocale);
Resources translations = allTranslations.get(language);
if (translations == null) {
translations = new Resources();
allTranslations.put(language, translations);
for (ResourceString string : resources.strings) {
final String translation = translate(client, string.value, language);
if (!TextUtils.isEmpty(translation)) {
translations.strings.add(new ResourceString(string.name, translation));
}
}
}
Utils.saveTranslations(translations, languageLocale, outDir, inFile.getName());
}
} finally {
Utils.close(client);
}
}
private static String translate(CloseableHttpClient client, String word, String language)
throws UnsupportedEncodingException {
final String uri =
"https://en.wikipedia.org/w/api.php?action=query&format=json&prop=langlinks&titles="
+ URLEncoder.encode(word, Charsets.UTF_8.toString()) + "&lllang="
+ language;
final HttpGet request = new HttpGet(
uri);
CloseableHttpResponse response = null;
try {
response = client.execute(request);
final String result = EntityUtils.toString(response.getEntity());
if (TextUtils.isEmpty(result)) {
System.out.println("No translation for " + word);
return null;
}
final JSONObject json = new JSONObject(result);
final JSONObject jsonQuery = json.getJSONObject("query");
final JSONObject jsonPages = jsonQuery.getJSONObject("pages");
for (String key : jsonPages.keySet()) {
final JSONObject jsonPage = jsonPages.getJSONObject(key);
final JSONArray jsonLangLinks = jsonPage.getJSONArray("langlinks");
if (jsonLangLinks.length() > 0) {
final JSONObject jsonLangLink = jsonLangLinks.getJSONObject(0);
final String translation = jsonLangLink.getString("*");
if (TextUtils.isBlank(translation)) {
return null;
}
final int i = translation.lastIndexOf(" (");
if(i >= 0) {
return translation.substring(0, i);
}
return translation;
}
}
} catch (IOException | RuntimeException e) {
e.printStackTrace();
System.err.println("Uri=" + uri);
} finally {
Utils.close(response);
}
return null;
}
private static String toLanguage(String languageLocale) {
final int i = languageLocale.indexOf('-');
if(i >= 0) {
return languageLocale.substring(0, i);
}
return languageLocale;
}
}