如何正确本地化Android资源以支持多种语言? [英] How to correctly localize Android resources to support multiple languages?

查看:315
本文介绍了如何正确本地化Android资源以支持多种语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究将在不同国家/地区使用的Android应用程序.

因此,我遵循了 Android开发人员指南以支持应用程序的不同语言,并使用Android Studio中的翻译编辑器指定我要翻译的资源.但是,Android Studio预览版会向我显示正确的资源,但在运行应用程序时不会翻译资源,而是将其保留为默认值.我相信我可能已经错过了一些东西,但似乎找不到它.

以下图像显示了Android Studio中的目录和资源字符串:

默认字符串:

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

西班牙语字符串:

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

我通常以自己喜欢的方式引用布局xml中的资源

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

以下图像是当我将其设置为西班牙语语言环境(与上面的资源不同的String资源)时,Android Studio向我显示的内容,以及即使设备语言设置为Español,运行该应用程序时所显示的内容:

解决方案

好的,我在"nl"后,所有内容均按预期显示.

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

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:

解决方案

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 
    }

这篇关于如何正确本地化Android资源以支持多种语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆