-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f29d773
commit a2ce6f4
Showing
3 changed files
with
68 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// custom tasks for gradle | ||
|
||
// run with: ./gradlew checkLanguages | ||
task checkLanguages { | ||
|
||
// TODO Feeder has a better version of this, but you need to change the locales code. See: | ||
// https://gitlab.com/spacecowboy/Feeder/-/blob/master/app/build.gradle.kts#L344 | ||
|
||
group 'NoNonsenseNotes custom tasks' | ||
description 'Ensure all languages in res/values-*/strings.xml are selectable in the spinner' | ||
|
||
var readFromFolders = () -> { | ||
// Read res/values/ folders, add their codes to localesInFolders | ||
File resDir = new File(projectDir, "app/src/main/res") | ||
FileFilter theFilter = (file) -> file.name.toLowerCase().startsWith("values") | ||
File[] dirs = resDir.listFiles(theFilter) | ||
|
||
var localesInFolders = new ArrayList<String>() | ||
for (File valueFolder : dirs) { | ||
// exclude folders like 'values-600dp' which don't represent languages | ||
if (valueFolder.name.contains("0dp")) continue | ||
if (valueFolder.name.contains("values-land")) continue | ||
|
||
String localeCode = valueFolder.name.replace("values-", "") | ||
localesInFolders.add(localeCode) | ||
} | ||
return localesInFolders | ||
} | ||
|
||
var readFromXML = () -> { | ||
// read the language list in arrays.xml | ||
File arrFile = new File(projectDir, "app/src/main/res/values/arrays.xml") | ||
var reader = new FileReader(arrFile.absolutePath) | ||
String xmlText = reader.text | ||
reader.close() | ||
|
||
// locate where the "translated_langs" array is | ||
int posBegin = xmlText.indexOf("<string-array name=\"translated_langs\" tools:ignore=\"MissingTranslation\">") | ||
int posEnd = xmlText.indexOf("</string-array>", posBegin) | ||
String onlyLangArray = xmlText.substring(posBegin, posEnd) | ||
|
||
// add locale codes to localesInXML | ||
var localesInXML = new ArrayList<String>() | ||
for (String line : onlyLangArray.split('\n')) { | ||
if (!line.contains("<item>")) continue | ||
|
||
String localeCode = line.replace("<item>", "").replace("</item>", "") | ||
localesInXML.add(localeCode) | ||
} | ||
|
||
return localesInXML | ||
} | ||
|
||
doLast { | ||
// actually run the task | ||
var listDir = readFromFolders() | ||
var listXML = readFromXML() | ||
if (listDir.size != listXML.size) { | ||
String errMsg = "Outdated translation: XML element has " + listXML.size | ||
errMsg += " locales, but the values-* folders are " + listDir.size | ||
throw new GradleException(errMsg) | ||
} | ||
} | ||
} |