That’s correct. That specific framework emits a notification via NotificationCenter whenever the language was changed, so your ViewControllers can listen for it and refresh the strings.
Hey Richard!
Thank you for very exciting and useful tutorial. Though some parts were and others will be useful and more clear to me. For I used to work as a freelance translator. Sometimes I was a little bit confused when translating strings with no clear explanations form the developer. Eh, no I am doing my best to become one. Just started learning Swift a while ago. And then I made my very first very simple app and as a former translator wished to localize it both for Russian and Turkish. Unfortunately, at that moment I could find only partly useful tutorials on that on youtube. But anyway, I did my best to “fake it till make it”.
So, no, I am stilling this nice tutorial of yours to my evernote. Thanks again!
Hi Alisher!
I’m glad to hear the tutorial is helpful to you. Best of luck on your journey as a developer!
-r
Question: are the steps outlined in this tutorial going to be essentially the same for macOS apps as for iOS? (Obviously replacing “NS” for “UI” in class names.)
Hi! Yes, the steps will be essentially the same.
My app allows the user to selected a preferred language for the app via UserDefaults. This could be different than the device language setting. Here is an action that launches the Settings app:
// Open the device settings app so the user can select application settings
@IBAction func settingsAction(_ sender: Any) {
if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(appSettings as URL, options: [ : ], completionHandler: { (success) in })
}
}
The ViewController listens for changes to the UserDefaults settings using:
// Add Observer in viewWillAppear
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil)
// Function called by #selector
@objc func userDefaultsDidChange(_ notification: Notification) {
// do whatever is necessary
}
The text of different languages is kept in Core Data. The app also allows the user to temporarily change the language of currently displayed text by simply clicking on a button (without changing the app’s default language). If you’d like to see the app (it is free) - or if you are interested in tournament poker rules, you can install it from here: TDA Rules 2015
Note that any default values that you might set in the Settings.bundle
are not automatically in sync with any values that you might set in UserDefaults.standard.register(defaults: )
. Make sure they are initially set correctly.
As your in tutorial, many other NSLocalizedString() examples provide the original english text as the first argument i.e
NSLocalizedString("Press this button", comment:"Explanation")
The problem with this is that it can quickly become a nightmare to maintain if a) the text is changing, b) the same text is used elsewhere but in a different context (so a slightly different meaning). I would recommend NOT to go this route but use the ‘key’ argument in NSLocalizedString() truly as a key that uniquely identifies the text. Something like:
NSLocalizedString(DIALOG_LBL_PRESS_BTN, comment:"Explanation")
Yes, i know it isnt as easy on the eyes but in the long run will save you a bunch of time.
This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!