iOS本地化,模拟器和设备始终使用Base.lproj而不是用户首选的语言环境
我正在创建一个应用程序,目前我想以英语和德语提供.我检查了项目配置中的基本本地化标记,并添加了German
.我离开了英语作为开发语言.
I am creating an app that for now I would like to offer in English and German. I checked the Base localization mark in the project configuration and added German
. I left English as the development language.
然后我创建了一个文件Translation.plist
,该文件基本上由我称为类别的字典组成,例如我有一本用于按钮文本,标签文本等的字典.每个类别字典又由包含两个字符串的字典组成:值和注释. Translation.plist通过XCode
进行了本地化.文件夹Base.lproj
,en.lproj
和de.lproj
存在,并且包含预期的plist文件的副本.
Then I created a file Translation.plist
which basically consists of dictionaries that I call categories, e.g. I have one dictionary for button texts, label texts etc. Each of the category dictionaries again consists of dictionaries that contain two Strings: value and comment. Translation.plist is localized via XCode
. The folders Base.lproj
, en.lproj
and de.lproj
exist and contain a copy of the plist-file as expected.
然后,我创建了一个类Translator.swift
,该类应该根据用户的首选语言环境将Translation.plist
文件作为NSDictionary
加载.代码如下:
Then I created a class Translator.swift
that is supposed to load the Translation.plist
file as an NSDictionary
depending on the user's preferred locale. The code looks like this:
func relevantDictionary(category: String) -> NSDictionary {
let preferredLocale = Bundle.main.preferredLocalizations.first ?? "Base"
NSLog("User's preferred locale is \(preferredLocale)")
guard let url = Bundle.main.url(forResource: "Translation", withExtension: "plist") else {
fatalError("Could not find Translation.plist")
}
NSLog("Using \(url.absoluteURL) for translation")
guard let root = NSDictionary(contentsOf: url) else {
fatalError("Could not find dictionary for category (locale=\(preferredLocale)")
}
guard let relevant = root.value(forKey: category) as? NSDictionary else {
fatalError("Could not create dictionary from Translation.plist")
}
return relevant
}
然后,我创建了一个使用扩展程序的String扩展,如下所示:
Then I created a String extension that uses the Translator as follows:
func localize(category: String) -> String {
return Translator.instance.translate(category: category, string: self)
}
通过此操作,我通过"yes" .localize("button")来调用翻译器.在英语中,我期望是",在德语中,我期望"Ja".日志显示以下内容:
With this I call the Translator by "yes".localize("button"). In English I would expect "Yes", in German I would expect "Ja". The log says the following:
2017-07-05 08:45:24.728 myApp [13598:35048360]用户的首选语言环境是de_DE
2017-07-05 08:45:24.728 myApp[13598:35048360] User's preferred locale is de_DE
2017-07-05 08:45:24.728 myApp [13598:35048360]使用file:///Users/me/Library/Developer/CoreSimulator/Devices/A39D3318-943D-4EFE-BB97-5C2218279132/data/Containers /Bundle/Application/4614E696-B52E-4C30-BBE8-3C76F6392413/myApp.app/Base.lproj/Translation.plist进行翻译
2017-07-05 08:45:24.728 myApp[13598:35048360] Using file:///Users/me/Library/Developer/CoreSimulator/Devices/A39D3318-943D-4EFE-BB97-5C2218279132/data/Containers/Bundle/Application/4614E696-B52E-4C30-BBE8-3C76F6392413/myApp.app/Base.lproj/Translation.plist for translation
我想知道为什么会这样以及我错过了什么.我本来希望加载de.lproj/Translation.plist
而不是Base.lproj/Translation.plist
.
I wonder why this happens and what I have missed. I would have expected that de.lproj/Translation.plist
is loaded instead of Base.lproj/Translation.plist
.
我们非常感谢您的帮助.
Any help is highly appreciated.
您可以使用单个.plist文件来实现.您无需为其创建其他.plist文件.
You can do it with single .plist file. You don't need to create different .plist files for it.
Firstly, Add English and German countries with locale in Project -> info
https://i.stack.imgur.com/M4QIY.png
Once, you added countries with locale in Project -> info then add localizable.strings file in your bundle.
https://i.stack.imgur.com/lnjgL.png
At the end, just add country's locale in your language support class.
NSArray* languages = @[@"en", @"de"];`enter code here`
NSString *current = [languages objectAtIndex:0];
[self setLanguage:current];
希望对您有帮助.