iOS国际化方案

初始化多语言

在项目PROJECT -> info -> Localizations 添加你想适配的语言。


info.plist国际化

info.plist中包含的项目名称、权限申请相关内容需要国际化:

新建:InfoPlist.strings,选择要国际化的语言,如下图所示:

在InfoPlist.string 不同的语言里填上相应的内容:

1
2
3
4
5
6
7
8
"CFBundleName" = "";
"CFBundleDisplayName" = "";
"NSLocationWhenInUseUsageDescription" = "";
"NSCameraUsageDescription" = "";
"NSMicrophoneUsageDescription" = "";
"NSPhotoLibraryUsageDescription" = "";
"NSBluetoothPeripheralUsageDescription" = "";
"NSAppleMusicUsageDescription" = "";

关于info.plist中的字段描述可以参考这里:apple 文档

判断系统是否是中文

我们的项目里,中文(台湾,澳门,繁体)一律显示问简体中文,其他都显示为英文:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef enum : NSUInteger {
LANGUAGE_CN,
LANGUAGE_EN,
LANGUAGE_OTHER,
} SYSTEMLANGUAGE;
-(SYSTEMLANGUAGE)systemLn
{
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
if (StringContains(language, @"zh-Hant") ||
StringContains(language, @"zh-Hans")) {
return LANGUAGE_CN;
}
return LANGUAGE_EN;
}