當(dāng)你的應(yīng)用需要適配多語言時(shí)毅待,需要將app名稱和內(nèi)部文字改成其他語言,現(xiàn)在主要是中英文轉(zhuǎn)換归榕,廢話不多說恩静,下面開始實(shí)戰(zhàn)。
- 一蹲坷、app名稱隨系統(tǒng)切換
1.創(chuàng)建InfoPlist.strings
文件,右側(cè)選項(xiàng)
之后選擇中英文(Base無需選擇)
在English中CFBundleDisplayName="FlowerField";
FlowerField是app的英文名
在Chinese中CFBundleDisplayName="花田小憩"
;花田小憩是app的中文名
創(chuàng)建之后的效果圖:
系統(tǒng)中文效果:
系統(tǒng)英文效果:
- 二驶乾、內(nèi)部應(yīng)用文字隨系統(tǒng)切換
1.創(chuàng)建Localizable.strings
文件,如下圖:(Base文件可以不用創(chuàng)建)
2.在strings文件中添加要替換的文字
English中
Chinese中
3.代碼中實(shí)現(xiàn)
在標(biāo)簽控制器UITabBarController
中(此次演示用的是Swift)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
setup()
}```
func setup () {
//設(shè)置TabBar的tintColor
tabBar.tintColor = UIColor.black
addViewController(HomeTableViewController(), title: NSLocalizedString("tab_theme", comment: ""))
addViewController(MallsTableViewController(), title: NSLocalizedString("tab_malls", comment: ""))
addViewController(profile, title: NSLocalizedString("tab_profile", comment: ""))
}
//添加子控制器
private func addViewController(_ childController:UIViewController,title:String) {
let nav = UINavigationController(rootViewController: childController)
addChildViewController(nav)
childController.tabBarItem.title = title
childController.tabBarItem.image = UIImage(named: "tb_\(childViewControllers.count - 1)" )
childController.tabBarItem.selectedImage = UIImage(named: "tb_\(childViewControllers.count - 1)" + "_selected")
}
其中需要使用的方法是```NSLocalizedString(key, comment) ```
其中key是Localizable.strings這個(gè)文件中讀取對(duì)應(yīng)的key-value值循签,comment可以為nil级乐,空的字符串,也可以是對(duì)key的注釋县匠。
這樣就設(shè)置完成了风科,系統(tǒng)英文狀態(tài)下:
![Snip20170409_4.png](http://upload-images.jianshu.io/upload_images/2675547-22b944aba11bffec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
中文狀態(tài)下:
![Snip20170409_5.png](http://upload-images.jianshu.io/upload_images/2675547-020e48d6c81ba717.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 三、注意點(diǎn)strings文件名稱一定要寫對(duì)乞旦,因?yàn)閄code就是根據(jù)文件名稱來尋找的贼穆,寫錯(cuò)了就找不到了。