1 Star 0 Fork 4

JoeBlack/AppExp

forked from mojie/AppExp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
niu_string_format.gradle 5.88 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* @describe:小牛电动文本转换工具
* @Author: lixiaopeng
* @Date: 2020/10/10
*/
/**
* 国内批量转换
*
* 使用步骤
* 1.将对应翻译文件复制到对应strings.xml文件
* 2.点击运行脚本
*
* 复制完成后格式如下:
*
* <!--之前写好的不用管-->
* <string name="Text_1096_C">All videos</string>
* <string name="Text_1097_C">Images and videos</string>
* //----------------2020/09/22新增的------------
* "Text_1094_L" = "Please leave a review on this service";
* "Text_1095_L" = "Please edit the review";
*/
task domesticFormatStrings(group: "translationConversion") {
doFirst {
def fileDirPtah = "niu-res-domestic/src/main/res"
ArrayList<File> fileList = findStrFile(new File(fileDirPtah))
println("找到"+fileList.size()+"个文件")
for (File file:fileList){
doStringFormat(file.getAbsolutePath(),file.getAbsolutePath())
}
}
}
/**
* 海外批量转换
*
* 使用步骤
* 1.将对应翻译文件复制到对应strings.xml文件
* 2.点击运行脚本
*
* 复制完成后格式如下:
*
* <!--之前写好的不用管-->
* <string name="Text_1096_C">All videos</string>
* <string name="Text_1097_C">Images and videos</string>
* //----------------2020/09/22新增的------------
* "Text_1094_L" = "Please leave a review on this service";
* "Text_1095_L" = "Please edit the review";
*/
task overseasFormatStrings(group: "translationConversion") {
doFirst {
def fileDirPtah = "niu-res-overseas/src/main/res/"
ArrayList<File> fileList = findStrFile(new File(fileDirPtah))
for (File file:fileList){
doStringFormat(file.getAbsolutePath(),file.getAbsolutePath())
}
}
}
/**
* 将国内中文版copy到海外版中文版,并打开海外版的中文文件和英文文件
*/
task domestic2OverseasZhString(group: "translationConversion"){
def filePtah = "niu-res-domestic/src/main/res/values/strings.xml"
def overseasZhFilePtah = "niu-res-overseas/src/main/res/values-zh/strings.xml"
def overseasFilePtah = "niu-res-overseas/src/main/res/values/strings.xml"
doFirst {
//海外版国内直接copy
doStringFormat(filePtah,overseasZhFilePtah)
}
doLast {
//海外版英文版手动copy,自动打开文件
exec {
commandLine "open",overseasZhFilePtah
}
exec {
commandLine "open",overseasFilePtah
}
}
}
ArrayList<File> findStrFile(File file){
ArrayList<File> fileList = new ArrayList<>()
if(!file.exists()){
return fileList
}
if (file.isDirectory()){
File[] fs = file.listFiles()
//values
for (File f:fs){
if (f.isDirectory()){
ArrayList<File> fileArrayList = findStrFile(f)
if (fileArrayList.size()>0){
fileList.addAll(fileArrayList)
}
}else{
if (f.absolutePath.endsWith("strings.xml")){
println("找到文件:"+f.absolutePath)
fileList.add(f)
}
}
}
}
return fileList
}
/**
*
* @param filePtah
* @param outFilePath 输出文件路径
* @return
*/
def doStringFormat(String filePtah,String outFilePath) {
//----------------2020/09/15------------
// "Text_1092_L" = "赞了你的评论";
def fileReader = new FileReader(filePtah)
BufferedReader reader = new BufferedReader(fileReader)
String s
StringBuilder sb = new StringBuilder()
//海外版英文
println("=============>开始转换<==============")
while ((s = reader.readLine()) != null) {
if (s.trim().startsWith("//")) {
//----------------2020/09/14------------
//<--2020/09/14-->
StringBuilder sbTemp = new StringBuilder()
sbTemp.append("<!--")
def replace = s.trim().replace("//", "")
def replace1 = replace.trim().replace("-", "")
sbTemp.append(replace1)
sbTemp.append("-->")
println("成功转换:" + sbTemp.toString())
sb.append(sbTemp + "\n")
} else if (s.trim().startsWith("\"")) {
//"C3_17_Text_01" = "正在向网点确认是否接纳此服务单";
//<string name="C_70_C_16" formatted="false">取消报修</string>
StringBuilder sbTemp = new StringBuilder()
def split = s.split(" = ")
if (split != null && split.length == 2) {
def textValue = split[1].trim()
if (textValue.length() > 3 && textValue.startsWith("\"") && textValue.endsWith("\";")) {
sbTemp.append("<string name=")
sbTemp.append(split[0].trim())
sbTemp.append(">")
//去掉前面的",去掉末尾的";
textValue = textValue.substring(1, textValue.length() - 2)
sbTemp.append(textValue)
sbTemp.append("</string>")
println("成功转换:" + sbTemp.toString())
sb.append(sbTemp + "\n")
} else {
sb.append(s + "\n")
//println("未识别字段 = " + s)
}
} else {
sb.append(s + "\n")
//println("未识别字段 = " + s)
}
} else {
sb.append(s + "\n")
// println("未识别字段 = " + s)
}
}
reader.close()
fileReader.close()
println("=============>转换结束<==============")
println("=============>开始写入<==============")
//重新写入,国内
FileWriter writer = new FileWriter(outFilePath)
BufferedWriter writerBuffer = new BufferedWriter(writer)
writerBuffer.writeLine(sb.toString())
writerBuffer.close()
writer.close()
println("=============>写入成功<==============")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Android
1
https://gitee.com/joeblack/AppExp.git
git@gitee.com:joeblack/AppExp.git
joeblack
AppExp
AppExp
master

搜索帮助