How to correctly localize Android resources to support multiple languages?

橙三吉。 提交于 2019-12-24 07:49:32

问题


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:

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:


回答1:


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 
    }


来源:https://stackoverflow.com/questions/52292137/how-to-correctly-localize-android-resources-to-support-multiple-languages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!