Skip to content

Instantly share code, notes, and snippets.

@Zeliret
Created March 14, 2018 13:13
Show Gist options
  • Save Zeliret/7a49051ce5f0dd08fce312d605126bf4 to your computer and use it in GitHub Desktop.
Save Zeliret/7a49051ce5f0dd08fce312d605126bf4 to your computer and use it in GitHub Desktop.
static def createStringsFromJson(File inputFile, String outputFolderRootPath) {
def json = new JsonSlurper().parseText(inputFile.text)
def sw = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(sw)
def keywords = ["abstract", "continue", "for", "new", "switch",
"default", "package", "synchronized",
"boolean", "do", "if", "private", "this",
"break", "double", "implements", "protected", "throw",
"byte", "else", "import", "public", "throws",
"case", "instanceof", "return", "transient",
"catch", "extends", "int", "short", "try",
"char", "final", "interface", "static", "void",
"class", "finally", "long", "volatile",
"float", "native", "super", "while"]
xml.resources {
json.each {
k, v -> string(name: (k.toLowerCase() in keywords ? "str_" : "") + "${k.toLowerCase()}", "${v.replace("'", "\\'")}")
}
}
def language = inputFile.name.substring(0, inputFile.name.lastIndexOf("."))
def outputFolderName = "en" == language ? "values" : "values-${language}"
def outputStringsFile = new File("${outputFolderRootPath}/${outputFolderName}/strings.xml")
outputStringsFile.write(sw.toString())
}
task convertLocalizedJsonToStrings {
def translationsDir = new File(translationsRepositoryLocalPath);
translationsDir.eachFileRecurse(FileType.FILES) { file ->
createStringsFromJson(file, "app/src/main/res")
}
}
gradle.projectsEvaluated {
preBuild.dependsOn('convertLocalizedJsonToStrings')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment