问题描述
我们正在构建iOS游戏,我们公司需要在 UIAlertView
中取消按钮,应始终根据用户的设备语言进行本地化。
We are building a iOS game, our company requires cancel button in a UIAlertView
should always be localized depend on user's device language.
看起来在UIKit框架中有这样一个字符串,如何在我自己的应用程序中访问它?
It looks like there is such a string in UIKit framework, how can I access it in my own app?
或者,任何其他创建方式带有本地化取消按钮的UIAlertView?
Or, any other way to create a UIAlertView with a localized cancel button?
谢谢
自己回答:
以下代码解决了问题:
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
This read string files from /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/System/Library/Frameworks/UIKit.framework
以下语言在<$ c $之间有不同的名称c> NSUserDefault 和 UIKit.framework
文件夹: fr en zh-Hans it de ja nl es pt-PT nb zh -Hant en-GB
。它们应该由代码处理。
Following languages have different name between NSUserDefault
and UIKit.framework
folder: fr en zh-Hans it de ja nl es pt-PT nb zh-Hant en-GB
. They should be handled by code.
推荐答案
简单的方法已经在UIKit中的字符串
Easy method for Strings already in UIKit
NSBundle* uikitBundle = [NSBundle bundleForClass:[UIButton class]];
NSString *cancel = [uikitBundle localizedStringForKey:@"Cancel" value:nil table:nil];
这篇关于如何在我的应用程序中使用UIKit本地化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!