NSURLConnection返回NSError,只有英语作为语言? [英] NSURLConnection returns NSError with only english as language?
问题描述
如果我在iPad上打开飞行模式并尝试进行NSURLConnection,我将收到错误消息Internet连接似乎处于脱机状态。如果我更改为任何其他语言,那么我的iPad上的英语仍然会有相同的错误文本。我不应该选择当前语言中的错误吗?
If i turn on Airplane mode 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?
使用此代码:
- (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.
包含错误代码和本地化描述的文件,位于 /System/Library/Frameworks/Foundation.Framework/Resources/en.lproj/FoundationErrors.strings
复制此文件并粘贴到其他地方(以避免弄乱原始文件)。现在在终端中运行命令将二进制文件转换为文本文件:
plutil -convert xml1 FoundationErrors.strings
现在将文件拖到你的文件中Xcode项目并右键单击并选择打开为...属性列表
。现在你有了密钥和描述。您可以翻译(如果您的所需语言在本地化中不可用)或根据需要自定义它们,并在我的案例中使用所需名称 Persian.strings
保存它。
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];
这篇关于NSURLConnection返回NSError,只有英语作为语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!