hopefully fix plurals

This commit is contained in:
Christian Schabesberger 2018-01-04 04:56:45 +01:00
parent 20c2426128
commit 3a84c47176
10 changed files with 136 additions and 34 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@
gradle.properties
*~
.weblate
*.class

131
CheckTranslations.java Normal file
View File

@ -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("(<item quantity=\")(zero|one|two|three|few|many|other)(\"></item>|\"/>)");
e = Pattern.compile("(<string[\\sa-z_\\\"=]*)((><\\/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<String> lines = new ArrayList<String>();
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);
}
}
}

View File

@ -154,7 +154,6 @@
<string name="error_snackbar_action">تقرير</string>
<string name="what_device_headline">معلومات:</string>
<string name="what_happened_headline">ماذا حدث:</string>
<string name="info_labels"/>
<string name="your_comment">تعليقك (باللغة الإنجليزية):</string>
<string name="error_details_headline">تفاصيل:</string>
@ -176,12 +175,6 @@
<string name="no_subscribers">صفر لا تقم با الإختيار (في بعض اللغات) لأنها ليست \"حالة خاصة\" للأندرويد</string>
<plurals name="subscribers">
<item quantity="zero"></item>
<item quantity="one"></item>
<item quantity="two"></item>
<item quantity="few"></item>
<item quantity="many"></item>
<item quantity="other"></item>
</plurals>
<string name="no_views">لاتوجد مشاهدات</string>

View File

@ -205,7 +205,6 @@ otevření ve vyskakovacím okně</string>
<item quantity="one">%s odběratel</item>
<item quantity="few">%s odběratelé</item>
<item quantity="many">%s odběratelů</item>
<item quantity="other"/>
</plurals>
<string name="no_views">Žádná zhlédnutí</string>
@ -220,7 +219,6 @@ otevření ve vyskakovacím okně</string>
<item quantity="one">%s video</item>
<item quantity="few">%s videa</item>
<item quantity="many">%s videí</item>
<item quantity="other"/>
</plurals>
<string name="settings_category_downloads_title">Stahování</string>

View File

@ -178,24 +178,18 @@
<plurals name="subscribers">
<item quantity="one">%s עוקב</item>
<item quantity="two">%s עוקבים</item>
<item quantity="many"></item>
<item quantity="other"></item>
</plurals>
<string name="no_views">אין תצוגות</string>
<plurals name="views">
<item quantity="one">תצוגה %s</item>
<item quantity="two">%s תצוגות</item>
<item quantity="many"></item>
<item quantity="other"></item>
</plurals>
<string name="no_videos">אין סרטונים</string>
<plurals name="videos">
<item quantity="one">סרטון %s</item>
<item quantity="two">%s סרטונים</item>
<item quantity="many"></item>
<item quantity="other"></item>
</plurals>
<string name="start">התחל</string>

View File

@ -128,15 +128,10 @@
<plurals name="subscribers">
<item quantity="one">%s prenumeratorius</item>
<item quantity="few">%s prenumeratoriai</item>
<item quantity="many"/>
<item quantity="other"/>
</plurals>
<plurals name="videos">
<item quantity="one">Vaizdo įrašai</item>
<item quantity="few"/>
<item quantity="many"/>
<item quantity="other"/>
</plurals>
<string name="start">Pradėti</string>
@ -214,9 +209,6 @@
<string name="no_views">Nėra peržiūrų</string>
<plurals name="views">
<item quantity="one">%a peržiūra</item>
<item quantity="few"></item>
<item quantity="many"></item>
<item quantity="other"></item>
</plurals>
<string name="no_videos">Nėra vaizdo įrašų</string>

View File

@ -248,22 +248,17 @@
<item quantity="one">%s subskrybent</item>
<item quantity="few">%s subskrybentów</item>
<item quantity="many">%s subskrybentów</item>
<item quantity="other"/>
</plurals>
<plurals name="views">
<item quantity="one">%s odtworzenie</item>
<item quantity="few"/>
<item quantity="many">%s odtworzeń</item>
<item quantity="other"/>
</plurals>
<string name="no_videos">Brak filmów</string>
<plurals name="videos">
<item quantity="one">%s film</item>
<item quantity="few">%s filmów</item>
<item quantity="many"/>
<item quantity="other"/>
</plurals>
<string name="charset_most_special_characters">Większość znaków specjalnych</string>

View File

@ -237,7 +237,6 @@
<item quantity="one">%s подписчик</item>
<item quantity="few">%s подписчика</item>
<item quantity="many">%s подписчиков</item>
<item quantity="other"/>
</plurals>
<string name="no_views">Нет просмотров</string>
@ -245,7 +244,6 @@
<item quantity="one">%s просмотр</item>
<item quantity="few">%s просмотра</item>
<item quantity="many">%s просмотров</item>
<item quantity="other"/>
</plurals>
<string name="no_videos">Нет видео</string>
@ -253,7 +251,6 @@
<item quantity="one">%s видео</item>
<item quantity="few">%s видео</item>
<item quantity="many">%s видео</item>
<item quantity="other"/>
</plurals>
<string name="item_deleted">Элемент удалён</string>

View File

@ -248,7 +248,6 @@ otvorenie okna na popredí</string>
<item quantity="one">%s odberateľ</item>
<item quantity="few">%s odberatelia</item>
<item quantity="many">%s odberateľov</item>
<item quantity="other"></item>
</plurals>
<string name="no_views">Žiadne zobrazenia</string>
@ -256,7 +255,6 @@ otvorenie okna na popredí</string>
<item quantity="one">%s zobrazenie</item>
<item quantity="few">%s zobrazenia</item>
<item quantity="many">%s zobrazení</item>
<item quantity="other"></item>
</plurals>
<string name="no_videos">Žiadne videá</string>
@ -264,7 +262,6 @@ otvorenie okna na popredí</string>
<item quantity="one">%s video</item>
<item quantity="few">%s videá</item>
<item quantity="many">%s videí</item>
<item quantity="other"></item>
</plurals>
<string name="item_deleted">Položka bola odstránená</string>

4
fixplurals.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
javac CheckTranslations.java
find app/src -name "*.xml" | grep values | xargs java CheckTranslations -r