iOS國際化切換 for Swift

公司業(yè)務做到了國外,要做給印度尼西亞使用的APP,需要在APP內部直接切換語言,而不是根據系統(tǒng)語言而顯示不同的語言,經過一番調研做了一個簡單的Demo,分享學習成果的同時自己也做個總結。

國際慣例先上效果圖

效果圖

準備工作

  • command+N進入創(chuàng)建new files菜單,選擇Strings File創(chuàng)建。


    第一步創(chuàng)建strings文件
  • 選擇創(chuàng)建好的Customer.strings后,點擊localize


    第二步選擇Customer后,點擊localize
  • 這里點擊“+”創(chuàng)建需要的語言文件,這里筆者添加了4個,分別是英語、簡體中文、繁體中文、印尼語。


    點擊+添加所需要的語言

這里需要提醒一下,如何查看國家對應語言代碼,筆者在調研過程中各種搜索也無果,后自己發(fā)現 添加語言時()里即為國家對應語言代碼,后面寫代碼時會用到,具體如下圖。

()里為國家對應語言代碼

  • 然后再次選擇Customer.strings把需要添加的語言進行勾選


    勾選語言文件
  • 展開Customer.strings可以看到剛剛添加的四個語言文件,其中showText是展示文字,iamge是圖片名稱。

    語言文件

    之后分別在語言文件中填寫相同參數名的參數,例子:

    CustomerLaction.strings(Chinese(Traditionnal))對應
    "showText" = "這是壹個臺灣繁體";
    "image" = "zh-Hant";
    
    CustomerLaction.strings(English)對應
    "showText" = "this is a English Text";
    "image" = "en";
    
    CustomerLaction.strings(Indonesian)對應
    "showText" = "Ini adalah bahasa Indonesia.";
    "image" = "id";
    

代碼實現

創(chuàng)建CustomLanguage工具類,其中語言代碼如何查看上文已提到,如下:

class CustomLanguage: NSObject {
    //單例
    static let share = CustomLanguage()
    // 國家語言代碼
    var lan = ""
    
    func showText(key: String) -> String {
        // 查找對應國家語言代碼的路徑
        guard let path = Bundle.main.path(forResource: lan, ofType: "lproj") else { return "" }
        // 通過路徑在CustomLacation語言文件中查找對應key的字符串
        guard let bundle = Bundle(path: path)?.localizedString(forKey: key, value: nil, table: "CustomLacation") else { return "" }
        return bundle
    }
}

四個按鈕的點擊方法,其中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中下載源碼,記得點擊Stars哈,感謝。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容