Android Lint: how to ignore missing translation warnings in a regional locale string file that purposely only overrides some default translations?

前端 未结 6 829
南方客
南方客 2020-12-24 11:34

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

For example: m

相关标签:
6条回答
  • 2020-12-24 11:38

    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>
      
    0 讨论(0)
  • 2020-12-24 11:43

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

    0 讨论(0)
  • 2020-12-24 11:44

    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>
    
    0 讨论(0)
  • 2020-12-24 11:48

    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.

    0 讨论(0)
  • 2020-12-24 11:53

    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.

    0 讨论(0)
  • 2020-12-24 12:00

    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

    0 讨论(0)
提交回复
热议问题