为什么机器人有它自己的方式来获得当前的语言环境? [英] Why does Android have its own way to get the current locale?

查看:97
本文介绍了为什么机器人有它自己的方式来获得当前的语言环境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面<一个Android的文件href="http://developer.android.com/guide/topics/resources/localization.html">http://developer.android.com/guide/topics/resources/localization.html解释说,你可以得到当前的区域用此方法:

The Android documentation here http://developer.android.com/guide/topics/resources/localization.html explains that you can get the current locale with this method:

context.getResources().getConfiguration().locale

这似乎已经的Java提供了这种方法的形式,这样的信息:

It seems Java already provides this information in the form of this method:

java.util.Locale.getDefault()

那么,为什么在Android开发者推出另一种方式来获得的语言环境?将上述两行的code以往任何时候都产生不同的结果,同时运行并排侧时?

So why did the Android developers introduce another way to get the locale? Would the above two lines of code ever produce different results when run side-by-side at the same time?

推荐答案

我同意这是混乱的,虽然我不是攻击它我也没有保卫它,我可以看到它让你做一些事情。

I agree it is confusing and while I am not attacking it nor am I defending it I can see it allowing you to do some things.

让我们从头开始。

java.util.Locale.getDefault来()是由Java运行时定义。这是手机/设备的区域。这就是权力之类的DateFormats(ISO日期格式标准,美国的格式标准等),NumberFormats(逗号或小数,3或4,等集团)和CurrenyFormats(它看起来像一个$或CAD)时,没有语言环境给出。对于这些情况下,它可能是最好到指定地点对这些类型的对象反正。

java.util.Locale.getDefault() is defined by the java runtime. It is the locale of the phone/device. It is what powers things like DateFormats (ISO Dateformat standard, US format standard, etc), NumberFormats (comma's or decimals, groupings of 3 or 4, etc) and CurrenyFormats (Does it look like a $ or a CAD) when no locale is given. For these cases it's probably best to specify the Locale to these types of objects anyways.

context.getResources()。getConfiguration()语言环境是注册在给定的背景下当前的资源包的语言环境。它可以包含的语言环境值,所有的资源内容将尊重目前的情况下/资源对。的配置可以是这类内容最好过滤到当前资源的设备的当前状态。你不一定需要指定改变基于区域的任何内容,但它是一个选项。

context.getResources().getConfiguration().locale is the locale that is registered with the current resources pack in the given Context. It can include the locale value that all the resource content will respect for the current context/resources pair. The configuration can be sort of like the current state of the device that best filtered to the current resources. You don't necessarily need to specify any content that changes based on locale, but it's an option.

资源使用一组鉴像方向,屏幕宽度,地点等配置,使您的应用程序中,你可能会允许仅通过语言环境的应用程序配置更改为不同的区域覆盖当前资源()与另一个。例如像,你做了一个地址的布局。您可能需要某些字段根据什么选择的国家是变身。 (不是说这是这样一个应用程序,正确的行为,但它想现在最简单的事)。如果你不得不简单地依靠Locale.getDefault(),它将使系统的应用程序和状态有些别扭复位,当你想要做一些事情,我刚刚描述。

Resources use a set of discriminators in the Configuration like orientation, screen width, locale, etc. So within your app you might allow overriding the current Resources() with another by just changing the locale app configuration to a different locale. Like for example, you were making a layout for an address. You might want certain fields to morph depending on what the country selected is. (Not saying this is correct behavior for such an app but it's the simplest thing to think of right now). If you had to simply rely on Locale.getDefault(), it would make for some awkward resetting of system apps and state when you wanted to do something as I just described.

您将基本上需要修改的区域设置为整个设备(这不一定是安全的,也不是任何东西的老用户将享有)。即使不理,将主机VM被复制的安全管理问题;在大多数设备上有很多国家已缓存为国家套件。因此,会有相当的滞后,可能是一个很大的不稳定性,同时切换该值超过(如果有人能做到这一点即是)。另一种选择将是始终指定区域的一切。你可以看到如何讨厌这将是。因此,它的上下文的配置中。

You would essentially be required to modify the Locale for the entire device (this isn't necessarily safe, nor is it something any old user would enjoy). Even if we disregard the security manager issues that would be duplicated by the host vm; on most devices there is a lot of state that is cached for the country kit. So there would be considerable lag and probably a lot of instability while switching this value over (If anyone could do it that is). The other alternative would to be always specify the locale for everything. You could see how annoying that would be. So it's inside the Context's Configuration.

因此​​,尽管它的笨拙,但它提供了自由与保护,以及方便的额外的程度。它的大部分将被Locale.getDefault时间(),因为你的应用程序从一个进程,是基本上下文被初始化为Locale.getDefault()开始。一般来说,我会从使用Locale.getDefault()值设置得太大回避。有没有太多的时间在消费者应用程序开发时,它会是什么,应该是可信的。

So while it's awkward, it does provide an extra degree of freedom and protection, and convenience. Most of the time it will be Locale.getDefault() since your Application was started from a process that's base context was initialized to Locale.getDefault(). In general I would shy away from using the Locale.getDefault() value too much. There aren't too many times in consumer app development when it would be something that should be trusted.

同样不是基于事实,因为我不是一个操作系统的开发,只是大多基于分析系统的优劣。我想,在配置的语言环境是完全合理的。

Again not based on fact since I am not a OS dev, just mostly based on analyzing the pros and cons of the system. I think the locale in the configuration is completely reasonable.

这篇关于为什么机器人有它自己的方式来获得当前的语言环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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