主页 Some Tips for iOS Language Localization/Internationalization
Post
Cancel

Some Tips for iOS Language Localization/Internationalization

Getting the internationalized language array via code

Getting the language currently used by the app

1
2
3
    NSArray *langArr1 = [[NSUserDefaults standardUserDefaults] valueForKey:@"AppleLanguages"];
    NSString *language1 = langArr1.firstObject;
    NSLog(@"模拟器语言:%@",language1);

Switching languages: en stands for English, zh-Hans for Simplified Chinese, zh-Hant for Traditional Chinese.

1
2
    NSArray *lans = @[@"en"];
    [[NSUserDefaults standardUserDefaults] setObject:lans forKey:@"AppleLanguages"];

Switching the launch language by modifying the scheme –

Figure 1

Figure 2

-AppleLanguages (zh-Hans) stands for Simplified Chinese
-AppleLanguages (zh-Hant) stands for Traditional Chinese
-AppleLanguages (en) stands for English
You can figure out the others yourself. Pay attention to the space.

Getting different localized images via code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //xxx is the localized image name, e.g. xxx.png 
    //if the image is xxx.jpg, replace xxx with xxx.jpg
    NSString *imageName = NSLocalizedString(@"xxx", nil); 
    self.imageView.image = [UIImage imageNamed:imageName];
}

@end

Here is a demo I wrote.
It mainly covers the following:

  1. Project name configuration: plist internationalization
  2. String internationalization
  3. Custom string internationalization
  4. Image internationalization

Reference: VV木公子

End of article

该博客文章由作者通过 CC BY 4.0 进行授权。