Swift中的本地化实现

1. 确保localization中添加了多语言
Swift中的本地化实现_第1张图片


2. 添加localisable.strings文件

Swift中的本地化实现_第2张图片


Swift中的本地化实现_第3张图片


3.选择这个文件,勾选多语言即可
Swift中的本地化实现_第4张图片


4.打开localisable.strings文件,添加一些测试字符串:
localisable.strings (base)
"LoginError" = "Error Login";
"UserNameEmpty" = "Username Empty";
"PasswordEmpty" = "Password Empty";
"GetUserInfoError" = "Error Getting User Info";


localisable.strings (english)
"LoginError" = "Error Login";
"UserNameEmpty" = "Username Empty";
"PasswordEmpty" = "Password Empty";
"GetUserInfoError" = "Error Getting User Info";




localisable.strings (chinese)
"LoginError" = "登录错误";
"UserNameEmpty" = "用户名不能为空";
"PasswordEmpty" = "密码不能为空";
"GetUserInfoError" = "获取用户信息错误";

5. 添加helper函数 :
 func localized() -> String{
        return NSLocalizedString(self, comment: "")
    }

使用:
"LoginError".localized();

在mainlayout中字符串的本地化:


...
/* Class = "UIButton"; normalTitle = "Button"; ObjectID = "oYr-T8-Q59"; */
"oYr-T8-Q59.normalTitle" = "登录";


/* Class = "UIButton"; normalTitle = "CHANGE PASSWORD"; ObjectID = "pzP-ri-GfN"; */
"pzP-ri-GfN.normalTitle" = "修改密码";
...


你可能感兴趣的:(Swift)