Merge pull request #977 from TobiGr/TranslationChecker-Fix

Fix crash due to no "other" item in plurals
This commit is contained in:
Christian Schabesberger 2018-01-09 13:51:14 +01:00 committed by GitHub
commit 7557acde6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 4 deletions

View File

@ -17,7 +17,8 @@ public final class CheckTranslations {
private static boolean remove = false; private static boolean remove = false;
private static int checks = 0; private static int checks = 0;
private static int matches = 0; private static int matches = 0;
private static Pattern p, e; private static int changes = 0;
private static Pattern p, pb, pe, e, o;
/** /**
* Search translated strings.xml files for empty item / plural tags * Search translated strings.xml files for empty item / plural tags
@ -56,7 +57,10 @@ public final class CheckTranslations {
} }
p = Pattern.compile("(<item quantity=\")(zero|one|two|three|few|many|other)(\"></item>|\"/>)"); p = Pattern.compile("(<item quantity=\")(zero|one|two|three|few|many|other)(\"></item>|\"/>)");
pb = Pattern.compile("(<plurals[\\sa-zA-Z=\"]*>)");
pe = Pattern.compile("(</plurals>)");
e = Pattern.compile("(<string[\\sa-z_\\\"=]*)((><\\/string>|\\/>){1})"); e = Pattern.compile("(<string[\\sa-z_\\\"=]*)((><\\/string>|\\/>){1})");
o = Pattern.compile("(<item quantity=\"other\">)[^</>]*(<\\/item>)");
for (int i = 0; i < args.length; i++) { 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")) { if (!args[i].equals("-d") && !args[i].equals("-p") && !args[i].equals("-e") && !args[i].equals("-r")) {
@ -73,7 +77,9 @@ public final class CheckTranslations {
System.out.println(checks + " files were checked."); System.out.println(checks + " files were checked.");
System.out.println(matches + " corrupt lines detected."); System.out.println(matches + " corrupt lines detected.");
if (remove) System.out.println(matches + " corrupt lines removed."); if (remove) {
System.out.println(matches + " corrupt lines removed and " + changes + " lines fixed.");
}
} }
@ -101,7 +107,8 @@ public final class CheckTranslations {
List<String> lines = new ArrayList<String>(); List<String> lines = new ArrayList<String>();
boolean checkFailed = false; boolean checkFailed = false;
boolean otherDetected = false;
boolean inPlurals = false;
try (BufferedReader br = new BufferedReader(new FileReader(f))) { try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line; String line;
int ln = 0; int ln = 0;
@ -120,6 +127,38 @@ public final class CheckTranslations {
} }
} }
br.close(); br.close();
int pluralsLine = 0;
for (int i = 0; i < lines.size(); i++) {
if (o.matcher(lines.get(i)).find()) {
otherDetected = true;
}
if (plurals && pb.matcher(lines.get(i)).find()) {
inPlurals = true;
pluralsLine = i;
} else if (plurals && pe.matcher(lines.get(i)).find()) {
inPlurals = false;
if (!otherDetected) {
boolean b = false;
check: for(int j = pluralsLine; j < i; j++) {
if (lines.get(j).contains("many")) {
b = true;
pluralsLine = j;
break check;
}
}
if (remove && b) {
if (debug) System.out.println(" Line " + (pluralsLine + 1) + " was " + ((remove) ? "changed" : "detected") + ": '" + lines.get(pluralsLine) + "'");
lines.set(pluralsLine, lines.get(pluralsLine).replace("many", "other"));
changes++;
checkFailed = true;
} else if (debug) {
if (debug) System.out.println(" WARNING: Line " + (i + 1) + " - No <item quantity=\"other\"> found!");
}
}
otherDetected = false;
}
}
if (remove && checkFailed) { if (remove && checkFailed) {
Files.write(f.toPath(), lines, Charset.forName("UTF-8")); Files.write(f.toPath(), lines, Charset.forName("UTF-8"));
} }

View File

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