From 25ed8952f9e6f68f3ea5f667e22cab61a4a412db Mon Sep 17 00:00:00 2001 From: TobiGr Date: Tue, 9 Jan 2018 13:24:34 +0100 Subject: [PATCH] Fix crash due to no "other" item in plurals --- CheckTranslations.java | 45 +++++++++++++++++++++++++++++++++++++++--- fixplurals.sh | 2 +- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/CheckTranslations.java b/CheckTranslations.java index fba4fa998..8b7c2388b 100644 --- a/CheckTranslations.java +++ b/CheckTranslations.java @@ -17,7 +17,8 @@ public final class CheckTranslations { private static boolean remove = false; private static int checks = 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 @@ -56,7 +57,10 @@ public final class CheckTranslations { } p = Pattern.compile("(|\"/>)"); + pb = Pattern.compile("()"); + pe = Pattern.compile("()"); e = Pattern.compile("(<\\/string>|\\/>){1})"); + o = Pattern.compile("()[^]*(<\\/item>)"); 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")) { @@ -73,7 +77,9 @@ public final class CheckTranslations { System.out.println(checks + " files were checked."); 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 lines = new ArrayList(); boolean checkFailed = false; - + boolean otherDetected = false; + boolean inPlurals = false; try (BufferedReader br = new BufferedReader(new FileReader(f))) { String line; int ln = 0; @@ -120,6 +127,38 @@ public final class CheckTranslations { } } 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 found!"); + } + } + otherDetected = false; + } + + } if (remove && checkFailed) { Files.write(f.toPath(), lines, Charset.forName("UTF-8")); } diff --git a/fixplurals.sh b/fixplurals.sh index 331a01240..8a637aa91 100755 --- a/fixplurals.sh +++ b/fixplurals.sh @@ -1,4 +1,4 @@ #!/bin/bash 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