How do I stop Android Lint complaining about not-translated strings?
Asked Answered
H

11

129

Is it possible to specify that the strings in a file within the values directoriy are purposely not translated into other languages?

I have a bunch of strings that are common for all the languages and need no translation. I've created an unlocalized-strings.xml file within the values directory. When I run 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 this possible?

This is the error that I am encountering:

"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.
Hebdomad answered 25/9, 2012 at 20:44 Comment(1)
C
165

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>
Csch answered 25/9, 2012 at 23:54 Comment(5)
this gives 'non translatable resources should only be defined in the base values /folder.Caruso
And that's correct. If you don't want a string to be translated, you should only define it in the "base values/folder", which means do not enter the string in the translated values/folder.Csch
@CyrilLeroux answer is more comprehensive and should be marked as the correct one because it shows the option of putting all the non translatable strings in one file.Wu
This works but I don't think is the best way to solve the problem by marking an actually translatable string "not translatable" just to suppress the error message. We just choose to not translate some strings for the time being. But that doesn't mean they are not translatable.Greenling
I want to put string in only one language and that's German(de) language(not the default English) one. I put it there and when I try to release build it gave me error. What to do in this case?Novel
C
239

Add the tools:ignore attribute with a MissingTranslation value in your unlocalized-strings.xml 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>
Cowbird answered 10/12, 2012 at 8:32 Comment(4)
<resources tools:ignore="MissingTranslation"> is giving me Error Parsing XML: unbound prefixCaruso
This works good but I had to go to Project->Clean to remove the errors definitively.Moulder
At least eclipse didn't coerce us to translate everything, including odd string folders like ids.xml and analytics.xml.Jone
This should be accepted answer for resolving all issues in just 2 lines of code.Veator
C
165

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>
Csch answered 25/9, 2012 at 23:54 Comment(5)
this gives 'non translatable resources should only be defined in the base values /folder.Caruso
And that's correct. If you don't want a string to be translated, you should only define it in the "base values/folder", which means do not enter the string in the translated values/folder.Csch
@CyrilLeroux answer is more comprehensive and should be marked as the correct one because it shows the option of putting all the non translatable strings in one file.Wu
This works but I don't think is the best way to solve the problem by marking an actually translatable string "not translatable" just to suppress the error message. We just choose to not translate some strings for the time being. But that doesn't mean they are not translatable.Greenling
I want to put string in only one language and that's German(de) language(not the default English) one. I put it there and when I try to release build it gave me error. What to do in this case?Novel
T
154

Add to build.gradle:

android {
     lintOptions {
        disable 'MissingTranslation'
    }
}
Tonneau answered 13/10, 2015 at 9:9 Comment(4)
The only workable solution for dealing with the auto-generated string resources.Thanks.Lermontov
no it shouldn't, based on the question: "I do not want to disable this check on the whole project"Wu
@Giraffe Lion you should post this as a different question and answer it yourself, Thanks for the solution, previously i had to disable it manually.Sixteenmo
This disabled the whole check which should be avoided if possible. I cannot understand why it has this many votes.Armyn
B
23

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

Bismuthic answered 5/5, 2014 at 10:21 Comment(2)
Is it true that if we put our string in the file donottranlate.xml, we don't need to add the translatable="false" tag any more?Mascia
Yep. I use donottranslate-strings.xml files in all my project. Works fine. Try it, it's very usefull !Bismuthic
D
13

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

enter image description here

Doornail answered 24/9, 2014 at 23:57 Comment(1)
It may an annoying error, but it could save you when you accidentally forget to translate a new string to one of your app languages.Stagestruck
P
2

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

translatable="false"
Parsec answered 25/9, 2012 at 21:26 Comment(0)
K
2

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.

Katleen answered 1/7, 2015 at 14:2 Comment(0)
P
1

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 :)

Paynim answered 29/4, 2014 at 9:51 Comment(1)
Is there a way to do it in Android Studio? I couldnt find settings related to lintIncorporation
M
1

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)
Mendie answered 17/5, 2016 at 23:31 Comment(0)
G
0

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

Gargoyle answered 10/10, 2014 at 18:21 Comment(0)
E
0

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

Egis answered 6/3, 2019 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.