Change app language in iOS without restarting the app

后端 未结 6 807
时光取名叫无心
时光取名叫无心 2020-12-08 07:33

I have seems some apps can change the language internally within the app without the need of restarting the app, I am wondering how they are implemented.

For example

6条回答
  •  生来不讨喜
    2020-12-08 08:00

    You need to load another bundle like this(where @"en" could be locale you need):

    NSString *path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
    NSBundle *languageBundle = [NSBundle bundleWithPath:path];
    

    and make macros/function like NSLocalizedString which use your loaded bundle or use methods on that bundle directly like this

    [languageBundle localizedStringForKey:key value:value table:tableName];
    

    [[NSBundle mainBundle] localizations] lists all app localizations(including "Base").

    Also I wrote helper class which does this(note that it has ReactiveCocoa as a dependency). It allows language change without app restart and sends current locale each time it's changed.

提交回复
热议问题