公司業(yè)務(wù)做到了國(guó)外,要做給印度尼西亞使用的APP,需要在APP內(nèi)部直接切換語(yǔ)言蜡吧,而不是根據(jù)系統(tǒng)語(yǔ)言而顯示不同的語(yǔ)言,經(jīng)過(guò)一番調(diào)研做了一個(gè)簡(jiǎn)單的Demo占键,分享學(xué)習(xí)成果的同時(shí)自己也做個(gè)總結(jié)昔善。
國(guó)際慣例先上效果圖
準(zhǔn)備工作
-
command+N進(jìn)入創(chuàng)建new files菜單,選擇Strings File創(chuàng)建畔乙。
-
選擇創(chuàng)建好的Customer.strings后君仆,點(diǎn)擊localize
-
這里點(diǎn)擊“+”創(chuàng)建需要的語(yǔ)言文件啸澡,這里筆者添加了4個(gè)袖订,分別是英語(yǔ)氮帐、簡(jiǎn)體中文嗅虏、繁體中文、印尼語(yǔ)上沐。
這里需要提醒一下皮服,如何查看國(guó)家對(duì)應(yīng)語(yǔ)言代碼,筆者在調(diào)研過(guò)程中各種搜索也無(wú)果参咙,后自己發(fā)現(xiàn) 添加語(yǔ)言時(shí)()里即為國(guó)家對(duì)應(yīng)語(yǔ)言代碼龄广,后面寫代碼時(shí)會(huì)用到,具體如下圖蕴侧。
-
然后再次選擇Customer.strings把需要添加的語(yǔ)言進(jìn)行勾選
-
展開(kāi)Customer.strings可以看到剛剛添加的四個(gè)語(yǔ)言文件择同,其中
showText
是展示文字,iamge
是圖片名稱净宵。
之后分別在語(yǔ)言文件中填寫相同參數(shù)名的參數(shù)敲才,例子:CustomerLaction.strings(Chinese(Traditionnal))對(duì)應(yīng) "showText" = "這是壹個(gè)臺(tái)灣繁體"; "image" = "zh-Hant"; CustomerLaction.strings(English)對(duì)應(yīng) "showText" = "this is a English Text"; "image" = "en"; CustomerLaction.strings(Indonesian)對(duì)應(yīng) "showText" = "Ini adalah bahasa Indonesia."; "image" = "id";
代碼實(shí)現(xiàn)
創(chuàng)建CustomLanguage工具類裹纳,其中語(yǔ)言代碼如何查看上文已提到,如下:
class CustomLanguage: NSObject {
//單例
static let share = CustomLanguage()
// 國(guó)家語(yǔ)言代碼
var lan = ""
func showText(key: String) -> String {
// 查找對(duì)應(yīng)國(guó)家語(yǔ)言代碼的路徑
guard let path = Bundle.main.path(forResource: lan, ofType: "lproj") else { return "" }
// 通過(guò)路徑在CustomLacation語(yǔ)言文件中查找對(duì)應(yīng)key的字符串
guard let bundle = Bundle(path: path)?.localizedString(forKey: key, value: nil, table: "CustomLacation") else { return "" }
return bundle
}
}
四個(gè)按鈕的點(diǎn)擊方法紧武,其中setAttributedWithImage
方法是封裝的富文本類別剃氧,可在github中下載源碼查看。
@objc func englishBtnClick() {
CustomLanguage.share.lan = "en"
label.attributedText = CustomLanguage.share.showText(key: "showText").setAttributedWithImage(subText: CustomLanguage.share.showText(key: "showText"), subColor: #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1), image: UIImage(named: CustomLanguage.share.showText(key: "image"))!)
}
@objc func chineseSimpleBtnClick() {
CustomLanguage.share.lan = "zh-Hans"
label.attributedText = CustomLanguage.share.showText(key: "showText").setAttributedWithImage(subText: CustomLanguage.share.showText(key: "showText"), subColor: #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1), image: UIImage(named: CustomLanguage.share.showText(key: "image"))!)
}
@objc func chineseTWBtnClick() {
CustomLanguage.share.lan = "zh-Hant"
label.attributedText = CustomLanguage.share.showText(key: "showText").setAttributedWithImage(subText: CustomLanguage.share.showText(key: "showText"), subColor: #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1), image: UIImage(named: CustomLanguage.share.showText(key: "image"))!)
}
@objc func indonesiaBtnClick() {
CustomLanguage.share.lan = "id"
label.attributedText = CustomLanguage.share.showText(key: "showText").setAttributedWithImage(subText: CustomLanguage.share.showText(key: "showText"), subColor: #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1), image: UIImage(named: CustomLanguage.share.showText(key: "image"))!)
}
如果感興趣可以到我的github中下載源碼阻星,記得點(diǎn)擊Stars
哈朋鞍,感謝。