当前位置:   article > 正文

swift 国际化,app内部中英语言切换_swift app 怎么区分国内外

swift app 怎么区分国内外

重点是app 内部切换

1.配置languege.strings.infoPist.Strings












2.LocalizationTool.swift,实现内部切换

  1. //
  2. // LocalizationTool.swift
  3. // IFXY
  4. //
  5. // Created by LiuXing on 2017/3/7.
  6. // Copyright © 2017年 IFly. All rights reserved.
  7. //
  8. import UIKit
  9. class LocalizationTool: NSObject {
  10. static let shareInstance = LocalizationTool()
  11. let def = UserDefaults.standard
  12. var bundle : Bundle?
  13. func valueWithKey(key: String!) -> String {
  14. let bundle = LocalizationTool.shareInstance.bundle
  15. let str = bundle?.localizedString(forKey: key, value: nil, table: "Language")
  16. return str!
  17. }
  18. // class func valueWithKey(key: String!) -> String {
  19. //
  20. // let languageString = SynthesizerSettingTool.shareIntance.vocieParams?.languageType
  21. // let path = Bundle.main.path(forResource: languageString!, ofType: "lproj")
  22. // let value = (Bundle.init(path: path!)?.localizedString(forKey: key, value: nil, table: "Language"))!
  23. // return value
  24. // }
  25. func setLanguage(langeuage:String) {
  26. var str=langeuage
  27. if langeuage=="" || langeuage==nil{
  28. let languages:[String]=UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
  29. let str2:String=languages[0]
  30. if ((str2=="zh-Hans-CN")||(str2=="zh-Hans")){
  31. str="zh-Hans"
  32. }else{
  33. str="en"
  34. }
  35. }
  36. UserDefaults.standard.set(str, forKey: "langeuage")
  37. UserDefaults.standard.synchronize()
  38. let path = Bundle.main.path(forResource:str , ofType: "lproj")
  39. bundle = Bundle(path: path!)
  40. // def.set(langeuage, forKey: UserLanguage)
  41. // def.synchronize()
  42. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LanguageChanged"), object: nil)
  43. }
  44. }

3.在appdelegate里面     

  1. var languege:String=""
  2. // guard languege==UserDefaults.standard.value(forKey: "langeuage") as! String else{
  3. // languege=""
  4. // }
  5. if (UserDefaults.standard.value(forKey: "langeuage")) != nil {
  6. languege=UserDefaults.standard.value(forKey: "langeuage") as! String
  7. }
  8. LocalizationTool.shareInstance.setLanguage(langeuage: languege)

4.语言切换界面



  1. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  2. tableView.deselectRow(at: indexPath, animated: false)
  3. selectCol = indexPath.row
  4. tableView.reloadData()
  5. if indexPath.row == 0 {
  6. LocalizationTool.shareInstance.setLanguage(langeuage: "zh-Hans")
  7. } else {
  8. LocalizationTool.shareInstance.setLanguage(langeuage: "en")
  9. }
  10. }
点击切换语言之后,需要立即修改之前缓存的页面

 

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

页面所有的viewdidload方法里面加上监听

 

  1. func changeLanguage(){
  2. items = [["title": LocalizationTool.shareInstance.valueWithKey(key: "my_language_zh")],["title": LocalizationTool.shareInstance.valueWithKey(key: "trans_title_en")]]
  3. self.title = LocalizationTool.shareInstance.valueWithKey(key: "my_language_switch")//"切换语言"
  4. if (UserDefaults.standard.value(forKey: "langeuage") as! String) == "en"{ // 英语
  5. selectCol = 1
  6. } else {
  7. selectCol = 0
  8. }
  9. m_TableView.reloadData()
  10. }

tip:日期选择器

  1. fileprivate lazy var datePicker:UIDatePicker = {[unowned self] in
  2. let datePic = UIDatePicker()
  3. datePic.backgroundColor = UIColor.white
  4. let languege=UserDefaults.standard.value(forKey: "langeuage") as! String
  5. var sexstr=UserAccountTool.shareIntance.account?.sex
  6. if languege=="en" {
  7.           datePic.locale = Locale(identifier: "en")
  8.  }else{
  9. datePic.locale = Locale(identifier: "zh_CN")
  10. }
  11. // datePic.locale = Locale(identifier: "zh_CN")
  12. return datePic
  13. }()


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/942989
推荐阅读
相关标签
  

闽ICP备14008679号