50

Is it possible to translate some strings, but not all, in a separate resource file without Lint complaining about MissingTranslation?

For example: my app's strings are all in res/values/strings.xml. One of the strings is

<string name="postal_code">Postal Code</string>

Since "postal code" is usually called "zip code" in the US, I want to add another resource res/values-en-rUS/strings.xml with contents:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="postal_code">Zip Code</string>
</resources>

However, Lint complains about other strings in values/strings.xml but not in values-en-rUS/strings.xml

I realize you can suppress the warnings by specifying tools:ignore in values/strings.xml. But this is undesirable because the Lint warning is actually useful when translating to another language.

Instead, is it possible to suppress the warning in the values-en-rUS/strings.xml file, as in, telling Lint not to use that file as criteria when looking for missing translations?

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
Steveo
  • 2,238
  • 1
  • 21
  • 34
  • Related/almost a dupe: [Ignoring Android Lint “MissingTranslation” check for partial translations](http://stackoverflow.com/questions/12019282/ignoring-android-lint-missingtranslation-check-for-partial-translations) – blahdiblah Apr 28 '15 at 23:05
  • 1
    Please mark @blahdiblah answer as accepted, because it is actually a correct answer. – Vadim Kotov Jul 26 '19 at 13:06

9 Answers9

71

A nice way to disable MissingTranslations check is to add the option in module specific build.gradle file .

android {

    lintOptions{
        disable 'MissingTranslation'
    }

    //other build tags
}

If the strings are not present in locale specific Strings file, it will take the strings from the default file which generally is strings.xml.

ByteHamster
  • 4,884
  • 9
  • 38
  • 53
Nicks
  • 16,030
  • 8
  • 58
  • 65
  • 2
    Note that this will disable *all* MissingTranslation warning/errors, so you won't get false positives for `en-rUS` intentionally partially overriding `en` defaults, but you also won't get true positives when some other locale is missing a translation. – blahdiblah Dec 06 '17 at 23:46
  • 2
    I definitely found your answer useful, however, it could be that you're interested in ignoring translations in different levels (ignore them all, ignore only strings in a given file or ignore only one string). Take a look here: https://stackoverflow.com/a/53206467/973919 – xarlymg89 Mar 14 '19 at 12:31
41

I found a better solution according to this answer: https://stackoverflow.com/a/13797364/190309

Just add ignore="MissingTranslation" to your string.xml, for example:

<?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>
Community
  • 1
  • 1
anticafe
  • 6,816
  • 9
  • 43
  • 74
  • 6
    The OP specifically asked for a solution that only ignores *some* missing translations. This solution applies to everything in a given file. – Sterling Feb 26 '15 at 21:12
25

Lint supports partial regional translations, but needs to know what language the default strings are. That way, it can distinguish a partial regional translation from missing translations in a different locale.

To specify the locale in values/strings.xml:

<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="en">

Quote from the lint command-line tool, when running 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.)

Source: https://android.googlesource.com/platform/tools/base/+/master/lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/TranslationDetector.java#88

Add the locale specification to your default language file, and you shouldn't get that error for en-rUS, but will still be informed of any other missing translations.

Mr-IDE
  • 7,051
  • 1
  • 53
  • 59
blahdiblah
  • 33,069
  • 21
  • 98
  • 152
  • 2
    your answer make sense but it does not work with me - I have a default strings file "values/strings.xml" and I translate some of the strings to en-rGB I put them in "values-en-rUS/strings.xml" file - I added tools:locale="en" to "values/strings.xml" but still got error that some strings is not translated to "en" – Ahmed Hashem Apr 07 '19 at 13:55
  • 3
    I try this and actually it works. If you run a lint task, after adding tools:locale="en", MissingTranslation warnings won't appear. But in Android Studio 3.5.1, red lines will still be there. So it's probably an Android Studio bug. This answer should be marked as the right answer – michgauz Oct 14 '19 at 11:55
  • 3
    I have created a ticket to Google : https://issuetracker.google.com/issues/142590628 – michgauz Oct 15 '19 at 11:52
  • @michgauz I saw you kept on the google guys to make this fix even when they didn't want to, good on you and thank you! – Matt Jenje Jul 21 '20 at 09:33
16

This seems to not answered yet, so I show you one solution:

In your DEFAULT xml file, you can define strings, that don't need translations like following:

<string name="developer" translatable="false">Developer Name</string>

This string does not need to be translated and lint will not complain about it either...

prom85
  • 16,896
  • 17
  • 122
  • 242
  • This will switch the problem to warnings about translating untranslatable strings in the locales that are supposed to provide a different resource. – blahdiblah Dec 06 '17 at 23:47
16

This is a Swiss knife answer, so you can ignore the missing translations message depending on which situation you are:

  • Ignore all MissingTranslation message:

    Edit your build.gradle file:

    android {
        lintOptions{
            disable 'MissingTranslation'
        }
    }
    
  • Ignore all MissingTranslation messages in a concrete resources.xml file:

    <?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>
    
  • Ignore MissingTranslation message for only one string:

    <string name="developer" translatable="false">Developer Name</string>
    
xarlymg89
  • 2,552
  • 2
  • 27
  • 41
0

If you are using Eclipse, please look at the toolbar buttons of the Lint warnings view. One of them is called "Ignore in file". This should help, but only if Lint assigns the error to your "US" file. That button simply modifies the lint.xml file in your project, so you can investigate (and undo) that easily.

More details about that file specific suppression are at http://tools.android.com/tips/lint/suppressing-lint-warnings

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
0

For the lint configuration file lint.xml:

<issue id="MissingTranslation" severity="ignore" />
sdex
  • 3,169
  • 15
  • 28
0

If case of using gradle 7 :

lint {
        disable("MissingTranslation")
    }
Amir
  • 1,290
  • 1
  • 13
  • 20
0

In case of Android Gradle plugin 7.4.x, and .kts:

android {
  lint {
    disable.add("MissingTranslation")
  }
}
Damiancioo
  • 462
  • 3
  • 13