Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set the default language in Android Studio

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?

like image 852
richjhart Avatar asked Dec 17 '19 13:12

richjhart


People also ask

How to change the app language programmatically Android?

This example demonstrates how do I in change the app language programmatically android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java

How to add other languages in Android?

So, similarly, for other languages, you need to add their strings.xml file. So, basically Android loads the resources related to language based on the system locale setting. For example, if the language of your Android phone is English, then strings.xml of English will be loaded by Android.

How do I change the compiler settings in Android Studio?

Click File > Settings (on macOS, Android Studio > Preferences) to open the Settings dialog. In the left pane, expand Build, Execution, Deployment and then click Compiler.

Why don't Android apps support more languages?

Browser apps can avoid offering to translate pages in a language the user already knows, and keyboard apps can auto-enable all appropriate layouts. Up through Android 6.0 (API level 23), Android supported only one or two locales for many common languages (en, es, ar, fr, ru).


1 Answers

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

like image 86
duongdt3 Avatar answered Oct 22 '22 12:10

duongdt3