是否有可能在一个单独的资源文件中翻译一些字符串,但不是全部,而 Lint 不会抱怨 MissingTranslation?
例如:我的app的字符串都在res/values/strings.xml中。其中一个字符串是
<string name="postal_code">Postal Code</string>
由于“邮政编码”在美国通常被称为“邮政编码”,我想添加另一个资源res/values-en-rUS/strings.xml 内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="postal_code">Zip Code</string>
</resources>
但是,Lint 抱怨 values/strings.xml 中的其他字符串,但不在 values-en-rUS/strings.xml
中我意识到您可以通过在
values/strings.xml中指定
tools:ignore
来抑制警告。但这是不可取的,因为在翻译成另一种语言时,Lint 警告实际上很有用。
相反,是否可以抑制 values-en-rUS/strings.xml 文件中的警告,例如,告诉 Lint 在查找丢失的翻译时不要使用该文件作为标准?
禁用 MissingTranslations 检查的一个好方法是在模块特定的
build.gradle
文件中添加选项。
android {
lintOptions{
disable 'MissingTranslation'
}
//other build tags
}
如果字符串不存在于特定于语言环境的字符串文件中,它将从默认文件中获取字符串,通常是
strings.xml
.
我根据这个答案找到了更好的解决方案:https://stackoverflow.com/a/13797364/190309
只需将
ignore="MissingTranslation"
添加到您的string.xml
,例如:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation" >
<!-- your strings here; no need now for the translatable attribute -->
</resources>
Lint 支持部分区域翻译,但需要知道默认字符串是什么语言。这样,它可以区分部分区域翻译和不同语言环境中缺失的翻译。
要在
values/strings.xml
中指定语言环境:
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en">
引用 lint 命令行工具,运行时
lint --show
:
By default this detector allows regions of a language to just provide a
subset of the strings and fall back to the standard language strings. [...]
You can tell lint (and other tools) which language is the default language
in your res/values/ folder by specifying tools:locale="languageCode" for
the root <resources> element in your resource file. (The tools prefix
refers to the namespace declaration http://schemas.android.com/tools.)
将语言环境规范添加到您的默认语言文件中,您不应该收到
en-rUS
的错误,但仍会收到任何其他缺失翻译的通知。
这似乎还没有答案,所以我告诉你一个解决方案:
在您的 DEFAULT xml 文件中,您可以定义不需要翻译的字符串,如下所示:
<string name="developer" translatable="false">Developer Name</string>
这个字符串不需要翻译,lint 也不会报错...
这是瑞士刀式的答案,因此您可以根据自己的情况忽略缺少的翻译消息:
忽略所有
MissingTranslation
消息:
编辑您的
build.gradle
文件:
android {
lintOptions{
disable 'MissingTranslation'
}
}
忽略具体
MissingTranslation
文件中的所有resources.xml
消息:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation">
<!-- your strings here; no need now for the translatable attribute -->
</resources>
仅忽略一个字符串的
MissingTranslation
消息:
<string name="developer" translatable="false">Developer Name</string>
如果您使用的是 Eclipse,请查看 Lint 警告视图的工具栏按钮。其中之一称为“忽略文件”。这应该有所帮助,但前提是 Lint 将错误分配给您的“美国”文件。该按钮只是修改项目中的 lint.xml 文件,因此您可以轻松地进行调查(和撤消)。
有关该文件特定抑制的更多详细信息,请访问http://tools.android.com/tips/lint/suppressing-lint-warnings
对于lint配置文件
lint.xml
:
<issue id="MissingTranslation" severity="ignore" />
如果使用gradle 7的情况:
lint {
disable("MissingTranslation")
}
如果是 Android Gradle 插件 7.4.x 和 .kts:
android {
lint {
disable.add("MissingTranslation")
}
}