I am making a test app to implement localization. In the test app, this is what I'm trying to achieve...
When the user launches the app, the screen will display a label with the text ... 'This is a test project' and button called 'Change' below. Now when I click on the 'change' button, the label should display in French..'c'est un projet test' and the button will be called 'changement'.
I have a class called L102Language
which will set my language to the current language. It is given like so..
import Foundation
import UIKit
// constants
let APPLE_LANGUAGE_KEY = "AppleLanguages"
/// L102Language
class L102Language {
/// get current Apple language
class func currentAppleLanguage() -> String{
let userdef = UserDefaults.standard
let langArray = userdef.object(forKey: APPLE_LANGUAGE_KEY) as! NSArray
let current = langArray.firstObject as! String
return current
}
/// set @lang to be the first in Applelanguages list
class func setAppleLAnguageTo(lang: String) {
let userdef = UserDefaults.standard
userdef.set([lang,currentAppleLanguage()], forKey: APPLE_LANGUAGE_KEY)
userdef.synchronize()
}
}
In the viewcontroller, this is the code I have written..
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var txtLabel: UILabel!
@IBOutlet weak var change: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func changeLanguage(_ sender: Any) {
if L102Language.currentAppleLanguage() == "en" {
L102Language.setAppleLAnguageTo(lang: "fr")
} else {
L102Language.setAppleLAnguageTo(lang: "en")
}
}
}
But when I click on the 'change' button, nothing happens. The text is in english only.
EDIT 1: My Main.strings
file have these...
/* Class = "UILabel"; text = "This is a test project"; ObjectID = "OQM-55-dEJ"; */
"OQM-55-dEJ.text" = "c'est un projet test";
/* Class = "UIButton"; normalTitle = "CHANGE"; ObjectID = "eMn-n8-na5"; */
"eMn-n8-na5.normalTitle" = "changement";