简体   繁体   English

如何将本地化的plist读取为Dictionary?

[英]How to read a localized plist as Dictionary?

How can I read a localised plist as dictionary? 我如何阅读本地化的plist作为字典? This felt promising … 感觉很有希望...

if let path = NSBundle.mainBundle().pathForResource("LocalizedDictionary", ofType: "plist", inDirectory: ???, forLocalization: "en") {
        localizedDictionary = NSDictionary(contentsOfFile: path)
        return localizedDictionary
}

But I apparently I had issues getting right path for the inDirectory parameter. 但显然我在为inDirectory参数获取正确路径时遇到了问题。 Is this the right approach? 这是正确的方法吗?

How do I get the localised version of a plist file as Dictionary? 如何将plist文件的本地化版本作为Dictionary?

Note: I am not using NSLocalizedString as I had issues without switching languages while the app is running. 注意:我没有使用NSLocalizedString,因为在应用程序运行时遇到了一些问题,没有切换语言。

Here's something that should help. 这应该有所帮助。

Create a NSDictionary class extension like this. 像这样创建一个NSDictionary类扩展。 It can be tweaked and adapted to your needs ( to load 'strings' dictionaries for exemple, or passing the locale in a parameter… ) 可以对其进行调整并适应您的需要(例如加载“字符串”字典,或在参数中传递语言环境……)

@implementation NSDictionary (NSDictionary_Extras)

+(NSDictionary*)dictionaryWithName:(NSString*)name
{
    NSDictionary    *dict = NULL;
    NSURL           *dictURL = [[NSBundle mainBundle] URLForResource:name withExtension:@"plist"];

    if (dictURL) {
        dict = [NSDictionary dictionaryWithContentsOfURL:dictURL];
    }

    return dict;
}

+(NSDictionary*)localizedDictionaryWithName:(NSString*)name
{
    NSDictionary    *localizedDict = NULL;
    NSString        *localizationName = [[NSLocale preferredLanguages] objectAtIndex:0];
    NSURL           *localizedDictURL = [[NSBundle mainBundle] URLForResource:name withExtension:@"plist" subdirectory:[NSString stringWithFormat:@"%@.lproj",localizationName]];
    // If resource doe not exist, we try at root ( resource must have a 'base' localization, or not being localized )
    if (!localizedDictURL) {
        localizedDictURL = [[NSBundle mainBundle] URLForResource:name withExtension:@"plist"];
    }

    if (localizedDictURL) {
        localizedDict = [NSDictionary dictionaryWithContentsOfURL:localizedDictURL];
    } 

    return localizedDict;
}

@end

Then you simply load a plist using : 然后,您只需使用以下命令加载plist:

NSDictionary* myDict = [NSDictionary dictionaryWithName:@"myDict"]

Or a localized plist with 或一个本地化的plist

NSDictionary* myDict = [NSDictionary localizedDictionaryWithName:@"myDict"].

I import this extra in all my projects. 我在所有项目中都导入了此额外内容。

You don't need to change any code. 您无需更改任何代码。

xCode's resource localisation is neat and can frequently be completely transparent to you. xCode的资源本地化很整洁,并且经常对您完全透明。 You just ask the main bundle for resource urls as normal and load from them. 您只需要向主捆绑包索取正常的资源网址,然后从它们中加载即可。 NSBundle performs any mapping required to give you localised resources. NSBundle执行为您提供本地化资源所需的任何映射。

There's a tutorial here that got me going. 有一个教程在这里让我去说。 It's a little out of date, but none of the significant details have changed. 它有点过时了,但是重要的细节都没有改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM