Cannot set the default language in Android Studio
Asked Answered
B

1

10

I am getting warnings in Android Studio about untranslated strings - it is telling me they have not been translated to English. This appears to be because I have an "en-rGB" resource folder. But I have tried to follow the instructions in lint to specify the default language, but this has not worked.

I have the following strings.xml files:

values/strings.xml:

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

    <string name="string1">Howdy</string>
    <string name="string2">Howdy</string>
</resources>

values-en-rGB/strings.xml:

<resources>

    <string name="string1">Hello</string>
</resources>

values-fr/strings.xml:

<resources>

    <string name="string1">Bonjour</string>
    <string name="string2">Bonjour</string>
</resources>

values-fr-rCA/strings.xml:

<resources>

    <string name="string2">Bonjour</string>
</resources>

Expected result: There should be no warnings, both strings are translated to both English and French

Actual result: Lint will warn about string2, saying it is not translated to English.

Note, I have included fr-rCA to show that it is not a regional thing. It is happy that as long as it is translated to the "unregioned" french, it does not also need to be translated to French Canadian. In this instance, it will not warn about string1, even though it's not been translated to French Canadian, because it's already been translated to French.

I cannot change the main resource folder to English (values-en), as that will result in errors that there is no default resources. Which is true.

I do not want to suppress the warning in general, because I am planning to add other languages and I DO want the warning to appear for strings which are not translated into other languages that I have included.

What I am after is the correct way to inform lint of the default language. I have done it as per the instructions for the "Imcomplete translation" warning:

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 element in your resource file. (The tools prefix refers to the namespace declaration http://schemas.android.com/tools.)

But this does not seem to work. Am I doing it wrong, or is this an issue with lint?

Bekha answered 17/12, 2019 at 13:31 Comment(8)
It is strange that you have 2 default strings but only one of each string is translated to another region language. I guess the error is thrown by not having string2 in your en-rGB values added.Gothurd
I second what @Gothurd said. Just copy the default string2 to en-rGB since they'll end up the same, this should make your warning go. The best way I'd go about translations is that all strings.xml files should have all entries as the default values/strings.xml, regardless if the value does need translation or not.Accustom
@Accustom Just copy the default string2 to en-rGB since they'll end up the same that's only easy here because it's only 1 string and only 1 other language that needs it. Imagine 100 strings and 20 languagesTade
imho, I'd still do that by using a diff tool. Since eventually all the entries need to be existing in all of the string files, I'd sort the entries by name (not value) in all of the files and then with a diff tool, have both the default file and the language specific file open to find missing entries and copy them over with the default values. One could also write a small piece of code to do so but I would consider this as a one time only and hence I'd do it using an off-the-shelf diff tool like WinMerge or othersAccustom
all the entries need to be existing in all of the string files only the default file needs to contain all stringsTade
@Tade I agree, but based on the OP's description the lint doesn't seem to have that kind logic, that's the problem!Accustom
The lint tool does have that logic - just not for the default language. It is quite happy that fr-rCA does not have all the strings definedBekha
#23839146enter link description hereWingless
L
8

It's a bug: https://issuetracker.google.com/issues/142590628

I have a hotfix for you: Auto copy values/strings.xml into values-en folder

1. Create values-en folder

2. Paste it into app/build.gradle

task copyEnResource {
    copy {
        from ('src/main/res/values/strings.xml')
        into ('src/main/res/values-en/')
    }
}

preBuild.dependsOn copyEnResource

Demo:

Translation Android studio issues

Limpopo answered 25/12, 2019 at 10:3 Comment(1)
Thanks, I'll give that a go. It's a bit clunky but as long as it's automated I guess it shouldn't be too much of a maintainability issueBekha

© 2022 - 2024 — McMap. All rights reserved.