【问题标题】:How to localize alert message box in Swift如何在 Swift 中本地化警报消息框
【发布时间】:2016-06-16 09:00:48
【问题描述】:
我的警告框消息代码是
let alertController = UIAlertController(title: "Hello!", message:
"Good Morning Everyone!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil))
我想翻译文本
Hello 和 大家早上好!。
如何快速实现?
【问题讨论】:
标签:
swift
localization
xcode7
localizable.strings
【解决方案1】:
您可以为此使用NSLocalizedString。
let title = NSLocalizedString("Hello!", comment: "alertController title")
let message = NSLocalizedString("Good Morning Everyone!", comment: "alertController message")
let alertController = UIAlertController(
title: title,
message: message,
preferredStyle: UIAlertControllerStyle.Alert
)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil))
然后cd进入项目目录并使用genstrings工具生成Localizable.strings文件。
然后将其添加到项目中,并选择它。
在文件检查器中单击Localize...,然后选择您要使用的语言。
这将为特定语言创建 Localizable.strings 文件。
或者你可以选择你的项目然后去Editor->Export For Localization...
这将创建一个 XLIFF 文件,您可以在其中进行翻译,
然后使用Editor->Import Localizations...将其导入
【解决方案2】:
现在,我找到了解决方案!
let alertController = UIAlertController(title: "Hello!", message:
"Good Morning Everyone!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil))
alertController.title = NSLocalizedString("Hello!", comment: "")
alertController.message = NSLocalizedString("Good Morning Everyone!", comment: "")