如何在我的应用程序中使用 UIKit 本地化字符串
我们正在构建一款 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
。它们应该由代码处理。
We are building a iOS game, our company requires cancel button in a UIAlertView
should always be localized depend on user's device language.
It looks like there is such a string in UIKit framework, how can I access it in my own app?
Or, any other way to create a UIAlertView with a localized cancel button?
Thank you
Answer by myself:
Problem solved by the following code:
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));
This read string files from /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/System/Library/Frameworks/UIKit.framework
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UIKit 中已有字符串的简单方法
Easy method for Strings already in UIKit
斯威夫特4:
Swift 4:
您应该使用基础框架中的 NSLocalizedString:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html
有一个关于它的很好的教程: http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/
这些预定义的按钮名称将由操作系统自动翻译(在选项卡栏中完成)并在uialertview 您可以将“取消”按钮标题设置为您想要的任何内容...
You should be using NSLocalizedString from foundation framework:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html
There is a nice tutorial about it: http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/
Those predefined button names will be translated automatically by OS (Done in tab bar) and in uialertview you can set Cancel button title to be whatever you want...