2013-01-14 45 views
1

我的应用程序有一个localizable.strings文件,支持英语,法语和德语,并且我有一个警报视图,当您点击一个按钮时弹出,我如何使这个警报视图的语言与设备已经设置的语言相匹配为? 任何帮助将不胜感激。如何更改UIAlertView的语言以匹配设备语言?

回答

14

与您的应用中的任何其他本地化字符串一样,在您的Localizable.strings文件中本地化了UIAlertView消息,标题和按钮标题。

见这个例子:

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]; 
+0

谢谢,它的工作原理。 –

0

检查设备的语言环境设置。

NSString *localeLang = [[NSLocale preferredLanguages] objectAtIndex:0]; 

这将返回代码为语言......你可以找到它的代码被用于与此谷歌搜索的语言列表:

http://www.google.com/search?client=safari&rls=en&q=ISO+639-1+codes&ie=UTF-8&oe=UTF-8

应当指出的是,一些语言* 可能 *有多个代码,我从来没有检查过,所以我不知道。

+0

*重新阅读这个问题后,它发生在我身上你说“本地化的字符串”我不知道这些是什么,所以也许它做了我上面描述的你......如果这么抱歉,但我会离开这个答案作为任何人尝试手动完成的参考。 –

+0

不要重新发明轮子,使用'NSLocalizedString()'! –

+0

^请阅读我的评论以上你的... –

0

的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) 
} 

享受。