swift 4.0 中的故事板本地化

storyboard localization in swift 4.0

我想用 swift 语言实现故事板本地化。 (意味着我想本地化修复标签和按钮文本)

我已经了解 NSLocalizedString,但我不想为固定文本标签编写代码

例如

NSLocalizedString("Welcome", comment: "")

我已经为特定语言添加了 Localizable.strings 文件和 Main.string 文件。但我无法成功实施本地化

Bhumesh

我已将此库用于应用内本地化。这是非常容易使用的。

https://github.com/marmelroy/Localize-Swift

现在为了故事板支持,我创建了以下扩展 IBDesignable 因此您可以轻松地从故事板本身提供本地化文本

1 ) 将此添加到新的 swift 文件中

import Localize_Swift

@IBDesignable class LocalizableLabel: UILabel {

    @IBInspectable var table :String? // Table 
    @IBInspectable var key:String? // KEY 

    @IBInspectable var extraTextToAppend:String? // Some text need to append , if any


    override func awakeFromNib() {
        guard let key = key else {return}
        self.text = key.localized(using: table)
        NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)

        if let extraText = self.extraTextToAppend, let text = self.text {
            self.text = text + extraText
        }

    }

    @objc func setText () {
        guard let key = key else {return}
        self.text = key.localized(using: table)

        if let extraText = self.extraTextToAppend, let text = self.text {
            self.text = text + extraText
        }


    }

}

@IBDesignable class LocalizableButton: UIButton {

    @IBInspectable var table :String?
    @IBInspectable var key:String?

    override func awakeFromNib() {
        guard let key = key else {return}
        self.setTitle(key.localized(using: table), for: .normal)

        if let attributedText = self.attributedTitle(for: .normal) {
            let mutableAttributedText = NSMutableAttributedString(attributedString: attributedText)
            mutableAttributedText.replaceCharacters(in: NSMakeRange(0, mutableAttributedText.length), with: key.localized(using: table))
            self.setAttributedTitle(mutableAttributedText, for: .normal)
        }

        NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)

    }

    @objc func setText () {
        guard let key = key else {return}
        self.setTitle(key.localized(using: table), for: .normal)

        if let attributedText = self.attributedTitle(for: .normal) {
            let mutableAttributedText = NSMutableAttributedString(attributedString: attributedText)
            mutableAttributedText.replaceCharacters(in: NSMakeRange(0, mutableAttributedText.length), with: key.localized(using: table))
            self.setAttributedTitle(mutableAttributedText, for: .normal)

        }
    }

}



@IBDesignable class LocalizeUINavigationItem: UINavigationItem {

    @IBInspectable var table :String?
    @IBInspectable var key:String?

    override func awakeFromNib() {
        guard let key = key else {return}
        self.title = key.localized(using: table)
        NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)

    }

    @objc func setText () {
        guard let key = key else {return}
        self.title = key.localized(using: table)

    }

}


@IBDesignable class LocalizableUITextField: UITextField {

    @IBInspectable var table_placeholder :String?
    @IBInspectable var key_place_holder:String?

    override func awakeFromNib() {
        guard let key = key_place_holder else {return}
        self.placeholder = key.localized(using: table_placeholder)
        NotificationCenter.default.addObserver(self, selector: #selector(setText), name: NSNotification.Name(LCLLanguageChangeNotification), object: nil)

    }

    @objc func setText () {
        guard let key = key_place_holder else {return}
        self.placeholder = key.localized(using: table_placeholder)

    }

}

2) Goto Storyboard set class to label 并提供 key

3) 运行 和测试

按照以下步骤本地化故事板元素:

  1. 单击您的项目。
  2. 在本地化部分点击 + 图标并添加您想要本地化的语言。
  3. 添加语言后,您将看到该语言的字符串文件。
  4. 转到故事板并单击要本地化的 UI 元素。
  5. Select 身份检查员,在文档部分,您将看到我们需要用于本地化该元素的 对象 ID
  6. 现在转到从第 3 步创建的本地化文件。
  7. 在该字符串文件中,您将看到元素的 对象 ID。更改 Object ID 键的值,它将仅反映到该特定语言。

完成本地化后 运行,您可以为 UI 元素添加扩展,为它们引入简单的本地化。

对于 UIlabel 它看起来像这样:

public extension UILabel {

    @IBInspectable public var localizedText: String? {
        get {
            return text
        }
        set {
            text = NSLocalizedString(newValue ?? "", comment: "")
        }
    }

}

@IBInspectable 允许您从情节提要和编程方式设置本地化密钥。

情节提要本地化是 Apple 提供的方式,但它让我很烦恼 - 肯定不是对程序员最友好的方式。

class ViewController: UIViewController {

    @IBOutlet weak var resetOutlet: MyButton! {
        didSet {
            resetOutlet.setTitle("RESET".localized().uppercased(), for: .normal)
        }
    }
}

extension String {

    func localized(tableName: String = "Localizable") -> String {
        if let languageCode = Locale.current.languageCode, let preferredLanguagesFirst = Locale.preferredLanguages.first?.prefix(2)  {
            if languageCode != preferredLanguagesFirst {
                if let path = Bundle.main.path(forResource: "en", ofType: "lproj") {
                    let bundle = Bundle.init(path: path)
                    return NSLocalizedString(self, tableName: tableName, bundle: bundle!, value: self, comment: "")
                }
            }
            }
        return NSLocalizedString(self, tableName: tableName, value: self, comment: "")
    }
}