I have followed the step in many tutorial for localization.
I added the storyboard localization , Added string files . And chnaged the vlue in both string file as well as in storyboard.
For example, if i am trying English , french
Then in storyboard we have :-> Main.Stroyboard
Main.Strings(French)
/* Class = "UILabel"; text = "Hamza Smith"; ObjectID = "2gA-Wx-5v2"; */
"2gA-Wx-5v2.text" = "Hamza Smith";
Same in language.String
Language.string(French)
"Hamza Smith" = "some thing";
Then in code on change language button action :
Bundle.setLanguage(lang: "Fn"). //FN for example i am adding
My code :
extension Bundle {
private static var bundle: Bundle!
public static func localizedBundle() -> Bundle! {
if bundle == nil {
var appLang = UserDefaults.standard.string(forKey: "app_lang") ?? "en"
if Bundle.main.path(forResource: appLang, ofType: "lproj") == nil {
appLang = "en"
}
let path = Bundle.main.path(forResource: appLang, ofType: "lproj")
bundle = Bundle(path: path!)
}
return bundle;
}
public static func setLanguage(lang: String) {
UserDefaults.standard.set(lang, forKey: "app_lang")
let path = Bundle.main.path(forResource: lang, ofType: "lproj")
bundle = Bundle(path: path!)
}
}
extension String {
func localized() -> String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.localizedBundle(), value: "", comment: "")
}
func localizeWithFormat(arguments: CVarArg...) -> String{
return String(format: self.localized(), arguments: arguments)
}
}
And my iboutlet label has:
titleLabel.text = "Hamza Smith".localized()
Now on tap on my save button after select any language its not changing. I killed the app and relaunch again. But its not changing.
Any help would be great.