私はAndroidアプリを持っています。このアプリの場合、crowdin.comから翻訳を自動的にダウンロードします-これはうまくいきます。
一部の言語ではアポストロフィを使用しており、strings.xmlにアポストロフィがある場合はエスケープする必要があることがわかっています。
私が探しているのは、さまざまなstrings.xmlにエスケープされていないアポストロフィがあるとビルドが失敗することです。
具体的には、内のすべてのstrings.xmlを検索し、先頭にが付いていないsrc/main/res/
ファイルが含まれている場合は、エラーをスローします。私はそれについて知ることができ、翻訳者と一緒にソースで翻訳を修正できるので、これは私にとって理想的です。'
\
あるいは、エスケープされていないアポストロフィを置き換えることを探していますが、ビルド自体の一部として自動化された方法で行います。
これは、「preBuild」の前に実行されるgradleタスクの簡単な例で、エスケープされていないアポストロフィを含む文字列値を見つけるために、見つかったすべてのstrings.xmlをチェックします。エラーが見つかった場合、ビルドは失敗します。ダウンロードタスクの必要に応じて依存関係を変更します。
(スクリプトは最初に発生したエラーで失敗することに注意してください。enバージョンとesバージョンの両方にエラーがある場合、1つのファイルのみがエラーとして報告されます。)
(注2 XMLパーサーが使用されるため、文字列タグ付きの値のみが処理され、結果としてxmlコメントの違反も無視されます。これは、エラーのない最初のテストケースで明らかです。)
(モジュール:アプリ(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を変更する場合は、「Build Project」だけでなく、「Rebuild Project」を使用してください。以前にビルドが失敗した場合、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)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加