Avoid Android Lint complains about not-translated string

坚强是说给别人听的谎言 提交于 2019-11-27 11:23:57
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>

It's the ignore attribute of the tools namespace in your strings file, as follows:

<?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>
Giraffe Lion

Add to build.gradle:

android {
     lintOptions {
        disable 'MissingTranslation'
    }
}

There are 3 ways that I know of :

To apply the modification value by value : Set the attribute translatable="false" on the <string> definition:

<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.

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.

See: Android Tools Project Site: Non-translatable Strings

And here is a Android Studio solution for disabling this fatal Lint error:

I think that what you need instead of disabling lint is to mark them with attribute

translatable="false"

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.

If you want to turn of the lint checking on missing translation, you may go

"Project Properties -> Android Lint Preferences -> Select MissingTranslation -> Swtich To Ignore In Severity",

hopes this helps others cause i just encounter this problem :)

Another way to do that is add your string to gradle file, using resValue or buildConfigField. Something like that:

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)

To avoid warnings in Android Studio, open Preferences ==> Inspections, uncheck "Incomplete translations". But it won't affect Gradle build.

There is also the "Translation editor" (right click on string.xml "Open Translation Editor").

Then check the 'Untranslatable' box on a string.

https://developer.android.com/studio/write/translations-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!