找回密码
 立即注册
查看: 5033|回复: 86

[学术/精华文章] Xcode增加多种本地化语言的方法

[复制链接]
发表于 2017-4-5 15:50 | 显示全部楼层 |阅读模式
最近为了使我的一个iOS Demo中系统预设控件显示的语言实现本地化,我便像往常一样去配置Localizations,然而却发现这个过程并不像往常那么顺利……
过去,我是这样添加的:
在PROJECT > Info > Localizations的Language列表中添加Chinese,如下图:


然而这次我却发现弹出的Resource File对话框中空空如也,如下图:


于是,问题应运而生了:为什么过去的本地化方式不好用了?而这些Resource File又出了什么事??翻了翻文档,不过没看太懂,推测应该是我的Project本地没有这些语言文件,所以加不进去。
Language IDs identify a language, dialect, or script and are used to name language-specific resource folders stored in the app bundle. Locale IDs identify a set of regional conventions and are used in APIs—such as the NSLocale, NSDateFormatter, NSNumberFormatter, and NSCalendar classes—where region information is needed to format data. OS X and iOS use standard language ID and locale ID formats that consist of language and region designators. For example, using a language combined with a region designator, a language ID can distinguish between different languages and regional dialects.
既然Xcode项目设置的图形界面不让改,那就去Info.plist看看:
于是发现了这货:


“我之前明明可以添加好几种语言的啊!”(摔)
稍安勿躁,咱们来看看这个键是干嘛的:


CFBundleDevelopmentRegion
CFBundleDevelopmentRegion (String - iOS, OS X) specifies the default language and region for the bundle, as a language ID. For example, English for the United Kingdom has the language ID en-UK. The system uses this value if it cannot locate a resource for the user’s preferred language.
该键为Bundle提供一个默认的语言和地区,当App的本地化列表中没有当前手机系统语言而系统又定位不到用户的首选语言顺序时将采用该语言。关于这个首选语言顺序(Preferred Language)见下图:


既然过去动刀的是Localizations,那么就看看Info.plist中有没有这个东西:


好了,既然都这么明显了,把想要适配的地区都添加进去就OK了!
最后删删改改试了试,发现如果只设置Localizations仍然符合需求。而且Info.plist默认给出的这个键Localization native development region以及Localizations都不是必须设置的项,如果他们都不被声明,你的iOS App仍然只支持英文。
结论
如果你的App需要支持多国语言,那么Localization native development region以及Localizations的设置很有必要。否则保证两者任意一条键中有中文即可。
至于国际化和本地化的一些更高阶用法,仍需后续补充。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2017-4-9 16:28 | 显示全部楼层
很不错
发表于 2017-4-9 16:11 | 显示全部楼层
楼主是超人
发表于 2017-4-9 16:35 | 显示全部楼层
顶顶多好
发表于 2017-4-9 16:34 | 显示全部楼层
说的非常好
发表于 2017-4-9 16:27 | 显示全部楼层
LZ真是人才
发表于 2017-4-11 10:59 | 显示全部楼层
楼主是超人
发表于 2017-4-11 11:09 | 显示全部楼层
好帖就是要顶
发表于 2017-4-11 10:55 | 显示全部楼层
难得一见的好帖
发表于 2017-4-11 11:19 | 显示全部楼层
说的非常好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-29 16:27 , Processed in 0.103598 second(s), 30 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表