Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

manual language selection in an iOS-App (iPhone and iPad)

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 (as 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?

like image 379
Hubert Schölnast Avatar asked Mar 30 '12 08:03

Hubert Schölnast


People also ask

How do I change the language of iOS apps?

Change The App Language in iPhone or iPad AppsTap on the Settings app on the Home screen. Scroll down, select the app you wish to change its language. Select Language under Preferred Language. Choose the language you want to use.

How do I change the language of an app on my iPad?

To do so, head to Settings > General > Language & Region. Tap “Other Languages” and add a language you want to use. This is also where you can set your overall system language on your iPhone or iPad.

How iOS determines the language for your app?

iOS uses files with the . strings file extension to store all of the localized strings used within the app, one or more for each supported language. A simple function call will retrieve the requested string based on the current language in use on the device.


2 Answers

In the meantime I did find a solution for my problem on myself:

I created a new class "LocalizeHelper":


Header LocalizeHelper.h

//LocalizeHelper.h  #import <Foundation/Foundation.h>  // some macros (optional, but makes life easy)  // Use "LocalizedString(key)" the same way you would use "NSLocalizedString(key,comment)" #define LocalizedString(key) [[LocalizeHelper sharedLocalSystem] localizedStringForKey:(key)]  // "language" can be (for american english): "en", "en-US", "english". Analogous for other languages. #define LocalizationSetLanguage(language) [[LocalizeHelper sharedLocalSystem] setLanguage:(language)]  @interface LocalizeHelper : NSObject  // a singleton: + (LocalizeHelper*) sharedLocalSystem;  // this gets the string localized: - (NSString*) localizedStringForKey:(NSString*) key;  //set a new language: - (void) setLanguage:(NSString*) lang;                @end 

iMplementation LocalizeHelper.m

// LocalizeHelper.m #import "LocalizeHelper.h"  // Singleton static LocalizeHelper* SingleLocalSystem = nil;  // my Bundle (not the main bundle!) static NSBundle* myBundle = nil;   @implementation LocalizeHelper   //------------------------------------------------------------- // allways return the same singleton //------------------------------------------------------------- + (LocalizeHelper*) sharedLocalSystem {     // lazy instantiation     if (SingleLocalSystem == nil) {         SingleLocalSystem = [[LocalizeHelper alloc] init];     }     return SingleLocalSystem; }   //------------------------------------------------------------- // initiating //------------------------------------------------------------- - (id) init {     self = [super init];     if (self) {         // use systems main bundle as default bundle         myBundle = [NSBundle mainBundle];     }     return self; }   //------------------------------------------------------------- // translate a string //------------------------------------------------------------- // you can use this macro: // LocalizedString(@"Text"); - (NSString*) localizedStringForKey:(NSString*) key {     // this is almost exactly what is done when calling the macro NSLocalizedString(@"Text",@"comment")     // the difference is: here we do not use the systems main bundle, but a bundle     // we selected manually before (see "setLanguage")     return [myBundle localizedStringForKey:key value:@"" table:nil]; }   //------------------------------------------------------------- // set a new language //------------------------------------------------------------- // you can use this macro: // LocalizationSetLanguage(@"German") or LocalizationSetLanguage(@"de"); - (void) setLanguage:(NSString*) lang {      // path to this languages bundle     NSString *path = [[NSBundle mainBundle] pathForResource:lang ofType:@"lproj" ];     if (path == nil) {         // there is no bundle for that language         // use main bundle instead         myBundle = [NSBundle mainBundle];     } else {          // use this bundle as my bundle from now on:         myBundle = [NSBundle bundleWithPath:path];          // to be absolutely shure (this is probably unnecessary):         if (myBundle == nil) {             myBundle = [NSBundle mainBundle];         }     } }   @end 

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

/* English - English */  /* for debugging */ "languageOfBundle" = "English - English";  /* Header-Title of the Table displaying all lists and projects */ "summary" = "Summary";  /* Section-Titles in table "summary" */ "help" = "Help"; "lists" = "Lists"; "projects" = "Projects"; "listTemplates" = "List Templates"; "projectTemplates" = "Project Templates"; 

German

/* German - Deutsch */  /* for debugging */ "languageOfBundle" = "German - Deutsch";  /* Header-Title of the Table displaying all lists and projects */ "summary" = "Überblick";  /* Section-Titles in table "summary" */ "help" = "Hilfe"; "lists" = "Listen"; "projects" = "Projekte"; "listTemplates" = "Vorlagen für Listen"; "projectTemplates" = "Vorlagen für Projekte"; 

To use localizing, you must have some settings-routines in your app, and in the language-selection you call the macro:

LocalizationSetLanguage(selectedLanguage); 

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:

cell.textLabel.text = @"nice title"; 

do:

cell.textLabel.text = LocalizedString(@"nice title"); 

and have a "nice title" entry in every version of Localizable.strings!

like image 170
Hubert Schölnast Avatar answered Oct 22 '22 18:10

Hubert Schölnast


Simply add the following to the screen with language choice:

    NSString *tempValue = //user chosen language. Can be picker view/button/segmented control/whatever. Just get the text out of it     NSString *currentLanguage = @"";     if ([tempValue rangeOfString:NSLocalizedString(@"English", nil)].location != NSNotFound) {         currentLanguage = @"en";     } else if ([tempValue rangeOfString:NSLocalizedString(@"German", nil)].location != NSNotFound) {         currentLanguage = @"de";     } else if ([tempValue rangeOfString:NSLocalizedString(@"Russian", nil)].location != NSNotFound) {         currentLanguage = @"ru";     }     [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:currentLanguage, nil] forKey:@"AppleLanguages"];     [[NSUserDefaults standardUserDefaults]synchronize]; 

Then ask them to restart the app and the app will be in other language.

Hope it helps

like image 25
Novarg Avatar answered Oct 22 '22 18:10

Novarg