Skip to content

Instantly share code, notes, and snippets.

@jonathan-beebe
Last active October 24, 2017 18:48
Show Gist options
  • Select an option

  • Save jonathan-beebe/a8e38daff9364f4e6447ef1d1293acc1 to your computer and use it in GitHub Desktop.

Select an option

Save jonathan-beebe/a8e38daff9364f4e6447ef1d1293acc1 to your computer and use it in GitHub Desktop.
Localizable views in xib or storyboards
import UIKit
/// Implements a UIButton that can be localized within a xib or storyboard.
/// To render a localized string set a “User defined runtime attribute” on
/// the view with a key path of `localizableStringKey` and a string value of
/// the localized string key.
class LocalizableButton: UIButton {
var localizableStringKey: String? {
didSet {
if let key = localizableStringKey {
let string = LocStr(key)
setTitle(string, forState: .Normal)
}
}
}
}
import UIKit
/// Implements a UILabel that can be localized within a xib or storyboard.
/// To render a localized string set a “User defined runtime attribute” on
/// the view with a key path of `localizableStringKey` and a string value of
/// the localized string key.
class LocalizableLabel: UILabel {
var localizableStringKey: String? {
didSet {
if let key = localizableStringKey {
text = LocStr(key)
}
}
}
}
@jonathan-beebe
Copy link
Author

trying out the suggestion here: http://stackoverflow.com/a/7413783/123781

Of course we could just use Base Internationalization but I wanted to get a feel for how this method worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment