如何在我的应用程序中使用 UIKit 本地化字符串

发布于 2024-11-27 20:22:53 字数 980 浏览 0 评论 0原文

我们正在构建一款 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

以下语言在 NSUserDefaultUIKit.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昵称有卵用 2024-12-04 20:22:53

UIKit 中已有字符串的简单方法

NSBundle* uikitBundle = [NSBundle bundleForClass:[UIButton class]];
NSString *cancel = [uikitBundle localizedStringForKey:@"Cancel" value:nil table:nil];

Easy method for Strings already in UIKit

NSBundle* uikitBundle = [NSBundle bundleForClass:[UIButton class]];
NSString *cancel = [uikitBundle localizedStringForKey:@"Cancel" value:nil table:nil];
凤舞天涯 2024-12-04 20:22:53

斯威夫特4:

    let bundle = Bundle.init(for: UIButton.self)
    let doneTitle = bundle.localizedString(forKey: "Done", value: nil, table: nil)
    for state: UIControlState in [.normal, .highlighted, .disabled, .selected, .focused, .application, .reserved] {
        buttonDone.setTitle(doneTitle, for: state)
    }

Swift 4:

    let bundle = Bundle.init(for: UIButton.self)
    let doneTitle = bundle.localizedString(forKey: "Done", value: nil, table: nil)
    for state: UIControlState in [.normal, .highlighted, .disabled, .selected, .focused, .application, .reserved] {
        buttonDone.setTitle(doneTitle, for: state)
    }
仅一夜美梦 2024-12-04 20:22:53

您应该使用基础框架中的 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 您可以将“取消”按钮标题设置为您想要的任何内容...

     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"title",@"titleKey") 
message:NSLocalizedString(@"message",@"messageKey") 
delegate:self 
cancelButtonTitle:NSLocalizedString(@"cancel",@"cancelKey") 
otherButtonTitles:nil];

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...

     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"title",@"titleKey") 
message:NSLocalizedString(@"message",@"messageKey") 
delegate:self 
cancelButtonTitle:NSLocalizedString(@"cancel",@"cancelKey") 
otherButtonTitles:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文