避免 Android Lint 抱怨未翻译的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12590739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avoid Android Lint complains about not-translated string
提问by Gianni Costanzi
is it possible to specify that the strings in a file within the value-*
?directories are purposely not translated into other languages? I have a bunch of strings that are common for all the languages and need no translation, so I've created an unlocalized-strings.xml
file within values
directory.. Running Android Lint to check for problems it keeps saying that some translations are missing.. I do not want to disable this check on the whole project, I'd like to disable it only in some XML files.. is it possible?
是否可以指定value-*
?directories内文件中的字符串故意不翻译成其他语言?我有一堆所有语言通用的字符串,不需要翻译,所以我unlocalized-strings.xml
在values
目录中创建了一个文件..运行 Android Lint 来检查问题,它一直说缺少一些翻译..要在整个项目中禁用此检查,我只想在某些 XML 文件中禁用它.. 可能吗?
"title_widget_updater_service" is not translated in de, en, en-rUS, it
Issue: Checks for incomplete translations where not all strings are translated
Id: MissingTranslation
If an application has more than one locale, then all the strings declared in one language
should also be translated in all other languages.
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 require all regions to
provide a full translation by setting the environment variable
ANDROID_LINT_COMPLETE_REGIONS.
How can defined this regionof unlocalizedstrings?
如何定义这个区域的未本地化的字符串?
回答by Adil Hussain
It's the ignore
attribute of the tools
namespace in your strings file, as follows:
它是字符串文件中命名空间的ignore
属性tools
,如下所示:
<?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>
回答by Luis
I don't know how to ignore all the file, but you can do it string by string using:
我不知道如何忽略所有文件,但您可以使用以下方法逐个字符串地进行处理:
<string name="hello" translatable="false">hello</string>
回答by Giraffe Lion
Add to build.gradle
:
添加到build.gradle
:
android {
lintOptions {
disable 'MissingTranslation'
}
}
回答by Cyril Leroux
There are 3 ways that I know of :
我知道有 3 种方法:
To apply the modification value by value : Set the attribute translatable="false"
on the <string>
definition:
要按值应用修改值:translatable="false"
在<string>
定义上设置属性:
<string name="account_setup_imap" translatable="false">IMAP</string>
If you have a lot of resources that should not be translated, you can place them in a file named donottranslate.xml
and lint will consider all of them non-translatable resources.
如果您有很多不应翻译的资源,您可以将它们放在一个名为的文件中donottranslate.xml
,lint 会将它们视为不可翻译的资源。
Another way I discovered while browsing Hackers Keyboard project sources:
You can add the prefix donottranslate-
to your resource file.
As in the previous example, lint will consider all of them non-translatable resources.
In your case, you can replace unlocalized-strings.xml
by donottranslate-strings.xml
.
It seems to work, but I haven't found any documentation for this tip.
我在浏览Hackers Keyboard 项目源时发现的另一种方法:
您可以将前缀添加donottranslate-
到您的资源文件中。与前面的示例一样,lint 将所有这些资源都视为不可翻译的资源。
在您的情况下,您可以替换unlocalized-strings.xml
为donottranslate-strings.xml
. 它似乎有效,但我还没有找到有关此提示的任何文档。
回答by Dzhuneyt
And here is a Android Studio solution for disabling this fatal Lint error:
这是用于禁用此致命 Lint 错误的 Android Studio 解决方案:
回答by Kai
Create a resource file with a file name starting with "donottranslate" (e.g. donottranslate.xml, donottranslate_urls.xml, etc), and lint will ignore its strings altogether when checking for missing translations.
创建一个文件名以“ donottranslate”开头的资源文件(例如donottranslate.xml、donottranslate_urls.xml等),当检查丢失的翻译时,lint 将完全忽略其字符串。
回答by Diego Torres Milano
I think that what you need instead of disabling lint is to mark them with attribute
我认为你需要的而不是禁用 lint 是用属性标记它们
translatable="false"
回答by hctang
If you want to turn of the lint checking on missing translation, you may go
如果你想打开 lint 检查丢失的翻译,你可以去
"Project Properties -> Android Lint Preferences -> Select MissingTranslation -> Swtich To Ignore In Severity",
"项目属性 -> Android Lint 首选项 -> 选择 MissingTranslation -> Swtich To Ignore In Severity",
hopes this helps others cause i just encounter this problem :)
希望这对其他人有帮助,因为我刚遇到这个问题:)
回答by heloisasim
Another way to do that is add your string to gradle file, using resValue
or buildConfigField
. Something like that:
另一种方法是将您的字符串添加到 gradle 文件中,使用resValue
或buildConfigField
。类似的东西:
buildTypes {
debug {
buildConfigField "string", "app_name1", "App Name"
resValue "string", "app_name2", "App Name"
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField "string", "app_name1", "App Name"
resValue "string", "app_name2", "App Name"
}
}
Usages:
用法:
// buildConfigField
BuildConfig.APP_NAME1
// resValue
getString(R.string.app_name2)
回答by user1010160
There is also the "Translation editor" (right click on string.xml "Open Translation Editor").
还有“翻译编辑器”(右键单击 string.xml“打开翻译编辑器”)。
Then check the 'Untranslatable' box on a string.
然后选中字符串上的“不可翻译”框。
https://developer.android.com/studio/write/translations-editor
https://developer.android.com/studio/write/translations-editor