Put translations in all locales

This commit is contained in:
serso 2016-02-13 23:37:35 +01:00
parent f45585edc6
commit 3f0513cf23

View File

@ -18,7 +18,9 @@ import org.simpleframework.xml.core.Persister;
import java.io.*; import java.io.*;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class Main { public class Main {
private static final Persister persister = new Persister(); private static final Persister persister = new Persister();
@ -32,35 +34,43 @@ public class Main {
outDir.mkdirs(); outDir.mkdirs();
final Resources resources = persister.read(Resources.class, inFile); final Resources resources = persister.read(Resources.class, inFile);
final List<String> languages = new ArrayList<>(); final List<String> languageLocales = new ArrayList<>();
languages.add("ar"); languageLocales.add("ar");
languages.add("cs"); languageLocales.add("cs");
languages.add("es"); languageLocales.add("es");
languages.add("de"); languageLocales.add("de");
languages.add("fi"); languageLocales.add("fi");
languages.add("fr"); languageLocales.add("fr");
languages.add("it"); languageLocales.add("it");
languages.add("nl"); languageLocales.add("nl");
languages.add("pl"); languageLocales.add("pl");
languages.add("pt"); languageLocales.add("pt-rbr");
languages.add("ru"); languageLocales.add("pt-rpt");
languages.add("tr"); languageLocales.add("ru");
languages.add("vi"); languageLocales.add("tr");
languages.add("ja"); languageLocales.add("vi");
languages.add("ja"); languageLocales.add("ja");
languages.add("zh"); languageLocales.add("ja");
languageLocales.add("zh-rcn");
languageLocales.add("zh-rtw");
final CloseableHttpClient client = HttpClients.createDefault(); final CloseableHttpClient client = HttpClients.createDefault();
try { try {
for (String language : languages) { final Map<String, Resources> allTranslations = new HashMap<>();
final Resources translations = new Resources(); for (String languageLocale : 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) { for (ResourceString string : resources.strings) {
final String translation = translate(client, string.value, language); final String translation = translate(client, string.value, language);
if (!TextUtils.isEmpty(translation)) { if (!TextUtils.isEmpty(translation)) {
translations.strings.add(new ResourceString(string.name, translation)); translations.strings.add(new ResourceString(string.name, translation));
} }
} }
saveTranslations(translations, language, outDir, inFile.getName()); }
saveTranslations(translations, languageLocale, outDir, inFile.getName());
} }
} finally { } finally {
@ -130,7 +140,7 @@ public class Main {
} }
private static void saveTranslations(Resources translations, String language, File outDir, String fileName) { private static void saveTranslations(Resources translations, String language, File outDir, String fileName) {
final File dir = new File(outDir, "values-" + androidLanguage(language)); final File dir = new File(outDir, "values-" + language);
dir.mkdirs(); dir.mkdirs();
FileWriter out = null; FileWriter out = null;
try { try {
@ -145,15 +155,12 @@ public class Main {
} }
private static String androidLanguage(String language) { private static String toLanguage(String languageLocale) {
switch (language) { final int i = languageLocale.indexOf('-');
case "pt": if(i >= 0) {
return "pt-rpt"; return languageLocale.substring(0, i);
case "zh":
return "zh-rcn";
default:
return language;
} }
return languageLocale;
} }
private static void close(Closeable closeable) { private static void close(Closeable closeable) {