gpt4 book ai didi

java - 如果 strings.xml 中存在未转义的撇号,如何使构建失败?

转载 作者:行者123 更新时间:2023-12-02 09:01:32 24 4
gpt4 key购买 nike

我有一个 Android 应用程序,对于这个应用程序,我自动从 crowdin.com 下载翻译 - 这效果很好。

某些语言使用撇号,众所周知,如果您的 strings.xml 中有撇号,则 you should escape them

我想要做的是如果我的各种 strings.xml 中存在未转义的撇号,则构建失败。

具体来说,我想搜索 src/main/res/ 中的所有 strings.xml如果任何文件包含 '前面没有 \ ,然后抛出错误。这对我来说是理想的选择,因为我可以了解它,并与译者一起在源头纠正翻译。

或者,我希望替换未转义的撇号,但以自动方式作为构建本身的一部分。

最佳答案

这是一个在“preBuild”之前执行的 gradle 任务的简单示例,该任务检查它找到的所有 strings.xml 中是否带有未转义撇号的字符串值。如果发现错误,则构建失败。根据下载任务的需要更改依赖项。

(请注意,脚本在遇到第一个错误时失败 - 因此,如果 en 版本和 es 版本中同时存在错误,则只有一个文件被报告为错误。)

(注2,因为使用了 XML 解析器,所以它只处理字符串标记值 - 因此也忽略 xml 注释中的任何违规 - 这在第一个没有错误的测试用例中很明显。)

(在模块中:app (build.gradle))

task checkUnescapedApostrophes {
doFirst {

println("checkUnescapedApostrophes")

fileTree("src").matching {
include "**/strings.xml"
}.each {
def stringsFile = it
def parser = (new XmlParser()).parse(stringsFile)
println("Processing file: "+stringsFile)
parser.'string'.each { m ->
def s = m.text()
def ss = "[^\\\\]\'"
println "[" + m.@name + "]: " + s
if (s =~ ss) {
throw new GradleException(
"Found a string value in " + stringsFile +
" have unescaped apostrophe: "+s)
}
}
}
println("strings.xml OK")
}
}

preBuild.dependsOn(checkUnescapedApostrophes)

在 strings.xml 没有错误的情况下(默认语言环境 + es):

<resources>
<!-- <string name="test">some value with '</string> -->
<string name="test2">some with escaped \'</string>
</resources>

<resources>
<!-- <string name="test">algún valor con apóstrofe sin escape '</string> -->
<string name="test2">algún valor con el apóstrofe escapado \'</string>
</resources>

构建输出:

> Configure project :app
checkUnescapedApostrophes
Processing file: ...\app\src\main\res\values\strings.xml
[test2]: some with escaped \'
Processing file: ....\app\src\main\res\values-es\strings.xml
[test2]: algún error con el apóstrofe escapado \'
strings.xml OK

> Task :app:checkUnescapedApostrophes UP-TO-DATE
> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :app:compileDebugAidl NO-SOURCE
> Task :app:compileDebugRenderscript NO-SOURCE
> Task :app:checkDebugManifest UP-TO-DATE
> Task :app:generateDebugBuildConfig UP-TO-DATE
> Task :app:prepareLintJar UP-TO-DATE
> Task :app:prepareLintJarForPublish UP-TO-DATE
> Task :app:generateDebugSources UP-TO-DATE

BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 up-to-date

在 strings.xml 错误的情况下(默认本地和 es)

<resources>
<string name="test">some value with '</string>
<string name="test2">some with escaped \'</string>
</resources>

<resources>
<string name="test">algún valor con apóstrofe sin escape '</string>
<string name="test2">algún valor con el apóstrofe escapado \'</string>
</resources>

(请注意,在上述情况下,未转义的撇号在工作室中被标记为红色并出现错误 - 正如预期的那样。)

构建输出:

> Configure project :app
checkUnescapedApostrophes
Processing file: ...\app\src\main\res\values\strings.xml
[test]: some value with '

FAILURE: Build failed with an exception.

* Where:
Build file '...\app\build.gradle' line: 43

* What went wrong:
A problem occurred evaluating project ':app'.
> Found a string value in ...\app\src\main\res\values\strings.xml have unescaped apostrophe: some value with '

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

一般 gradle 注意:在自己测试并更改 build.gradle 时 - 请务必“重建项目”而不仅仅是“构建项目”,因为在之前构建的情况下,似乎不会拾取任何 gradle 更改失败。

<小时/>

另外,作为您提到的替代方案,此任务将通过替换找到的所有 strings.xml 来转义未转义的撇号。这是就地更新 - 这里有一个更强大的方法:https://stackoverflow.com/a/48356111/2711811 .

task replaceUnescapedApostrophes {
doFirst {
ant.replaceregexp(match: "([^\\\\])'", replace: "\\1\\\\\\\\'", byline: "true") {
fileset(dir: 'src/', excludes: '*', includes: '**/*strings.xml')
}
}
}

//preBuild.dependsOn(checkUnescapedApostrophes)
preBuild.dependsOn(replaceUnescapedApostrophes)

关于java - 如果 strings.xml 中存在未转义的撇号,如何使构建失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60125988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com