美文网首页iOS DevelopmentIOS
swift----- 国际化之app内切换语言

swift----- 国际化之app内切换语言

作者: 71150ce14a00 | 来源:发表于2017-01-12 15:08 被阅读2836次

    �1. 首先创建个工程不说了。。。。。。。。

    2. 创建一个Strings File 命名为 Localizable.strings

    3.选择Localizable.strings文件 点击localizaiton 选择english

    4, 选择项目 info 添加语言

    选择 Chinese(simplified)为例, 去掉main.storyboard和LaunchScreen.storyboard选项

    发现Localizable.strings 多了两个文件,这就是多语言配置文件

    中文配置文件(注意后边要有分号)

    英文配置文件 (注意后边要有分号)

    接下来就代码啦~~~~~~~

    1.创建一个LanguageHelper.swift   ,这个类包含设置语言,获取语言,初始化

    import UIKit

    let UserLanguage = "UserLanguage"

    let AppleLanguages = "AppleLanguages"

    class LanguageHelper: NSObject {

    static let shareInstance = LanguageHelper()

    let def = UserDefaults.standard

    var bundle : Bundle?

    class func getString(key:String) -> String{

    let bundle = LanguageHelper.shareInstance.bundle

    let str = bundle?.localizedString(forKey: key, value: nil, table: nil)

    return str!

    }

    func initUserLanguage() {

    var string:String = def.value(forKey: UserLanguage) as! String? ?? ""

    if string == "" {

    let languages = def.object(forKey: AppleLanguages) as? NSArray

    if languages?.count != 0 {

    let current = languages?.object(at: 0) as? String

    if current != nil {

    string = current!

    def.set(current, forKey: UserLanguage)

    def.synchronize()

    }

    }

    }

    string = string.replacingOccurrences(of: "-CN", with: "")

    string = string.replacingOccurrences(of: "-US", with: "")

    var path = Bundle.main.path(forResource:string , ofType: "lproj")

    if path == nil {

    path = Bundle.main.path(forResource:"en" , ofType: "lproj")

    }

    bundle = Bundle(path: path!)

    }

    func setLanguage(langeuage:String) {

    let path = Bundle.main.path(forResource:langeuage , ofType: "lproj")

    bundle = Bundle(path: path!)

    def.set(langeuage, forKey: UserLanguage)

    def.synchronize()

    }

    }

    2.在appdelegate中第一次打开程序初始化程序语言

    LanguageHelper.shareInstance.initUserLanguage()

    3.添加修改语言通知

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.changeLanguage), name: NSNotification.Name(rawValue: "LanguageChanged"), object: nil)

    整页代码:自己研究去吧~~~~~~~

    import UIKit

    class ViewController: UIViewController {

    var lab: UILabel?

    override func viewDidLoad() {

    super.viewDidLoad()

    self.view.backgroundColor = UIColor.white

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.changeLanguage), name: NSNotification.Name(rawValue: "LanguageChanged"), object: nil)

    let button = UIButton(frame:CGRect(x:10, y:150, width:50, height:50))

    button.addTarget(self, action: #selector(ViewController.changelangage2), for: UIControlEvents.touchUpInside)

    self.view.addSubview(button)

    button.backgroundColor = UIColor.red

    let button2 = UIButton(frame:CGRect(x:100, y:150, width:100, height:30))

    button2.addTarget(self, action: #selector(ViewController.changelangage), for: UIControlEvents.touchUpInside)

    self.view.addSubview(button2)

    button2.backgroundColor = UIColor.green

    lab = UILabel.init(frame: .init(x: 10, y: 100, width: 300, height: 20))

    self.view.addSubview(lab!)

    //        lab.text = NSLocalizedString("GOOD_MORNING",comment:"default")

    lab?.text = LanguageHelper.getString(key: "GOOD_MORNING")

    }

    func changeLanguage() -> Void {

    lab?.text = LanguageHelper.getString(key: "GOOD_MORNING")

    }

    func changelangage() -> Void {

    LanguageHelper.shareInstance.setLanguage(langeuage: "en")

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LanguageChanged"), object: nil)

    }

    func changelangage2() -> Void {

    LanguageHelper.shareInstance.setLanguage(langeuage: "zh-Hans")

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LanguageChanged"), object: nil)

    }

    }

    代码传送门:https://github.com/dljike/LanguageChage

    相关文章

      网友评论

      • awindyseason:提个建议 改下代码:
        static let shareInstance : LanguageHelper = {
        let shared = LanguageHelper()
        var string:String = shared.def.value(forKey: UserLanguage) as! String? ?? ""

        if string == "" {

        let languages = shared.def.object(forKey: AppleLanguages) as? NSArray

        if languages?.count != 0 {

        let current = languages?.object(at: 0) as? String

        if current != nil {

        string = current!

        shared.def.set(current, forKey: UserLanguage)

        shared.def.synchronize()

        }

        }

        }

        string = string.replacingOccurrences(of: "-CN", with: "")

        string = string.replacingOccurrences(of: "-US", with: "")

        var path = Bundle.main.path(forResource:string , ofType: "lproj")

        if path == nil {

        path = Bundle.main.path(forResource:"en" , ofType: "lproj")

        }

        shared.bundle = Bundle(path: path!)

        // }
        return shared
        }()

        以上是对shared单例的懒加载
        // LanguageHelper.shareInstance.initUserLanguage() //该方法可以去掉 同理initUserLanguage() 方法可以去掉。

      本文标题:swift----- 国际化之app内切换语言

      本文链接:https://www.haomeiwen.com/subject/nnfhbttx.html