Swift中的本地化实现

前端之家收集整理的这篇文章主要介绍了Swift中的本地化实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1. 确保localization中添加了多语言



2. 添加localisable.strings文件





3.选择这个文件,勾选多语言即可



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" = "修改密码";
...
原文链接:https://www.f2er.com/swift/321424.html

猜你在找的Swift相关文章