Reputation: 3979
have googled around but found no solution:
Basically, i have a Localizable.strings set up, which i'm using in my code. However, it would be really sweet if i somehow could just refer those values in my XIB's too, so that i can avoid having to create one annoying XIB per language...
Is this possible?
Upvotes: 18
Views: 18078
Reputation: 1076
This is my version. Hope it helps.
extension UILabel {
/// The IBInspectable helps us to set localizable text in IB
@IBInspectable var localizableText: String? {
get { return text }
set(value) { text = value?.localized() }
}
/// The IBInspectable helps us to set localizable text in IB
@IBInspectable var localizableUppercaseText: String? {
get { return text }
set(value) { text = value?.localized().uppercased() }
}
}
extension String {
func localized(bundle: Bundle = .main, tableName: String = "Localizable") -> String {
return NSLocalizedString(self, tableName: tableName, value: "**\(self)**", comment: "")
}
}
Upvotes: 1
Reputation: 31
Another swifty way, but with extension :
extension UILabel {
static var localizedKey:UInt8 = 0
@IBInspectable public var localizationKey: String? {
set {
objc_setAssociatedObject(self, &UILabel.localizedKey, newValue, .OBJC_ASSOCIATION_RETAIN)
}
get {
return objc_getAssociatedObject(self, &UILabel.localizedKey) as? String
}
}
open override func awakeFromNib() {
super.awakeFromNib()
if let localizationKey = self.localizationKey {
self.text = NSLocalizedString(localizationKey, comment: "")
}
}
}
You will then be able to write the localization key directly from IB, and it will be localized at runtime.
Upvotes: 3
Reputation: 2771
I present to you the swifty way
1. Create a subclass of UILabel
class LozalizedLabel : UILabel {
@IBInspectable var keyValue: String {
get {
return self.text!
}
set(value) {
self.text = NSLocalizedString(value, comment: "")
}
}
}
2. Set the class to be LocalizedLabel in IB
3. Enter your key from Localizable.strings directly in IB
Voila, now spend less time on creating useless IBOutlets just for making it a localizable UILabel.
NOTE This does not make the localized string show in the interface builder, but rather just the "key". So when building your TextView's remember to set constraints accordingly
Upvotes: 16
Reputation: 22701
The Apple-supported way to the localize the .xib file. This allow you to localize the text and layout for each language/country.
Upvotes: 0
Reputation: 815
IB cannot use strings from Localizable.strings, but there is another option called Base Internationalization.
In short: you can create separate .strings file attached to the xib/storyboard for every required language. In the result you will have xib with it's own translations for each language. So it can duplicate some of the values that you have in Localizable.strings, but it's more clear way than setting through the code or copying xib files.
Please refer to the Apple Documentation about Base Internationalization.
Upvotes: 4
Reputation: 3042
NSLocalizedString
has the advantage of being easy to implement. Risk is that you might forget to add an entry for a new Gui element.
You can use itool
to dump the string from the original xib file, translate these dumped strings and then load the translated string into the other-language-xib file.
There are some advantages doing it this way compared to NSLocalizedString
.
When you create a XIB you set it to being localized (as before), then run itool
against this original xib to extract the strings.
Translate the strings file into the desired language then load the translation into the localized xib.
If you google for ibtool --generate-strings-file
you should find a few tutorials and examples.
The syntax should be similar to:
#dump original xib to a $xibfile.strings file:
ibtool --generate-strings-file Resources/English.lproj/$xibfile.strings Resources/English.lproj/$xibfile.xib
# offline: translate the $xibfile.xib, place it into the correct lproj folder
# then: merge translated $xibfile.strings with original xib to create localized xib
ibtool --strings-file Resources/de.lproj/$xibfile.strings --write Resources/de.lproj/$xibfile.xib Resources/English.lproj/$xibfile.xib
$xibfile.strings
in the Resources/English.lproj directory will contain the original (English language) strings. The translated $xibfile.strings
must go in the appropriate xx.lproj
directory.
If you add a buildscript to xcode then the loading of the translation will be called at each build.
Upvotes: 5
Reputation: 9212
You could implement a subclass of UILabel and have it automatically pull the localized value when it's initialized from the nib. In the XIB you would then just set the token (or english text if you prefer) and have UILabel pull the localized text from this value.
Upvotes: 18
Reputation: 69469
There is no way to set then from the Localizable.strings
directly with Interface Builder.
The reason you might want to translate the NIB is because the length of the words may vary in each language. Thus allowing you to change the layout of you nib per language.
What you could set them in the viewDidLoad
method of your UIViewController
.
- (void) viewDidLoad {
[supper viewDidLoad];
self.someLabel.text = NSLocalizedString(@"Some Label", @"Some label on some view.");
}
Upvotes: 5
Reputation: 32066
You can do this in code:
label.text = NSLocalizedString(@"key", @"comment");
But I don't think there is a way to do it with just nibs other than localizing the entire nib. I've certainly not found another way in the past.
Upvotes: 1