在做多语言项目的时候,当涉及地区相对较多的时候,翻译管理是个比较麻烦的事,下面的代码脚本自动将 md 格式的翻译文件转换成 Android 需要的 .xml 格式的文件.
翻译文件格式:
转换之后的格式:
具体代码如下:
import groovy.transform.Field
def mkGroup = 'translation'
@Field def sourcePath = "./translationRes/source/test.md"
//@Field def targetPath = "./app/src/main/res/"
@Field def targetPath = "./translationRes/target/"
@Field def androidKey = 2
//@Field def iosKey = 2
//@Field def COLUMN_AU_EN = 2 // au-en 列号.
@Field def COLUMN_CN_ZH = 3 // cn-zh 列号.
//@Field def COLUMN_HK_ZH = 4 // hk-zh 列号.
//@Field def COLUMN_KR_KO = 5 // kr-ko 列号.
@Field def COLUMN_MY_EN = 5 // my-en 列号.
@Field def COLUMN_MY_ZH = 4 // my.zh 列号.
@Field def COLUMN_MY_MS = 6 // my-ms 列号.
//@Field def COLUMN_NZ_EN = 9 // nz-en 列号.
//@Field def COLUMN_PH_EN = 10 // ph-en 列号.
//@Field def COLUMN_SG_EN = 11 // sg-en 列号.
//@Field def COLUMN_SG_ZH = 12 // sg-zh 列号.
//@Field def COLUMN_TW_ZH = 13 // tw-zh 列号.
@Field File cn_zh_file
//@Field File hk_zh_file
//@Field File tw_zh_file
//@Field File au_en_file
//@Field File nz_en_file
//@Field File kr_ko_file
@Field File my_en_file
//@Field File sg_en_file
@Field File my_zh_file
//@Field File sg_zh_file
@Field File my_ms_file
//@Field File ph_en_file
ext {
curLine = 1
}
task translationMain {
group mkGroup
description '翻译.'
new File(sourcePath).splitEachLine("\\|", "UTF-8") {
if (curLine == 1) {
println "curLine = $curLine , 内容 = $it"
createLanguageFiles(it)
} else if (curLine == 2) {
println "正在写入..."
} else { // 从第三行还是才是 翻译.前两行是 翻译标题 和 分割线.
writeKeyValue(it)
}
curLine++
}
writeEndTag2Xmls()
}
def writeEndTag2Xmls() {
def resources = "</resources>\n"
cn_zh_file.append(resources)
// hk_zh_file.append(resources)
//
// tw_zh_file.append(resources)
//
// au_en_file.append(resources)
//
// nz_en_file.append(resources)
//
// kr_ko_file.append(resources)
//
my_en_file.append(resources)
//
// sg_en_file.append(resources)
//
my_zh_file.append(resources)
//
// sg_zh_file.append(resources)
//
my_ms_file.append(resources)
//
// ph_en_file.append(resources)
}
def writeKeyValue(ary) {
def key = ary[androidKey]
def value_cn_zh = ary[COLUMN_CN_ZH]
writeKeyValue2Xmls(cn_zh_file, key, value_cn_zh)
// def value_hk_zh = ary[COLUMN_HK_ZH]
// writeKeyValue2Xmls(hk_zh_file,key, value_hk_zh)
//
// def value_tw_zh = ary[COLUMN_TW_ZH]
// writeKeyValue2Xmls(tw_zh_file,key, value_tw_zh)
//
// def value_au_en = ary[COLUMN_AU_EN]
// writeKeyValue2Xmls(au_en_file,key, value_au_en)
//
// def value_nz_en = ary[COLUMN_NZ_EN]
// writeKeyValue2Xmls(nz_en_file,key, value_nz_en)
//
// def value_kr_ko = ary[COLUMN_KR_KO]
// writeKeyValue2Xmls(kr_ko_file,key, value_kr_ko)
def value_my_en = ary[COLUMN_MY_EN]
writeKeyValue2Xmls(my_en_file, key, value_my_en)
// def value_sg_en = ary[COLUMN_SG_EN]
// writeKeyValue2Xmls(sg_en_file,key, value_sg_en)
def value_my_zh = ary[COLUMN_MY_ZH]
writeKeyValue2Xmls(my_zh_file, key, value_my_zh)
// def value_sg_zh = ary[COLUMN_SG_ZH]
// writeKeyValue2Xmls(sg_zh_file,key, value_sg_zh)
def value_my_ms = ary[COLUMN_MY_MS]
writeKeyValue2Xmls(my_ms_file, key, value_my_ms)
// def value_ph_en = ary[COLUMN_PH_EN]
// writeKeyValue2Xmls(ph_en_file,key, value_ph_en)
}
def writeKeyValue2Xmls(file, key, value) {
if (key != null) {
key = key.trim().replaceAll("\\\\", "")
if (key != "-"
&& key != "") {
value = value.trim()
.replaceAll("%d", "%s") // 占位符替换.
.replaceAll("%@", "%s")
.replaceAll("%ld", "%s")
.replaceAll("\\\\!", "!")
.replaceAll("'", "\\\\'") // 单引号转义
.replaceAll("\\\\\\\\n", "\\\\n") // 换行转义
.replaceAll("\\\\\\\\", "") // 去掉双反斜杠
.replaceAll("\$", "")
file.append(" <string name=\"" + key + "\">" + value + "</string>\n", "UTF-8")
}
} else {
println "写入 key-value 出现错误 : $curLine 行"
}
}
def createLanguageFiles(ary) {
println "开始 生成各个语言文件..."
def resources = "<resources>\n"
def suffix = ".xml"
new File(targetPath + "values-zh-rCN/").mkdirs()
cn_zh_file = new File(targetPath + "values-zh-rCN/" + "strings" + suffix)
cn_zh_file.withPrintWriter { it -> it.append(resources) }
// new File(targetPath + "values-zh-rHK/").mkdirs()
// hk_zh_file = new File(targetPath + "values-zh-rHK/" + "strings" + suffix)
// hk_zh_file.withPrintWriter { it -> it.append(resources) }
//
// new File(targetPath + "values-zh-rTW/").mkdirs()
// tw_zh_file = new File(targetPath + "values-zh-rTW/" + "strings" + suffix)
// tw_zh_file.withPrintWriter { it -> it.append(resources) }
//
// new File(targetPath + "values-en-rAU/").mkdirs()
// au_en_file = new File(targetPath + "values-en-rAU/" + "strings" + suffix)
// au_en_file.withPrintWriter { it -> it.append(resources) }
//
// new File(targetPath + "values-en-rNZ/").mkdirs()
// nz_en_file = new File(targetPath + "values-en-rNZ/" + "strings" + suffix)
// nz_en_file.withPrintWriter { it -> it.append(resources) }
//
// new File(targetPath + "values-ko-rKR/").mkdirs()
// kr_ko_file = new File(targetPath + "values-ko-rKR/" + "strings" + suffix)
// kr_ko_file.withPrintWriter { it -> it.append(resources) }
new File(targetPath + "values-en-rMY/").mkdirs()
my_en_file = new File(targetPath + "values-en-rMY/" + "strings" + suffix)
my_en_file.withPrintWriter { it -> it.append(resources) }
// new File(targetPath + "values-en-rSG/").mkdirs()
// sg_en_file = new File(targetPath + "values-en-rSG/" + "strings" + suffix)
// sg_en_file.withPrintWriter { it -> it.append(resources) }
new File(targetPath + "values-zh/").mkdirs()
my_zh_file = new File(targetPath + "values-zh/" + "strings" + suffix)
my_zh_file.withPrintWriter { it -> it.append(resources) }
//
// new File(targetPath + "values-zh-rSG/").mkdirs()
// sg_zh_file = new File(targetPath + "values-zh-rSG/" + "strings" + suffix)
// sg_zh_file.withPrintWriter { it -> it.append(resources) }
new File(targetPath + "values-ms-rMY/").mkdirs()
my_ms_file = new File(targetPath + "values-ms-rMY/" + "strings" + suffix)
my_ms_file.withPrintWriter { it -> it.append(resources) }
//
// new File(targetPath + "values-en-rPH/").mkdirs()
// ph_en_file = new File(targetPath + "values-en-rPH/" + "strings" + suffix)
// ph_en_file.withPrintWriter { it -> it.append(resources) }
}
具体使用:
1.在项目根目录创建 translation.gradle 文件,将脚本代码复制到文件中
2.在项目根目录的 build.gradle 中 应用脚本文件,如下图
这种写法存在以下问题:
1.此脚本文件会在每次执行 gradle 的时候都会进行无必要的运行.
2.修改配置比较麻烦,如果需要增加语言或者修改语言就需要去源代码里修改,解决方法就是可以使用 gradle plugin方式.