新建一个工程
LocalizationsDemo

设置 Application has localized display name
打开 Info.plist 文件,添加 Application has localized display name,并设置为 YES:

设置 Localizations
打开 LocalizationsDemo -> PROJECT -> Info -> Localizations -> +,点击 “+” 添加所需语言:

先添加一个中文简体的,点击“Finish”:

添加完成后会看到文件的变化:

添加 InfoPlist.strings 文件
新建 Strings File 文件(快捷键 command + N):

选中“Resource”下面的“Strings File”,然后点击“Next”:

命名为“InfoPlist”(注意:必须是 InfoPlist,别的没试过),点击“Create”:

InfoPlist.strings 文件创建完成:

点击右侧面板的“Localize”按钮,再点击弹框的“Localize”按钮:

右侧面板的 Localization 会多出一个选项:

选中“Chinese”选项,左侧面板会自动添加一个“InfoPlist.strings(Chinese (Simplifield))”文件:

配置 InfoPlist.strings 文件
分别在 InfoPlist.strings(English) 和 InfoPlist.strings(Chinese (Simplifield)) 文件中添加以下内容:
-
English:
CFBundleDisplayName = "APP Name";
-
Chinese:
CFBundleDisplayName = "APP 名字";


其他可配置的内容:
- English:
NSCameraUsageDescription = "Use your camera to scan the qr code and update the avatar";
NSPhotoLibraryUsageDescription = "Use your photo album for qr code recognition and update avatar";
NSContactsUsageDescription = "Find contacts you might know";
NSFaceIDUsageDescription = "Use Face ID to login";
NSCalendarsUsageDescription = "Use your calendar to get dates.";
NSMicrophoneUsageDescription = "Use your microphone for voice playback";
NSHealthShareUsageDescription = "Use your health data for data statistics";
NSHealthUpdateUsageDescription = "Use your health data for data statistics";
NSPhotoLibraryAddUsageDescription = "Use your photo album to add photo";
NSLocationAlwaysUsageDescription = "Use your location data for data statistics";
NSLocationWhenInUseUsageDescription = "Use your location data for data statistics";
- Chinese:
NSCameraUsageDescription = "使用您相机进行扫描二维码以及更新头像";
NSPhotoLibraryUsageDescription = "使用您的相册进行二维码识别以及更新头像";
NSContactsUsageDescription = "使用您的相册通讯录查找可能认识的人";
NSFaceIDUsageDescription = "使用Face ID解锁应用";
NSCalendarsUsageDescription = "使用您的日历获取日期";
NSMicrophoneUsageDescription = "使用您的麦克风进行语音播放";
NSHealthShareUsageDescription = "使用您的健康数据进行统计";
NSHealthUpdateUsageDescription = "使用您的健康数据进行统计";
NSPhotoLibraryAddUsageDescription = "使用您的相册添加图片";
NSLocationAlwaysUsageDescription = "使用您的位置进行数据统计";
NSLocationWhenInUseUsageDescription = "使用您的位置进行数据统计";
添加 Localizable.strings 文件
新建 Strings File 文件(快捷键 command + N),选中“Resource”下面的“Strings File”,然后点击“Next”:

命名为“Localizable”(注意:非必须是 Localizable,后面可以自己设置),点击“Create”:

Localizable.strings 文件创建完成后,点击右侧面板的“Localize”按钮,再点击弹框的“Localize”按钮:

选中“Chinese”选项,左侧面板会自动添加一个“Localizable.strings(Chinese (Simplifield))”文件:

配置 Localizable.strings 文件
分别添加以下内容:
-
English:
test_text = "Test Text";
-
Chinese:
test_text = "测试文本";

添加代码测试
在 ViewController.m
文件里添加以下代码:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
label.text = NSLocalizedString(@"test_text", nil);
[self.view addSubview:label];
运行模拟器:
- 设置系统语言为英文:


- 设置系统语言为中文:


系统语言设置








总结
欢迎留言讨论,有错误请指出,谢谢!
【联系我(QQ:3500229193)或者加入社群,请戳这里!】
更新日志
日期 | 次数 | 备注 |
---|---|---|
2019.02.26 | 1 |
网友评论