Last active
March 19, 2017 20:22
-
-
Save devindi/cfa432903fcd96c4988cd6a2af78c0df to your computer and use it in GitHub Desktop.
Continuous localization
This file contains hidden or 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
| buildscript { | |
| repositories { | |
| jcenter() | |
| } | |
| dependencies { | |
| classpath 'com.xlson.groovycsv:groovycsv:1.1' | |
| } | |
| } | |
| import static com.xlson.groovycsv.CsvParser.parseCsv | |
| import groovy.xml.* | |
| task localize<< { | |
| println 'Localize task started' | |
| if (!project.hasProperty('inputPath')) throw new IllegalStateException("Define input path") | |
| if (!project.hasProperty('fileName')) throw new IllegalStateException("Define csv file name") | |
| def inputDir = new File(inputPath) | |
| def resDirPath = android.sourceSets.main.resDirectories.first() | |
| def defaultLocale = 'en' | |
| inputDir.eachDir { | |
| def locale = it.name | |
| def input = new File(it, fileName) | |
| def outputDir | |
| if (locale == defaultLocale) | |
| outputDir = new File("${resDirPath}/values") | |
| else | |
| outputDir = new File("${resDirPath}/values-${locale}") | |
| if (!outputDir.exists()) outputDir.mkdir() | |
| def output = new File(outputDir, "localized.xml") | |
| processFile(input, output) | |
| } | |
| println 'Localize task finished' | |
| } | |
| void processFile(File input, File output) { | |
| println "Converting ${input.absolutePath} to ${output.absolutePath}" | |
| def UTF8_BOM = "\uFEFF"; | |
| def UTF8_WORD_JOINER = "\u2060" | |
| output.withWriter('UTF-8') { w -> | |
| input.withReader('UTF-8') { r -> | |
| def csvParser = parseCsv( r , separator: ';', readFirstLine: true, columnNames: ['tag', 'key', 'value'] ) | |
| def map = new HashMap<String, Map<String, List<String>>>() | |
| csvParser.each { line -> | |
| String groupId = line['tag'] | |
| groupId = groupId.replace(UTF8_BOM, UTF8_WORD_JOINER) | |
| if (map.containsKey(groupId)) { | |
| def group = map.get(groupId) | |
| String key = line['key'] | |
| key = key.replaceAll("[^_]([0-9])", "") | |
| def values | |
| if (group.containsKey(key)) { | |
| values = group.get(key) | |
| } else { | |
| values = new ArrayList<String>() | |
| group.put(key, values) | |
| } | |
| values.add(line['value']) | |
| } else { | |
| def group = new HashMap<String, List<String>>() | |
| String key = line['key'] | |
| def values = new ArrayList<String>() | |
| values.add(line['value']) | |
| group.put(key, values) | |
| map.put(groupId, group) | |
| } | |
| } | |
| def xmlBuilder = new StreamingMarkupBuilder() | |
| xmlBuilder.encoding = 'UTF-8' | |
| String xml = xmlBuilder.bind { | |
| mkp.xmlDeclaration() | |
| resources { | |
| map.entrySet().each { group -> | |
| mkp.comment group.key | |
| group.value.entrySet().each { resourceItem -> | |
| if (resourceItem.value.size() == 1) { | |
| string name: resourceItem.key, resourceItem.value.get(0) | |
| } else { | |
| "string-array"(name: resourceItem.key) { | |
| resourceItem.value.each { arrItem -> | |
| item(arrItem) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| w.write( XmlUtil.serialize(xml) ) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment