Mar*_*man 5 iphone localization ios
我需要使用我的应用程序分发包含html文件和图像的目录.
该应用程序支持不同的语言.我为每种语言创建了一个目录,然后根据当前的语言环境选择正确的目录:
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"];
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能更好地处理这个而不是必须做上述(这有点乱)?
我想也许是使用xx.lproj目录这个不知何故,并把本地化HTML 目录中的每个xx.lproj目录,并使用NSBundle pathForResource找到正确的文件.虽然无法让它工作.
使用xx.lproj文件夹[NSBundle pathForResource:ofType:]是直截了当的.
您可以添加index.html到Xcode的项目文件中,然后从"获取信息"窗口进行本地化.添加另一种语言,如"de",Xcode将复制index.html到新创建的语言中de.lproj.如果删除该文件,该应用程序将退回英文版本.
您可以使用日志记录进行测试:
NSLog([[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]);
Run Code Online (Sandbox Code Playgroud)
你可以使用这个:
NSString *filename = [NSString stringWithFormat:@"html_%@.html", NSLocalizedString(@"abbr", @"")];
Run Code Online (Sandbox Code Playgroud)