Android的获取设备的语言环境 [英] Android get device locale

查看:262
本文介绍了Android的获取设备的语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在安装我的Andr​​oid程序我检查设备的语言环境:

Upon installation of my Android program I check for device locale:

String deviceLocale=Locale.getDefault().getLanguage();

如果deviceLocale是我支持的语言里面(英语,法语,德语)我不会改变语言环境。

If deviceLocale is inside my supported languages (english, french, german) I don't change locale.

但是,例如说,如果我不支持设备的语言:例如西班牙
我把当前的区域设置为英语,因为大部分人能听懂英语部分。

But for instance say that if I don't support device's language: for example spanish.
I set current locale to English, because most of the people can understand English partly.

Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
pContext.getResources().updateConfiguration(config, null);

但在那之后,在其他方法,当我检查设备的语言环境是这样的:

But after that, in other methods, when I check device's locale like this:

String deviceLocale=Locale.getDefault().getLanguage();

我得到的结果为英语。但是,设备的区域设置为西班牙语。

I get result as "English". But device's locale is Spanish.

是否使用getLanguage()给出了当前应用程序的语言环境?
我怎样才能获得设备的区域设置?

Does getLanguage() gives current app's locale ?
How can I get device's locale ?

编辑:在应用程序的第一次运行,它是一个选项,保存设备的语言环境。但是,如果用户更改设备区域我省后,我不知道该设备的新的语言环境。我只知道,我保存在应用程序安装的语言环境。

In app's first run, it is an option to save device locale. But if user changes device locale after I save, I can't know new locale of the device. I can only know the locale that I saved in app install.

推荐答案

还有一个更简单和更清洁的方式比使用反射或分析一些系统属性来做到这一点。

There's a much simpler and cleaner way to do this than using reflection or parsing some system property.

当你设置了默认的语言环境中您的应用程序,你没有访问到系统默认的区域设置更多的注意到了一次。仅在您的应用程序的生命周期,你在你的应用程序中设置默认的语言环境是有效的。无论你在Locale.setDefault设置(区域)走了,一旦这个过程被终止。当应用程序重新启动,你将再次能够读取系统默认语言环境。

As you noticed once you set the default Locale in your app you don't have access to the system default Locale any more. The default Locale you set in your app is valid only during the life cycle of your app. Whatever you set in your Locale.setDefault(Locale) is gone once the process is terminated . When the app is restarted you will again be able to read the system default Locale.

因此​​,所有你需要做的是检索系统默认区域设置时,应用程序启动时,只要记住它的应用程序运行和更新如果用户决定改变在Android设置的语言。因为我们需要它的资料片仅仅只要应用程序运行,我们可以简单地将其存储在一个静态变量,它可从您的应用程序的任何地方。

So all you need to do is retrieve the system default Locale when the app starts, remember it as long as the app runs and update it should the user decide to change the language in the Android settings. Because we need that piece of information only as long as the app runs, we can simply store it in a static variable, which is accessible from anywhere in your app.

这是我们如何做到这一点:

And here's how we do it:

public class MyApplication extends Application {

    public static String sDefSystemLanguage;

    @Override
    public void onCreate() {
        super.onCreate();

        sDefSystemLanguage = Locale.getDefault().getLanguage();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        sDefSystemLanguage = newConfig.locale.getLanguage();
    }

}

使用的应用程序(不要忘了在你的清单中定义它),我们得到默认的语言环境时,应用程序启动(的onCreate()),我们更新,当用户改变在Android设置的语言(onConfigurationChanged(配置))。这是所有有。 当你需要知道什么是系统默认语言是你用你的setDefault调用之前,sDefSystemLanguage会给你答案。

Using an Application (don't forget to define it in your manifest) we get the default locale when the app starts (onCreate()) and we update it when the user changes the language in the Android settings (onConfigurationChanged(Configuration)). That's all there is. Whenever you need to know what the system default language was before you used your setDefault call, sDefSystemLanguage will give you the answer.

还有一个问题,我看到你的code。当您设置新的语言环境,你做的:

There's one issue I spotted in your code. When you set the new Locale you do:

Configuration config = new Configuration();
config.locale = locale;
pContext.getResources().updateConfiguration(config, null);

当你这样做,你覆盖,你可能希望将所有的配置信息。例如。 Configuration.fontScale反映了辅助功能设置大文本。通过设置的语言,失去所有其他配置您的应用程序不会反映大文本设置更多,这意味着文本小于它应该是(如果用户启用了大文本设置)。 做正确的方法是:

When you do that, you overwrite all Configuration information that you might want to keep. E.g. Configuration.fontScale reflects the Accessibility settings Large Text. By settings the language and losing all other Configuration your app would not reflect the Large Text settings any more, meaning text is smaller than it should be (if the user has enabled the large text setting). The correct way to do it is:

Resources res = pContext.getResources();
Configuration config = res.getConfiguration();
config.locale = locale;
res.updateConfiguration(config, null);

而不是创建一个新的配置对象

所以,我们刚刚更新当前之一。

So instead of creating a new Configuration object we just update the current one.

这篇关于Android的获取设备的语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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