Search code examples
androidandroid-studiolocale

How to correctly localize Android resources to support multiple languages?


I've been working on an Android application that is going to be used throughout different countries.

Because of this I have followed the Android Developer Guide to support different languages for an application and used the Translation Editor in Android Studio to specify the resources I want translated. However the preview Android Studio gives me shows the correct resources, they aren't translated and instead kept at the default value when I run the application. I believe I might have missed something and can't seem to find what it is.

The following images show the directories and resource Strings in Android Studio:

Locale directories and resource files Strings in Translation Editor

Default strings:

<resources>
    <string name="welcome_user">Welcome %1$s</string>
    <string name="logout">Logout</string>
    <string name="main_menu">Main menu</string>
    <string name="inventory">Inventory</string>
    <string name="login">Login</string>
</resources>

Spanish strings:

<resources>
    <string name="welcome_user">Bienvenido %1$s</string>
    <string name="logout">Cerrar sesión</string>
    <string name="main_menu">Menú principal</string>
    <string name="inventory">Inventario</string>
    <string name="login">"Iniciar sesión "</string>
</resources>

I refer to the resources in my layout xml like one would normally do like

android:id="@+id/textWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_user"

The following images are what Android Studio shows me when I set it to the Spanish locale (different String resources than the ones above) and what is shown when the application is ran even though the device language is set to Español:

Spanish resources preview

Application


Solution

  • Alright, I found an answer on this post after searching related documentation and questions. The problem wasn't coming from the structuring of my folders or how I retrieved the resources.

    The resConfigs property in my Android Gradle file was set to only support language resources related to English resources. Because of this setting any other language that was not specified got removed.

    After I added "es" and "nl" everything showed up as intended.

    productFlavors {
        dev {
            dimension "default"
            applicationIdSuffix '.dev'
            versionNameSuffix '-dev'
            resConfigs "en", "nl", "es" //The culprit I created and forgot 
        }