【问题标题】:How to change the language of a UIAlertView to match the device language?如何更改 UIAlertView 的语言以匹配设备语言?
【发布时间】:2016-01-18 09:35:13
【问题描述】:

我的应用程序有一个 localizable.strings 文件,它支持英语、法语和德语,我有一个警报视图,当您点击一个按钮时会弹出一个警报视图,那么我怎样才能使这个警报视图的语言与设备的语言相匹配设置为? 任何帮助将不胜感激。

【问题讨论】:

    标签: iphone ios sdk localization uialertview


    【解决方案1】:

    与您应用中的任何其他本地化字符串一样,将 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];
    

    【讨论】:

      【解决方案2】:

      检查设备语言环境设置。

      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()
      【解决方案3】:

      对于 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)
      }
      

      享受吧。

      【讨论】:

        猜你喜欢
        • 2016-05-20
        • 1970-01-01
        • 1970-01-01
        • 2017-03-08
        • 1970-01-01
        • 2011-02-05
        • 1970-01-01
        • 1970-01-01
        • 2014-01-11
        相关资源
        最近更新 更多