是否有一个Swift等效的NSLocalizedString(…)?
在Objective-C中,我们通常使用:
NSString *string = NSLocalizedString(@"key", @"comment");
我如何在Swift中实现同样的目标?我找到了一个函数:
func NSLocalizedString(
key: String,
tableName: String? = default,
bundle: NSBundle = default,
value: String = default,
#comment: String) -> String
但是,它很长,一点也不方便。
我使用下一个解决方案:
1)创建扩展:
extension String {
var localized: String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
}
}
2)本地化。字符串文件:
"Hi" = "Привет";
3)使用实例:
myLabel.text = "Hi".localized
享受吧!;)
——乌利希期刊指南:
对于带有注释的情况,您可以使用此解决方案:
1)扩展:
extension String {
func localized(withComment:String) -> String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: withComment)
}
}
2) .strings文件:
/* with !!! */
"Hi" = "Привет!!!";
3)使用:
myLabel.text = "Hi".localized(withComment: "with !!!")
也许最好的方法是这个。
fileprivate func NSLocalizedString(_ key: String) -> String {
return NSLocalizedString(key, comment: "")
}
and
import Foundation
extension String {
static let Hello = NSLocalizedString("Hello")
static let ThisApplicationIsCreated = NSLocalizedString("This application is created by the swifting.io team")
static let OpsNoFeature = NSLocalizedString("Ops! It looks like this feature haven't been implemented yet :(!")
}
然后你可以像这样使用它
let message: String = .ThisApplicationIsCreated
print(message)
对我来说这是最好的,因为
硬编码的字符串在一个特定的文件中,所以哪天你想改变它就很容易了
比每次在文件中手动输入字符串更容易使用
Genstrings仍然可以工作
你可以添加更多的扩展,比如每个视图控制器一个扩展来保持整洁
实际上,你可以在Swift项目中使用两个阶段来翻译你的文本:
1)第一阶段是使用旧的方法来创建所有可翻译的字符串:
NSLocalisedString("Text to translate", comment: "Comment to comment")
1.1)然后你应该使用genstrings生成Localizable.strings:
$ genstrings *swift
2)之后,你应该使用这个答案。
2.1)基于正则表达式使用XCode的“Find and Replace”选项。
对于给定的例子(如果你没有注释),正则表达式将是:
NSLocalizedString\((.*)\, comment:\ \"\"\)
并将其替换为
$1.localized
或者(如果你有意见的话)
NSLocalizedString\((.*)\, comment:\ (.*)\)
并将其替换为
$1.localizedWithComment(comment: $2)
您可以随心所欲地使用正则表达式和不同的扩展组合。一般的方法是把整个过程分成两个阶段。希望这能有所帮助。
这是对“的改进”。本地化”的方法。首先添加类扩展名,因为这将帮助您以编程方式设置任何字符串:
extension String {
func localized (bundle: Bundle = .main, tableName: String = "Localizable") -> String {
return NSLocalizedString(self, tableName: tableName, value: "\(self)", comment: "")
}
}
用程序设置的字符串示例:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
现在Xcode的故事板翻译文件使文件管理器变得混乱,也不能很好地处理故事板的更新。一个更好的方法是创建一个新的基本标签类,并将其分配给所有的故事板标签:
class BasicLabel: UILabel {
//initWithFrame to init view from code
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
//initWithCode to init view from xib or storyboard
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
//common func to init our view
private func setupView() {
let storyboardText = self.text
text = storyboardText?.localized()
}
}
现在,您在故事板中添加并提供默认默认值的每个标签都将自动得到翻译,假设您已经为它提供了一个翻译。
你可以对UIButton做同样的事情:
class BasicBtn: UIButton {
//initWithFrame to init view from code
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
//initWithCode to init view from xib or storyboard
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
//common func to init our view
private func setupView() {
let storyboardText = self.titleLabel?.text
let lclTxt = storyboardText?.localized()
setTitle(lclTxt, for: .normal)
}
}
我已经创建了自己的genstrings工具,用于使用自定义翻译函数提取字符串
extension String {
func localizedWith(comment:String) -> String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: comment)
}
}
https://gist.github.com/Maxdw/e9e89af731ae6c6b8d85f5fa60ba848c
它将解析所有swift文件,并将代码中的字符串和注释导出到.strings文件中。
也许这不是最简单的方法,但这是可能的。