是否有一个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

但是,它很长,一点也不方便。


当前回答

扩展:

 extension String {
    func localized(comment: String = "") -> String {
        return NSLocalizedString(self, comment: comment)
    }
  }

使用:“_YOUR_STRING_NAME_ .localized ()

其他回答

有助于在单元测试中使用:

这是一个简单的版本,可以扩展到不同的用例(例如使用tableNames)。

public func NSLocalizedString(key: String, referenceClass: AnyClass, comment: String = "") -> String 
{
    let bundle = NSBundle(forClass: referenceClass)
    return NSLocalizedString(key, tableName:nil, bundle: bundle, comment: comment)
}

像这样使用它:

NSLocalizedString("YOUR-KEY", referenceClass: self)

或者像这样加一条评论:

NSLocalizedString("YOUR-KEY", referenceClass: self, comment: "usage description")

为总是忽略“comment”的情况创建了一个小的helper方法。更少的代码更容易阅读:

public func NSLocalizedString(key: String) -> String {
    return NSLocalizedString(key, comment: "")
}

只要把它放在任何地方(类之外),Xcode就会找到这个全局方法。

这是对“的改进”。本地化”的方法。首先添加类扩展名,因为这将帮助您以编程方式设置任何字符串:

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

Swift 3版本:)…

import Foundation

extension String {
    var localized: String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
    }
}

除了伟大的扩展写在这里,如果你懒得找到和替换旧的NSLocalizedString,你可以在Xcode中打开查找和替换,在查找部分,你可以写NSLocalizedString\(\(".*"\),评论:""\)然后在替换部分,你需要写$1。localizedstring的所有NSLocalizedString与"blabla"。本地化到您的项目中。