Storyboard Localization

Internationalizing the users interface in Xcode is really easy. Xcode separates all the texts from your views in a dictionary. However, we can make it better, using extensions and @IBDesignables. How handy would it be, if setting localized strings were as easy as the following?


Easier than that, are the extensions to unleash this fancy feature.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
extension UITextField {

    @IBInspectable var localizedPlaceholder: String {
        get { return "" }
        set {
            self.placeholder = NSLocalizedString(newValue, comment: "")
        }
    }

    @IBInspectable var localizedText: String {
        get { return "" }
        set {
            self.text = NSLocalizedString(newValue, comment: "")
        }
    }
}

extension UITextView {

    @IBInspectable var localizedText: String {
        get { return "" }
        set {
            self.text = NSLocalizedString(newValue, comment: "")
        }
    }
}

extension UIBarItem {

    @IBInspectable var localizedTitle: String {
        get { return "" }
        set {
            self.title = NSLocalizedString(newValue, comment: "")
        }
    }
}

extension UILabel {

    @IBInspectable var localizedText: String {
        get { return "" }
        set {
            self.text = NSLocalizedString(newValue, comment: "")
        }
    }
}

extension UINavigationItem {

    @IBInspectable var localizedTitle: String {
        get { return "" }
        set {
            self.title = NSLocalizedString(newValue, comment: "")
        }
    }
}

extension UIButton {

    @IBInspectable var localizedTitle: String {
        get { return "" }
        set {
            self.setTitle(NSLocalizedString(newValue, comment: ""), forState: UIControlState.Normal)
        }
    }
}

extension UISearchBar {

    @IBInspectable var localizedPrompt: String {
        get { return "" }
        set {
            self.prompt = NSLocalizedString(newValue, comment: "")
        }
    }

    @IBInspectable var localizedPlaceholder: String {
        get { return "" }
        set {
            self.placeholder = NSLocalizedString(newValue, comment: "")
        }
    }
}

extension UISegmentedControl {

    @IBInspectable var localized: Bool {
        get { return true }
        set {
            for index in 0..<numberOfSegments {
                var title = NSLocalizedString(titleForSegmentAtIndex(index)!, comment: "")
                setTitle(title, forSegmentAtIndex: index)
            }
        }
    }
}

UISegmentedControl may have multiple segments. In this case, set the localized to true and put the localized key into the storyboard. The extension will lookup into it and return the right value for every segment.

Way better, don’t you think? Now you have a shortcut for setting localized strings.