From 3a84c47176c1ad196c8352ac918d8899b5f6b92d Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Thu, 4 Jan 2018 04:56:45 +0100 Subject: [PATCH] hopefully fix plurals --- .gitignore | 1 + CheckTranslations.java | 131 +++++++++++++++++++++++++ app/src/main/res/values-ar/strings.xml | 7 -- app/src/main/res/values-cs/strings.xml | 2 - app/src/main/res/values-he/strings.xml | 6 -- app/src/main/res/values-lt/strings.xml | 8 -- app/src/main/res/values-pl/strings.xml | 5 - app/src/main/res/values-ru/strings.xml | 3 - app/src/main/res/values-sk/strings.xml | 3 - fixplurals.sh | 4 + 10 files changed, 136 insertions(+), 34 deletions(-) create mode 100644 CheckTranslations.java create mode 100755 fixplurals.sh diff --git a/.gitignore b/.gitignore index e0e3fc21b..b1746282e 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ gradle.properties *~ .weblate +*.class diff --git a/CheckTranslations.java b/CheckTranslations.java new file mode 100644 index 000000000..fba4fa998 --- /dev/null +++ b/CheckTranslations.java @@ -0,0 +1,131 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.regex.*; +import java.util.Arrays; +import java.util.List; +import java.util.ArrayList; +import java.nio.file.Files; +import java.nio.charset.Charset; + +public final class CheckTranslations { + + private static boolean debug = false; + private static boolean plurals = false; + private static boolean empty = false; + private static boolean remove = false; + private static int checks = 0; + private static int matches = 0; + private static Pattern p, e; + + /** + * Search translated strings.xml files for empty item / plural tags + * and remove them. + * @param args directories which contain string.xml files (in any subdirectory) + * -e option to find all empty string tags + * -p option to find all empty plurals and item tags + * -r option to remove all occurrences from the files + * -d option to see more details + */ + public static void main(String[] args) { + if (args.length < 1 || (args[0].equals("-d") && args.length < 2)) { + System.out.println("Not enough arguments"); + return; + } + for (int i = 0; i < args.length; i++) { + switch (args[i]) { + case "-d": + debug = true; + break; + case "-p": + plurals = true; + break; + case "-e": + empty = true; + break; + case "-r": + remove = true; + break; + } + } + + if (!plurals && !empty) { + plurals = true; + empty = true; + } + + p = Pattern.compile("(|\"/>)"); + e = Pattern.compile("(<\\/string>|\\/>){1})"); + + for (int i = 0; i < args.length; i++) { + if (!args[i].equals("-d") && !args[i].equals("-p") && !args[i].equals("-e") && !args[i].equals("-r")) { + File f = new File(args[i]); + if (f.exists() && !f.isDirectory()) { + checkFile(f); + } else if (f.isDirectory()) { + checkFiles(f.listFiles()); + } else { + System.out.println("'" + args[i] + "' does not exist!"); + } + } + } + + System.out.println(checks + " files were checked."); + System.out.println(matches + " corrupt lines detected."); + if (remove) System.out.println(matches + " corrupt lines removed."); + } + + + private static void checkFiles(File[] f) { + for (int i = 0; i < f.length; i++) { + if (f[i].exists() && !f[i].isDirectory()) { + if (f[i].toString().contains("strings.xml")) { + checkFile(f[i]); + } + } else if (f[i].isDirectory()) { + checkFiles(f[i].listFiles()); + } + } + } + + private static void checkFile(File f) { + // Do not check our original English strings to cause no unwanted changes + // Btw. there should not be empty plural/item tags + if (f.toString().contains("values/strings.xml")) { + return; + } + if (debug) System.out.println("Checking " + f.toString()); + checks++; + + + List lines = new ArrayList(); + boolean checkFailed = false; + + try (BufferedReader br = new BufferedReader(new FileReader(f))) { + String line; + int ln = 0; + while ((line = br.readLine()) != null) { + ln++; + if (plurals && p.matcher(line).find()) { + matches++; + if (debug) System.out.println(" Line " + ln + " was " + ((remove) ? "removed" : "detected") + ": '" + line + "'"); + checkFailed = true; + } else if (empty && e.matcher(line).find()) { + matches++; + checkFailed = true; + if (debug) System.out.println(" Line " + ln + " was " + ((remove) ? "removed" : "detected") + ": '" + line + "'"); + } else { + if (remove) lines.add(line); + } + } + br.close(); + if (remove && checkFailed) { + Files.write(f.toPath(), lines, Charset.forName("UTF-8")); + } + } catch (IOException e) { + System.out.println(e); + } + } +} + diff --git a/app/src/main/res/values-ar/strings.xml b/app/src/main/res/values-ar/strings.xml index a02be21fd..0c70de999 100644 --- a/app/src/main/res/values-ar/strings.xml +++ b/app/src/main/res/values-ar/strings.xml @@ -154,7 +154,6 @@ تقرير معلومات: ماذا حدث: - تعليقك (باللغة الإنجليزية): تفاصيل: @@ -176,12 +175,6 @@ صفر لا تقم با الإختيار (في بعض اللغات) لأنها ليست \"حالة خاصة\" للأندرويد - - - - - - لاتوجد مشاهدات diff --git a/app/src/main/res/values-cs/strings.xml b/app/src/main/res/values-cs/strings.xml index 55434038d..4d17ade32 100644 --- a/app/src/main/res/values-cs/strings.xml +++ b/app/src/main/res/values-cs/strings.xml @@ -205,7 +205,6 @@ otevření ve vyskakovacím okně %s odběratel %s odběratelé %s odběratelů - Žádná zhlédnutí @@ -220,7 +219,6 @@ otevření ve vyskakovacím okně %s video %s videa %s videí - Stahování diff --git a/app/src/main/res/values-he/strings.xml b/app/src/main/res/values-he/strings.xml index c28a742c8..25aed9cd8 100644 --- a/app/src/main/res/values-he/strings.xml +++ b/app/src/main/res/values-he/strings.xml @@ -178,24 +178,18 @@ %s עוקב %s עוקבים - - אין תצוגות תצוגה %s %s תצוגות - - אין סרטונים סרטון %s %s סרטונים - - התחל diff --git a/app/src/main/res/values-lt/strings.xml b/app/src/main/res/values-lt/strings.xml index 1e465458f..e51e71e46 100644 --- a/app/src/main/res/values-lt/strings.xml +++ b/app/src/main/res/values-lt/strings.xml @@ -128,15 +128,10 @@ %s prenumeratorius %s prenumeratoriai - - Vaizdo įrašai - - - Pradėti @@ -214,9 +209,6 @@ Nėra peržiūrų %a peržiūra - - - Nėra vaizdo įrašų diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index aa3672a36..1b27469ff 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -248,22 +248,17 @@ %s subskrybent %s subskrybentów %s subskrybentów - %s odtworzenie - %s odtworzeń - Brak filmów %s film %s filmów - - Większość znaków specjalnych diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index a1c979665..36c25f5cf 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -237,7 +237,6 @@ %s подписчик %s подписчика %s подписчиков - Нет просмотров @@ -245,7 +244,6 @@ %s просмотр %s просмотра %s просмотров - Нет видео @@ -253,7 +251,6 @@ %s видео %s видео %s видео - Элемент удалён diff --git a/app/src/main/res/values-sk/strings.xml b/app/src/main/res/values-sk/strings.xml index eaa8cb48f..653e54dbe 100644 --- a/app/src/main/res/values-sk/strings.xml +++ b/app/src/main/res/values-sk/strings.xml @@ -248,7 +248,6 @@ otvorenie okna na popredí %s odberateľ %s odberatelia %s odberateľov - Žiadne zobrazenia @@ -256,7 +255,6 @@ otvorenie okna na popredí %s zobrazenie %s zobrazenia %s zobrazení - Žiadne videá @@ -264,7 +262,6 @@ otvorenie okna na popredí %s video %s videá %s videí - Položka bola odstránená diff --git a/fixplurals.sh b/fixplurals.sh new file mode 100755 index 000000000..331a01240 --- /dev/null +++ b/fixplurals.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +javac CheckTranslations.java +find app/src -name "*.xml" | grep values | xargs java CheckTranslations -r