Reputation: 239
I had google many posts regarding the localization on IOS app. However, most of them are talking about objective-C and they do not mention on swift language. I just wonder that is it possible to use built in locatization feature in Xcode when changing storyboard at runtime. Please see the example as link below. Thank you.
http://tinypic.com/view.php?pic=fw3cyu&s=8#.VcBAIniloRl
Upvotes: 0
Views: 1873
Reputation: 11
If you want to localize the label of storyboard programmatically, you can simply subclass the UILabel and translated it after you set text. So you do not need to create another storyboard.
class TranslatedLabel: UILabel {
override func drawTextInRect(rect: CGRect) {
self.text = Language.sharedInstance.stringforKey(self.text!)
super.drawTextInRect(rect)
}
}
class Language {
class var sharedInstance : Language {
struct Static {
static var onceToken : dispatch_once_t = 0
static var instance : Language? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = Language()
}
return Static.instance!
}
func stringforKey(key:String)->String{
let lan:String = NSUserDefaults.standardUserDefaults().objectForKey("language") as! String
let string:String?
if lan.containsString("Hans"){
let path = NSBundle.mainBundle().pathForResource("zh-Hans", ofType: "lproj")
let bundle = NSBundle(path: path!)
string = bundle?.localizedStringForKey(key, value: nil, table: nil)
}else if lan.containsString("Hant"){
let path = NSBundle.mainBundle().pathForResource("zh-Hant", ofType: "lproj")
let bundle = NSBundle(path: path!)
string = bundle?.localizedStringForKey(key, value: nil, table: nil)
}else{
let path = NSBundle.mainBundle().pathForResource("en", ofType: "lproj")
let bundle = NSBundle(path: path!)
string = bundle?.localizedStringForKey(key, value: nil, table: nil)
}
return string!
}
}
Upvotes: 0
Reputation: 3598
You Have to Write Custom Localisation methods, else in iPhone you can only change from system setting, which definitely makes device reboot, Here i have created a Custom Module of In-app-localisation in SWIFT from which it is possible to change language of your app in runtime
https://github.com/pr0gramm3r8hai/InAppLocalize
Upvotes: 2