【发布时间】:2016-01-18 09:35:13
【问题描述】:
我的应用程序有一个 localizable.strings 文件,它支持英语、法语和德语,我有一个警报视图,当您点击一个按钮时会弹出一个警报视图,那么我怎样才能使这个警报视图的语言与设备的语言相匹配设置为? 任何帮助将不胜感激。
【问题讨论】:
标签: iphone ios sdk localization uialertview
我的应用程序有一个 localizable.strings 文件,它支持英语、法语和德语,我有一个警报视图,当您点击一个按钮时会弹出一个警报视图,那么我怎样才能使这个警报视图的语言与设备的语言相匹配设置为? 任何帮助将不胜感激。
【问题讨论】:
标签: iphone ios sdk localization uialertview
与您应用中的任何其他本地化字符串一样,将 UIAlertView 消息、标题和按钮标题本地化到您的 Localizable.strings 文件中。
看这个例子:
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Connection Error", nil) message:NSLocalizedString(@"Couldn't connect to the internet. Please check your network connection", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil, nil];
【讨论】:
检查设备语言环境设置。
NSString *localeLang = [[NSLocale preferredLanguages] objectAtIndex:0];
这将返回该语言的代码...您可以通过此 google 搜索找到哪些代码用于哪些语言的列表:
http://www.google.com/search?client=safari&rls=en&q=ISO+639-1+codes&ie=UTF-8&oe=UTF-8
需要注意的是,有些语言*可能*有多个代码,我没查过所以不知道。
【讨论】:
NSLocalizedString()!
对于 Swift 2.0,请参见以下示例:
let alert = UIAlertController(
title: (NSLocalizedString("alert_Title" , comment: "Alert title.") as String),
message: "Your message here",
preferredStyle: .Alert)
alert.addAction(UIAlertAction(
title: (NSLocalizedString("alert_RateButton" , comment: "Alert button to rate in App Store.") as String),
style: .Default,
handler: { action in UIApplication.sharedApplication().openURL(NSURL(
string: "https://itunes.apple.com/your_app_at_app_store")!)
print("Going to App at AppStore")
}))
// DISMISS
alert.addAction(UIAlertAction(
title: (NSLocalizedString("alert_BackButton" , comment: "Alert back button.") as String),
style: .Default,
handler: nil))
presentViewController(
alert,
animated: true,
completion: nil)
}
享受吧。
【讨论】: