是否有一个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() -> String {
       let defaultLanguage = "en"
       let path = Bundle.main.path(forResource: defaultLanguage, ofType: "lproj")
       let bundle = Bundle(path: path!)

       return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
    }
}

其他回答

使用默认语言进行本地化:

extension String {
func localized() -> String {
       let defaultLanguage = "en"
       let path = Bundle.main.path(forResource: defaultLanguage, ofType: "lproj")
       let bundle = Bundle(path: path!)

       return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
    }
}

当你在开发SDK时。你需要一些额外的操作。

1)创建Localizable。字符串通常在YourLocalizeDemoSDK。

2)创建相同的Localizable。字符串在YourLocalizeDemo。

3)找到你的捆绑路径YourLocalizeDemoSDK。

Swift4:

// if you use NSLocalizeString in NSObject, you can use it like this
let value = NSLocalizedString("key", tableName: nil, bundle: Bundle(for: type(of: self)), value: "", comment: "")

Bundle(for: type(of: self))帮助你在YourLocalizeDemoSDK中找到Bundle。如果你使用Bundle。相反,在Main中,您将得到一个错误的值(实际上它将是与键相同的字符串)。

但是如果你想使用OX博士提到的String扩展,你需要做更多的事情。原点扩展是这样的。

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

正如我们所知,我们正在开发一个SDK, Bundle。main将获得YourLocalizeDemo的bundle。这不是我们想要的。我们需要YourLocalizeDemoSDK中的捆绑包。这是一个快速找到它的技巧。

在YourLocalizeDemoSDK中的NSObject实例中运行下面的代码。你会得到YourLocalizeDemoSDK的URL。

let bundleURLOfSDK = Bundle(for: type(of: self)).bundleURL
let mainBundleURL = Bundle.main.bundleURL

打印这两个url,你会发现我们可以基于mainBundleURL构建bundleURLofSDK。在本例中,它将是:

let bundle = Bundle(url: Bundle.main.bundleURL.appendingPathComponent("Frameworks").appendingPathComponent("YourLocalizeDemoSDK.framework")) ?? Bundle.main

而String扩展名将是:

extension String {
    var localized: String {
        let bundle = Bundle(url: Bundle.main.bundleURL.appendingPathComponent("Frameworks").appendingPathComponent("YourLocalizeDemoSDK.framework")) ?? Bundle.main
        return NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: "")
    }
}

希望能有所帮助。

NSLocalizedString也存在于Swift的世界中。

func NSLocalizedString(
    key: String,
    tableName: String? = default,
    bundle: NSBundle = default,
    value: String = default,
    #comment: String) -> String

tableName、bundle和value参数用默认关键字标记,这意味着在调用函数时可以忽略这些参数。在本例中,将使用它们的默认值。

这导致一个结论,方法调用可以简化为:

NSLocalizedString("key", comment: "comment")

Swift 5 -没有变化,仍然像那样工作。

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

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

我使用下一个解决方案:

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 !!!")