如何从Android中的不同语言环境获取字符串? [英] how to get string from different locales in Android?

查看:69
本文介绍了如何从Android中的不同语言环境获取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,无论设备/应用程序的当前语言环境设置如何,我都想在多个语言环境中获取字符串的值.我该怎么做?

So I want to get the value of a String in several locales regardless of the current locale setting of the device/app. How should I do that?

基本上我需要的是一个函数 getString(int id, String locale) 而不是 getString(int id)

Basically what I need is a function getString(int id, String locale) rather than getString(int id)

我怎么能这样做?

谢谢

推荐答案

注意 如果您的最低 API 为 17+,请直接转到此答案的底部.否则,请继续阅读...

NOTE If your minimum API is 17+, go straight to the bottom of this answer. Otherwise, read on...

注意 如果您使用的是 App Bundle,则需要确保禁用语言拆分或动态安装不同的语言.请参阅https://stackoverflow.com/a/51054393.如果您不这样做,它将始终使用回退.

NOTE If you are using App Bundles, you need to make sure you either disable language splitting or install the different language dynamically. See https://stackoverflow.com/a/51054393 for this. If you don't do this, it will always use the fallback.

如果你有不同地区的各种 res 文件夹,你可以这样做:

If you have various res folders for different locales, you can do something like this:

Configuration conf = getResources().getConfiguration();
conf.locale = new Locale("pl");
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Resources resources = new Resources(getAssets(), metrics, conf);
String str = resources.getString(id);

或者,您可以使用@jyotiprakash 指向的方法重新启动您的活动.

Alternatively, you can just restart your activity using the method pointed to by @jyotiprakash.

注意 像这样调用 Resources 构造函数会改变 Android 内部的一些东西.您将必须使用原始语言环境调用构造函数才能使事情恢复原状.

NOTE Calling the Resources constructor like this changes something internally in Android. You will have to invoke the constructor with your original locale to get things back the way they were.

EDIT 从特定语言环境中检索资源的一个略有不同(并且更简洁)的方法是:

EDIT A slightly different (and somewhat cleaner) recipe for retrieving resources from a specific locale is:

Resources res = getResources();
Configuration conf = res.getConfiguration();
Locale savedLocale = conf.locale;
conf.locale = desiredLocale; // whatever you want here
res.updateConfiguration(conf, null); // second arg null means don't change

// retrieve resources from desired locale
String str = res.getString(id);

// restore original locale
conf.locale = savedLocale;
res.updateConfiguration(conf, null);

从 API 级别 17 开始,您应该使用 conf.setLocale() 而不是直接设置 conf.locale.如果您碰巧在从右到左和从左到右的语言环境之间切换,这将正确更新配置的布局方向.(布局方向是在 17 中引入的.)

As of API level 17, you should use conf.setLocale() instead of directly setting conf.locale. This will correctly update the configuration's layout direction if you happen to be switching between right-to-left and left-to-right locales. (Layout direction was introduced in 17.)

创建一个新的 Configuration 对象是没有意义的(正如@Nulano 在评论中建议的那样),因为调用 updateConfiguration 将改变通过调用 获得的原始配置>res.getConfiguration().

There's no point in creating a new Configuration object (as @Nulano suggests in a comment) because calling updateConfiguration is going to change the original configuration obtained by calling res.getConfiguration().

如果您要为一个语言环境加载多个字符串资源,我会犹豫是否将它捆绑到一个 getString(int id, String locale) 方法中.更改语言环境(使用任一方法)需要框架做大量的工作来重新绑定所有资源.最好更新一次语言环境,检索您需要的所有内容,然后重新设置语言环境.

I would hesitate to bundle this up into a getString(int id, String locale) method if you're going to be loading several string resources for a locale. Changing locales (using either recipe) calls for the framework to do a lot of work rebinding all the resources. It's much better to update locales once, retrieve everything you need, and then set the locale back.

编辑(感谢@Mygod):

EDIT (Thanks to @Mygod):

如果您的最低 API 级别为 17+,则有更好的方法,如另一个线程上的此答案所示.例如,您可以创建多个 Resource 对象,您需要的每个区域设置一个,使用:

If your minimum API level is 17+, there's a much better approach, as shown in this answer on another thread. For instance, you can create multiple Resource objects, one for each locale you need, with:

@NonNull Resources getLocalizedResources(Context context, Locale desiredLocale) {
    Configuration conf = context.getResources().getConfiguration();
    conf = new Configuration(conf);
    conf.setLocale(desiredLocale);
    Context localizedContext = context.createConfigurationContext(conf);
    return localizedContext.getResources();
}

然后只需从该方法返回的本地化Resource 对象中检索您喜欢的资源.检索资源后无需重置任何内容.

Then just retrieve the resources you like from the localized Resource object returned by this method. There's no need to reset anything once you've retrieved the resources.

这篇关于如何从Android中的不同语言环境获取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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