简体   繁体   English

如何找到本地化目录的路径?

[英]How to find path to localized directory?

I need to distribute a directory containing html files and images with my app.我需要使用我的应用程序分发一个包含 html 文件和图像的目录。

The app has support for different languages.该应用程序支持不同的语言。 I have created a directory for each language and then pick the right one based on current locale:我为每种语言创建了一个目录,然后根据当前语言环境选择正确的目录:

NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" 
                                                 ofType:@"html" 
                                            inDirectory:[language stringByAppendingPathExtension:@"html"];];

if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
    // Fallback to english
    path = [[NSBundle mainBundle] pathForResource:@"index" 
                                           ofType:@"html" 
                                      inDirectory:@"en.html"];
}

How can I better deal with this instead of having to do the above (which is a bit messy)?我怎样才能更好地处理这个而不是必须做上述(这有点乱)?

I'm thinking perhaps using the xx.lproj directories for this somehow and putting a localized html directory in each xx.lproj directory and use NSBundle pathForResource to find the correct file.我在想可能以某种方式使用xx.lproj目录,并在每个 xx.lproj 目录中放置一个本地化的 html目录,并使用NSBundle pathForResource找到正确的文件。 Couldn't get it to work though.虽然无法让它工作。

Using the xx.lproj folders with [NSBundle pathForResource:ofType:] is straight-forward.使用带有[NSBundle pathForResource:ofType:]xx.lproj文件夹非常简单。

You can add index.html to your Xcode's project file and then make it localizable from its "Get Info" window.您可以将index.html添加到 Xcode 的项目文件中,然后从其“获取信息”window 使其可本地化。 Add another language like "de", and Xcode will copy index.html into the newly created de.lproj .添加另一种语言,如“de”,Xcode 会将index.html复制到新创建的de.lproj中。 If you remove that file, the app will fall back on the English version.如果您删除该文件,该应用程序将使用英文版本。

You can test it with logging:您可以使用日志记录对其进行测试:

NSLog([[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]);

I too am unable to get this to work:我也无法让它工作:

    NSLog([[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]);

But this does, using the standard Xcode language.lproj structure (eg, en.lproj):但这样做,使用标准 Xcode language.lproj 结构(例如,en.lproj):

    NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" 
                  ofType:@"html" 
                  inDirectory:[language stringByAppendingPathExtension:@"lproj"]];

also removed extra;还删除了额外的; in Martin's original question above...在上面马丁的原始问题中......

Extra tip: make sure that you remove the unlocalized versions of the files in your built product, otherwise pathForResource will find those files instead of the localized ones.额外提示:确保删除构建产品中文件的未本地化版本,否则 pathForResource 将找到这些文件而不是本地化文件。

Why not the forLocalization way:为什么不用forLocalization方式:

        htmlFile = [[NSBundle mainBundle] pathForResource:@"myContent"
                                               ofType:@"html"
                                          inDirectory:@"de.lproj"
                                      forLocalization:@"de"];

This works fine here with Xcode 5.1.1 and iOS 7.1这适用于 Xcode 5.1.1 和 iOS 7.1

Add the generalization of @Martin Wickman (with the fallback) and everything should be great.添加@Martin Wickman 的概括(带有后备),一切都应该很棒。

You could use this:你可以使用这个:

NSString *filename = [NSString stringWithFormat:@"html_%@.html", NSLocalizedString(@"abbr", @"")];

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

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