Is it possible to "redirect" localizations on iOS?

124 views Asked by At

What I want is simple:

My app has only two languages: English and Simplified Chinese. And I hope that if the system language is Simplified or Traditional Chinese, then Simplified Chinese is used; otherwise English is used.

It seems that most user don't know they can set their secondary preferred language. So I cannot rely on them.

2

There are 2 answers

2
Mutawe On BEST ANSWER

You can use this code to retrieve the user language and set the app language:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Reset system defaults to get the complete language list.
[defaults removeObjectForKey:@"AppleLanguages"];
// Default language choosen by user.
NSString *defLanguage = [defaults objectForKey:@"Language"];
NSArray *sysLangugages = [defaults arrayForKey:@"AppleLanguages"];
// System default language: first element of array.
NSString *sysLanguage = [sysLangugages objectAtIndex:0];
NSArray *array = [NSArray arrayWithObjects:defLanguage, sysLanguage, nil];
LocalizationSetLanguage(defLanguage);
[defaults setObject:array forKey:@"AppleLanguages"];

 //To set and get the saved language
 // in the .h
#define LocalizationSetLanguage(language) \
[[LocalizationSystem sharedLocalSystem] setLanguage:(language)]

//in the .m
- (void) setLanguage:(NSString*) language
{

    lang = language;
}

- (NSString*) getLanguage
{

    if (!lang) {
        NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];

        NSString *preferredLang = [languages objectAtIndex:0];
        lang = preferredLang;
    }

    return lang;
}
0
chrios On

Use AMLocalizedString, you can use this to set your local Language.