开发者

Displaying localized text unavailable at compile time on iPhone

开发者 https://www.devze.com 2023-01-15 09:35 出处:网络
For use in a questionnaire application, a web service will provide a list of questions in one of several languages, chosen by the user at runtime.The questions will be downloaded from the web service

For use in a questionnaire application, a web service will provide a list of questions in one of several languages, chosen by the user at runtime. The questions will be downloaded from the web service in the chosen language and displayed to the user.

The problem: I have no idea how to do this.

As a sample, I tried loading in UTF-8 text files (e.g. arabic.txt) in the resources containing samples of text in said languages. The file开发者_StackOverflows open and render properly in TextMate and TextEdit, but are illegible in Xcode. They are successfully read in, but their contents will not display.

Example: I create a UITextView and, at initialization:

...
NSString *path = [[NSBundle mainBundle] pathForResource:@"arabic" ofType:@"txt"];
NSString *arabicString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringENcoding error:&error];
myTextView.text = arabicString;
...

The NSError returns NULL, so there's no error reading in the text file, but the contents of the UITextView is not set. Nothing happens. No error (compilation or runtime), nothing.

Any ideas for a different approach? Or a way to make this work?

Thanks so much.


What you are currently doing sounds reasonable, but for a different approach that I have used (that worked for me), try using a UIWebView. Something along the lines of:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/arabic.html",path]];

NSData *data = [NSData dataWithContentsOfURL:url];
[self.helpWebView loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:baseURL];

(Be interesting to see if using arabic.txt and mime-type of text/plain loads any differently in the web view.)

0

精彩评论

暂无评论...
验证码 换一张
取 消