2011-05-04 102 views
1

我想显示一个html文件到一个UIWebView:iPhone - 使用本地化的资源

NSString *htmlPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"error.htm"]; 

NSError* error; 
NSStringEncoding encoding; 
NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath usedEncoding:&encoding error:&error]; 

NSString* bundlePath = [[NSBundle mainBundle] bundlePath]; 
[self.webView loadHTMLString:htmlContent baseURL:[NSURL fileURLWithPath:bundlePath]]; 

error.htm是局部的。使用此方法时,不加载页面。 htmlContent引用myApp.app/error.htm。但是我所有的error.htm文件都在本地化文件夹中。
如果我使用另一个非本地化的HTML文件(error2.htm,error.htm的纯副本),则会显示它。

我该如何使用本地化文件?

+0

你可能需要分配给NSStringEncoding。如果失败了,你应该检查'htmlContent'字符串是否为空。 – 2011-05-04 11:52:06

+0

编译时是否收到警告?你不应该忽略它!你为什么用字符串编码做那件奇怪的事情? – deanWombourne 2011-05-04 12:01:01

回答

4

您正在使用根资源路径和字符串自己创建html文件的路径 - iPhone不是通灵,它会如何知道您已经本地化了这个文件?

使用

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]; 

而是试图 - 这应该解决您的本地化资源。

0

本地化应该不是问题 - 我完全本地化的加载HTML文件使用罚款这样的事情:

NSString *path = [[NSBundle mainBundle] pathForResource:@"error" ofType:@"htm"]; 

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]]; 

// ... 

[self.webView loadRequest:request]; 
+0

此外,不知道如果这可能是你的问题,但我有我的本地化文件编码为UTF16。 – 2011-05-04 12:07:11

0

答案是不正确的(并且也没有为我工作) - 这是用于加载本地化资源的函数:

- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath forLocalization:(NSString *)localizationName; 

localizationName是两个字符的本地化语言代码。 顺便说一句 - 您可以使用让您的默认/电流之一:

return [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0]; 
+0

它为我工作。 – Oliver 2013-03-11 22:39:07

+0

也许您的资源本地化不正确。 也[NSLocale currentLocale] .localeIdentifier应该工作,可能更正确,而不是深入standardUserDefaults。 – Jon 2014-05-28 14:19:55