我们正在构建一款 iOS 游戏,我们公司要求 UIAlertView
中的取消按钮应始终根据用户的设备语言进行本地化。
UIKit框架中好像有这样一个字符串,我如何在自己的应用程序中访问它?
或者,还有其他方法可以创建带有本地化取消按钮的 UIAlertView 吗?
谢谢
我自己回答:
通过以下代码解决问题:
NSBundle* uikitBundle = [NSBundle bundleForClass:[UIButton class]];
NSString *language = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0];
NSBundle *languageBundle = [NSBundle bundleWithPath:[uikitBundle pathForResource:language ofType:@"lproj"]];
NSLog(@"%@: %@", language, NSLocalizedStringFromTableInBundle(@"Cancel", @"Localizable", languageBundle, nil));
这将从 /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/System/Library/Frameworks/UIKit.framework
以下语言在 NSUserDefault
和 UIKit.framework
文件夹中具有不同的名称:fr en zh-Hans it de ja nl es pt-PT nb zh-Hant en-GB
。它们应该由代码处理。
最佳答案
针对 UIKit 中已有字符串的简单方法
NSBundle* uikitBundle = [NSBundle bundleForClass:[UIButton class]];
NSString *cancel = [uikitBundle localizedStringForKey:@"Cancel" value:nil table:nil];
关于iphone - 如何在我的应用程序中使用 UIKit 本地化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6909885/