易语言修改区域和语言
Hi everybody, I’m Riccardo. Senior iOS Engineer at Bending Spoons, I breathe iOS development, both apps and tools and I love to share my knowledge with others.
大家好,我是Riccardo。 Bending Spoons的高级iOS工程师,我负责iOS开发,包括应用程序和工具,我喜欢与他人分享我的知识。
The Locale
object in Swift is used to retrieve some pieces of information on the Locale
settings of the user. These settings can depend on the general set up of the device or on the settings for a specific app.
Swift中的Locale
对象用于检索有关用户Locale
设置的一些信息。 这些设置可以取决于设备的常规设置或特定应用程序的设置。
The Locale
has some subtleties that are worth exploring, especially when it comes to the language. It offers two different ways to retrieve languages:
Locale
有一些细微之处值得探讨,尤其是在语言方面。 它提供了两种不同的检索语言的方式:
Locale.autoupdatingcurrent.languageCode
to retrieve the current language used by the app.Locale.autoupdatingcurrent.languageCode
检索应用程序使用的当前语言。Locale.preferredLanguages
to retrieve all the languages set up by the user.Locale.preferredLanguages
检索用户设置的所有语言。
Last week I was doing some experiments on these fields for one of our features… And that’s what I found!
上周,我在这些领域中针对我们的功能之一进行了一些实验……这就是我所发现的!
如果应用程序未本地化,则`languageCode`始终返回应用程序的基本语言。 (If the app is not localized, `languageCode` always returns the app’s base language.)
Yes, you read right. If your app does not support localization, every time that you ask the Locale.autoupdatingcurrent.languageCode
you will always get the default development language of your app (that, for us, is en
).
是的,您没看错。 如果您的应用程序不支持本地化,则每次您询问Locale.autoupdatingcurrent.languageCode
您将始终获得应用程序的默认开发语言(对我们来说是en
)。
You can check what is your base language by clicking on the project in the Project Navigator, selecting the project, and scrolling to the localization section.
您可以通过在项目导航器中单击项目 ,选择项目并滚动到本地化部分来检查您的基本语言。
如果应用支持本地化并且未更改任何设置,则` languageCode
返回设备语言 (If the app support localization and no settings are changed, `languageCode
`returns the device language)
Let’s suppose that the app is localized in English and in Italian and your users’ device language is Italian, the Locale.autoupdatingcurrent.languageCode
will return it
as language code.
假设该应用程序已以英语和意大利语进行了本地化,并且您的用户的设备语言为意大利语,则Locale.autoupdatingcurrent.languageCode
会将it
作为语言代码返回。
However, if the app supports localization, but the device is in a language not supported (let’s say Spanish, in the example above) the default development language is returned ( en
in the example, even if the app supports both English and Italian).
但是,如果应用程序支持本地化,但设备使用的语言不受支持(例如,在上面的示例中为西班牙语),则返回默认的开发语言(即使应用程序同时支持英语和意大利语,在示例中也为en
)。
如果应用支持本地化并且用户为该单个应用选择了支持的语言,则`languageCode`将返回用户选择。 (If the app support localization and the user choose a supported language for that single app, `languageCode` returns the user choice.)
Starting from iOS 13, users can choose a different language for each of their apps. They can have their device in English, but they can decide to have a specific app in another language.
从iOS 13开始,用户可以为每个应用程序选择不同的语言。 他们可以使用英语来安装设备,但是可以决定使用另一种语言来安装特定的应用。
This can be done by navigating to the device Settings, select an app, select language, and change the chosen language.
可以通过导航至设备设置,选择应用程序,选择语言并更改选择的语言来完成。
更改设备默认语言会重置所有特定于应用程序的语言。 (Changing the device default language resets all the app-specific languages.)
Imagine that you spent half an hour choosing the right language for all your apps and then you decide you want your phone in Spanish… Puff!!
想象一下,您花了半个小时为所有应用程序选择正确的语言,然后您决定要使用西班牙语的手机…… 吹!
All your single app configurations disappear, reset to the new default language chosen.
您所有的单个应用程序配置都会消失,并重置为所选的新默认语言。
静态` preferredLanguage
返回用户选择的语言列表。 (The static `preferredLanguage
` returns the list of user’s chosen languages.)
When you change the default language of your phone, the iOS settings page keeps the list of all the languages you have ever added. Whenever you change the order of the language, the system resets. Notice that the first language of the list is the one used by your device as the default language.
当您更改手机的默认语言时,iOS设置页面会保留您曾经添加的所有语言的列表。 每当您更改语言顺序时,系统都会重置。 请注意,列表的第一种语言是设备使用的默认语言。
The Locale.preferredLanguages
returns an array that mimics the list of languages in that settings page.
Locale.preferredLanguages
返回一个数组,该数组模仿该设置页面中的语言列表。
结论 (Conclusion)
The two language API gives us slightly different information. The Locale.autoupdatingcurrent.languageCode
returns the language chosen for the specific app, that is compatible with the localization information. This is useful to provide content in the same language of the app’s one, for example.
两种语言的API给我们的信息略有不同。 Locale.autoupdatingcurrent.languageCode
返回为特定应用程序选择的语言,该语言与本地化信息兼容。 例如,这对于以与应用程序相同的语言提供内容很有用。
The Locale.preferredLanguages[0]
returns the language of the device.
Locale.preferredLanguages[0]
返回设备的语言。
翻译自: https://medium.com/swlh/know-your-language-locale-in-swift-beae4fcc5174
易语言修改区域和语言