Swift 4.0中的情节提要板本地化 [英] storyboard localization in swift 4.0
问题描述
我想用快速的语言实现情节提要的本地化.(意味着我想本地化修复标签和按钮文本)
我已经对NSLocalizedString有所了解,但是我不想为固定文本标签编码
例如
NSLocalizedString("Welcome",评论:")
我已经为特定语言添加了Localizable.strings文件和Main.string文件.但我无法成功实现本地化
Bhumesh
我已将此库用于应用内本地化.这是非常容易使用.
3)运行并测试
I want to implement storyboard localization in swift language. (means I want to localization for fix label and button text)
I have already idea about NSLocalizedString but I dont want to code for fix text Label
for e.g
NSLocalizedString("Welcome", comment: "")
I have already add Localizable.strings file as well as Main.string file for particular language. but I can not success in implement Localization
Bhumesh
I have used this library for in - app localisation. Which is very easy to use.
https://github.com/marmelroy/Localize-Swift
Now For Storyboard support I have Created Following extension that is IBDesignable So you can easily provide localised text from storyboard itself
1 ) Add This into new swift file
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 and provide the key
3) Run and test
这篇关于Swift 4.0中的情节提要板本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!