How To Refresh Content Of Localization Strings Of Storyboard
Yes. This is one of the million limitations of Xcode. I generally only do this localization as the last thing I do for an application.
As you add new elements, Xcode will not add these new elements to the localization file. You have to do it manually.
Use localized strings in storyboard
According to your requirement it's not possible but
You don't need different storyboards for localization
Suppose you want to localize a label string.
- Draw and outlet and change text using
mylabel.text = nsLocalizedString("THIS_IS_MY_STRING",nil);
Of course in your localization file there will be a line.You must have different files for different language.Suppose you have a file for english and there must be a line.
"THIS_IS_MY_STRING" = "This is my string";
When you compile your app, that function will use mapping to localize your app.
Edit:
If you want detail information please have a look at these tutorials
internationalization-tutorial-for-ios-2014
and ios-localization-tutorial
There are some online script(e.g localize.py) which will help you to automatically search all of your code and find out nslocalizedString function and make lines in your localizableString files. like this.
"THIS_IS_MY_STRING" = "THIS_IS_MY_STRING"
and later on you just have to write actual string there. :)
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:
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.
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
3) Run and test
iOS is there a way to assign Localized String to a UILable from IB/storyboard
Just select your storyboard/xib
, go to File Inspector
. In the Localization
part, you can tick the language you want. Then, it will create a .string
file with all text used in storyboard
. You'll just have to provide a translation for each text used in storyboard
.
If no language appear in this part, you have to change values of Localization native development region
in Info of your project.
Related Topics
Didreceiveremotenotification Not Working in the Background
How to Save, Retrieve, Delete & Update My Data in Plist File in iOS
Swift Default Alertviewcontroller Breaking Constraints
Date to Milliseconds and Back to Date in Swift
Command /Usr/Bin/Codesign Failed with Exit Code 1
Trying to Compile the Ffmpeg Libraries for Iphoneos Platform with Armv6 and Arv7 Architecture
How to Create Negative Firebase Timestamp in Swift
Duplicate Symbol Error in Nsmanagedobject Subclass
Xcode 7 Swift 2 Impossible to Instantiate Uiviewcontroller Subclass of Generic Uitableviewcontroller
Firebase Cloud Messaging - Check Existing or Available Topics
How to Add Multiple Collection Views in a Uiviewcontroller in Swift
Why Calling Setneedsupdateconstraints Isn't Needed for Constraint Changes or Animations
How to Manually Deprecate Members
How to Center a Popoverview in Swift
Using Audiobufferlist with Swift