My question:
How can my iPhone-app tell the iOS, that the user did select a language in the apps preferences, that is different from the language set in the general settings?
Other formulation of the same question:
How can i tell the system, that NSLocalizedString (@"text", @"comment");
should not access the systemwide selected language, but the in-app-selected language?
background, example:
Please take this situation as an example: A son of german immigrants is living in the north-east of France next to Luxemburg and Germany. His native language is French, so he did set the user-interfaces language of his iPhone to French (Settings -> General -> International -> Language -> Français). But due to his cultural background and because the region where he is living is bilingual, he also speaks German very well. But he doesn't speak ten words of English. On an iPhone (and iPad as well) he has no chance to select a second language, so the phone only knows that he spreaks french. It has no knowledge of the users skills in other languages.
Now comes my app: I did develop it in English and German (German is my native language and English is standard-language in IT). I did develop it according to all rules and best practices for mulilingual iOS-Apps. "First" Language (default language) of my app is English.
This means:
If somebody has chosen English or German in his Settings, the apps user-interface automatically will use the selected language. The user will not even notice that there are other languages available.
But if he did select any other language (like Chinese, Polish or French) in the general settings, he will get the apps default-language, which, in my case, is English. But for my french-german friend this is not the best choice. He would like to use the existing german version, but there seems to be no way to let the user select this version.
Adding a french translation would solve the problem for our french-german friend, but not for people speaking two other languages (such as italian and german), and I can not support my app with all languages spoken on this planet. Setting the default-language to German is also not optimal, because this would rise the same problem for people speaking french (an native language) and English (as second language).
So I think my app must have the possibility to manually select a language that is different from the pre-selected language. Adding a language-selection to the apps settings-panel ist not the problem. But how can i tell the system, that NSLocalizedString (@"text", @"comment");
should not access the systemwide selected language, but the in-app-selected language?
Localize-Swift - Swift friendly localization and i18n with in-app language switching
Its very simple and easy to change language manually. First of all you need to localize your app, then you can use below code to change language manually in your app.
Here's a ready-to-use and step-by-step guide on how to use Novarg's approach in Swift 3:
Step #1: Implement a language chooser
How to best do this is up to you and depends on the project. But use
to get a list of all your supported locales language codes programmatically. Also you can use
to present the language name in the apps current language.
As a complete example you could present a popover action sheet after clicking a button like this:
Step #2: Explain user what to do + Change language with restart
You might have noticed that the code in step #1 calls a method named
changeToLanguage(langCode:)
. That's what you should do, too when the user chooses a new language to change to, no matter how you designed your chooser. Here's its implementation, just copy it to your project:This will both ask and inform the user about if he wants to do the change and how to do it. Also it sets the apps language on the next start using:
Step #3 (optional): Localize the Strings
You might want to localize the strings like "Close now" by using the
NSLocalizedString
macro (or any other enhanced method).Real World Example
I'm using this exact implementation in an app targeted for iOS 10, I can confirm it works for me both on simulator and device. The app is actually open source, so you can find the above code distributed into different classes here.
My answer may be a matter of preference since I would disdain manual language selection in the app: you will have to override all system-provided buttons and make sure not to use them. It also adds another layer of complexity and may lead the user to confusion.
However, since this has to be an answer, I think your use case is solved without hacking language selection.
In iOS preferences, you may set additional languages:
Your example son of immigrants could have set French as main language and German as an additional language.
Then, when your app is localized to English and German, that young man's iPhone would pick German resources.
Would that solve the problem?
Simply add the following to the screen with language choice:
Then ask them to restart the app and the app will be in other language.
Hope it helps
In the meantime I did find a solution for my problem on myself:
I created a new class "LocalizeHelper":
Header LocalizeHelper.h
iMplementation LocalizeHelper.m
For each language you want to support you need a file named
Localizable.strings
. This works exactly as described in Apples documentation for localization. The only difference: Now you even can use languages like hindi or esperanto, that are not supported by Apple.To give you an example, here are the first lines of my english and german versions of Localizable.strings:
English
German
To use localizing, you must have some settings-routines in your app, and in the language-selection you call the macro:
After that you must enshure, that everything that was displayed in the old language, gets redrawn in the new language right now (hidden texts must be redrawn as soon as they get visible again).
To have localized texts available for every situation, you NEVER must write fix texts to the objects titles. ALWAYS use the macro
LocalizedString(keyword)
.don't:
do:
and have a "nice title" entry in every version of Localizable.strings!