2013-01-14 92 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) 
} 

享受。