Storyboard Localization in Swift 4.0

storyboard localization in swift 4.0

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

Sample Image

3) Run and test

Swift: Localization in Storyboard - Labels not added to Main.strings?

After selecting UILabel from storyboard, You can find object id of UILabel in right panel:

Sample Image

Then you can set text of label as below in your Main.string file:

"sxl-NO-5WX.text" = "Phone";

How to change localisation from storyboard to strings file

Turns out, you need to use the Base Localization feature, the dropdown then appears and you can convert existing storyboards to *.strings files.

Sample Image

How to localize an iOS storyboard

In iOS 6 there is a Project setting under the Info tab that says "Use Base Internationalization". If you check the box, it will pull all of the strings out of the Storyboard and into internationalized .strings files.

This way you don't have to have multiple copies of the Storyboard.

Sample Image

Swift: iOS multiple language set label text from storyboard

You can create an extension of the UIKit component and localized text inside the awakeFromNib

Here is demo

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

For UILabel

extension UILabel {
open override func awakeFromNib() {
super.awakeFromNib()
if let textValue = self.text {
self.text = textValue.localized
}
}
}

For UIButton

extension UIButton {
open override func awakeFromNib() {
super.awakeFromNib()
if let text = self.title(for: self.state) {
self.setTitle(text.localized, for: self.state)
}
}
}

For UITextField

extension UITextField {
open override func awakeFromNib() {
super.awakeFromNib()
if let placeHolderValue = self.placeholder {
self.placeholder = placeHolderValue.localized
}
}
}


Related Topics



Leave a reply



Submit