开发者

NSURLConnection returns NSError with only english as language?

If i turn on Airplane m开发者_开发知识库ode on my iPad and trying to make a NSURLConnection i will get an error "The Internet connection appears to be offline." If i change to any other language then english on my iPad i will still have the same error text. Shouldn't i get the error in the current language that is choosen?

Using this code:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error: %@",[error localizedDescription]);
}


I found the answer to my question, and since it's related to this question, I'm sharing it so that it can help if someone else needs it.

The file containing error codes and localized descriptions, is located in /System/Library/Frameworks/Foundation.Framework/Resources/en.lproj/FoundationErrors.strings Copy this file and paste somewhere else (to avoid messing up with the original file). Now run the command in terminal to convert the binary file to a text file: plutil -convert xml1 FoundationErrors.strings Now drag the file into your Xcode project and right click and select Open as... Property list. Now you have the keys and the descriptions. You can translate (if your desired language isn't available in localization) or customize them as you want, and save it with a desired name which is Persian.strings in my case.

Now add this code to the method where you want to use it:

//persian error messages file full path
NSString * persianErrorsListFile = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Persian.strings"];
//Load file as dictionary
NSDictionary * persianErrorsList = [[NSDictionary alloc] initWithContentsOfFile:persianErrorsListFile];

NSString *errorKey = [NSString stringWithFormat:@"Err%ld", (long)errorCode];
NSString *errorMessage = persianErrorsList[errorKey];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜