One: internationalization of system permission prompt

  1. Create a new file in the project named InfoPlist. Strings

  2. Choose good locallzation

  3. Go to info.plist and selectshow Raw keysTo extract the key

  4. Write the key to InfoPlist. Strings:

    NSAppleMusicUsageDescription = "You need to access media data to store pictures or videos.Do you allow access (yes or no)";
    NSMicrophoneUsageDescription = "Use a microphone to capture sound during live broadcast,Are you allowed to turn on the microphone.";
    NSPhotoLibraryAddUsageDescription = "You need to visit the album to select a picture,Do you allow access.";
    Copy the code

Two: Internationalization of LaunchScreen

Most of the time we also need to internationalize the LaunchScreen by following these steps:

  1. Plist key: Launch screen interface file Base name value: LaunchScreen

  2. Create two new launchscrees named LaunchScreen_CH and LaunchScreen_EN

  3. Write to the corresponding language file in InfoPlist. Strings

    UILaunchStoryboardName = "LaunchScreen_EN"; UILaunchStoryboardName = "LaunchScreen_CH"; (in Chinese)Copy the code

The Launchscreens could have been internationalized by locallization, but there is a bug in Apple that locallization is only displayed in English, so I chose to create two Launchscreens this way

Three: get the system’s current language

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
Copy the code

Four: multi-language loading of MJRefresh

Adding multiple languages to MJRefresh can be modified according to specific business requirements, and this method can be searched globally:

+ (NSString *)mj_localizedStringForKey:(NSString *)key value:(NSString *)value
Copy the code

Five: Set the APP language tool class

# import <Foundation/Foundation.h>

@interface NSBundle (Language)

+ (void)setLanguage:(NSString *)language;

@end
Copy the code
#import "NSBundle+Language.h"
#import <objc/runtime.h>

static const char _bundle = 0;

@interface BundleEx : NSBundle

@end

@implementation BundleEx

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName {
    NSBundle *bundle = objc_getAssociatedObject(self, &_bundle);
    return bundle ? [bundle localizedStringForKey:key value:value table:tableName] : [super localizedStringForKey:key value:value table:tableName];
}

@end

@implementation NSBundle (Language)

+ (void)setLanguage:(NSString *)language {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        object_setClass([NSBundle mainBundle], [BundleEx class]);
    });
    
    objc_setAssociatedObject([NSBundle mainBundle], &_bundle, language ? [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]] : nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
Copy the code

Usage:

[NSBundle setLanguage:@”zh-Hans”];