Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i link a uilabel to a localizable string in Interface Builder?

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?

like image 833
Mathias Avatar asked Sep 14 '11 08:09

Mathias


People also ask

How do you make a string file localizable?

To add Localizable. strings file, go to File->New->File , choose Strings File under Resource tab of iOS, name it Localizable. strings , and create the file. Now, you have a Localizable.

How do I use localized strings in Swiftui?

create a new file of type "Strings File", call it Localizable. strings. select the new file and navigate to File Inspector in the right hand side panel and click Localize... go to your project file to the Localizations section and add another language to the list - Xcode will create localization files for you.

What is localization string?

There are two categories of localized strings: the strings included in the installation package's UI, common to every MSI file. the strings included in your project, that are particular to the current project: the name of your application, file names, registry values, properties etc.


2 Answers

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.

like image 77
Claus Broch Avatar answered Oct 20 '22 19:10

Claus Broch


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

enter image description here

3. Enter your key from Localizable.strings directly in IB

enter image description here

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

like image 27
Ibrahim Yildirim Avatar answered Oct 20 '22 20:10

Ibrahim Yildirim