lproj文件尚未加载

时间:2017-03-15 10:47:06

标签: ios swift localization

我创建了3个翻译文件:

  • localizable.strings(英文)
  • localizable.strings(瑞典语)
  • localizable.strings(芬兰语)

我有这个方法来按键获取字符串:

static func localized(key: String) -> String? {
        if let path = Bundle.main.path(forResource: currentLanguage, ofType: "lproj") {
            if let bundle = Bundle(path: path) {
                return NSLocalizedString(key, tableName: nil, bundle: bundle, value: "", comment: "")
            }
        }

        return nil;
    }

但我得到了“NSBundle< /var/containers/Bundle/Application/213B1469-9751-4459-ABED-80879B80EFBE/Dicken.app/en.lproj>(尚未加载)”

为什么不加载,问题出在哪里?

1 个答案:

答案 0 :(得分:1)

制作扩展名为

extension String {
func localized(lang:String) -> String {

    let path = Bundle.main.path(forResource: lang, ofType: "lproj")
    let bundle = Bundle(path: path!)

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

}

let label: UILabel
var language: String?
label.text  = “Hello”.localized(lang:  self.language! )