故事板本地化在swift 4.0中

问题描述 投票:0回答:4

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

我已经了解了NSLocalizedString,但我不想编写修复文本标签

例如

NSLocalizedString("Welcome", comment: "")

我已经添加了Localizable.strings文件以及特定语言的Main.string文件。但我无法成功实现本地化

ios swift localization swift4 nslocalizedstring
4个回答
1
投票

Bhumesh

我使用这个库进行应用内本地化。这很容易使用。

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

现在,对于Storyboard支持,我创建了以下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将类设置为标签并提供密钥

enter image description here

3)运行并测试


3
投票

按照以下步骤本地化Storyboard元素:

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

enter image description here

enter image description here enter image description here


0
投票

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

对于UIlabel,它看起来像这样:

public extension UILabel {

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

}

@IBInspectable允许您从故事板和编程方式设置本地化密钥。

故事板本地化是苹果公司提供的方式,但它让我很烦恼 - 当然不是程序员最友好的。


0
投票

enter image description here

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: "")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.