赞
踩
重点是app 内部切换
1.配置languege.strings.infoPist.Strings
2.LocalizationTool.swift,实现内部切换
- //
- // LocalizationTool.swift
- // IFXY
- //
- // Created by LiuXing on 2017/3/7.
- // Copyright © 2017年 IFly. All rights reserved.
- //
-
- import UIKit
-
- class LocalizationTool: NSObject {
- static let shareInstance = LocalizationTool()
-
- let def = UserDefaults.standard
- var bundle : Bundle?
-
- func valueWithKey(key: String!) -> String {
- let bundle = LocalizationTool.shareInstance.bundle
- let str = bundle?.localizedString(forKey: key, value: nil, table: "Language")
- return str!
- }
- // class func valueWithKey(key: String!) -> String {
- //
- // let languageString = SynthesizerSettingTool.shareIntance.vocieParams?.languageType
- // let path = Bundle.main.path(forResource: languageString!, ofType: "lproj")
- // let value = (Bundle.init(path: path!)?.localizedString(forKey: key, value: nil, table: "Language"))!
- // return value
- // }
-
- func setLanguage(langeuage:String) {
- var str=langeuage
- if langeuage=="" || langeuage==nil{
-
- let languages:[String]=UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
- let str2:String=languages[0]
- if ((str2=="zh-Hans-CN")||(str2=="zh-Hans")){
- str="zh-Hans"
- }else{
- str="en"
- }
-
- }
- UserDefaults.standard.set(str, forKey: "langeuage")
- UserDefaults.standard.synchronize()
- let path = Bundle.main.path(forResource:str , ofType: "lproj")
- bundle = Bundle(path: path!)
- // def.set(langeuage, forKey: UserLanguage)
- // def.synchronize()
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "LanguageChanged"), object: nil)
-
-
- }
- }
- var languege:String=""
- // guard languege==UserDefaults.standard.value(forKey: "langeuage") as! String else{
- // languege=""
- // }
- if (UserDefaults.standard.value(forKey: "langeuage")) != nil {
- languege=UserDefaults.standard.value(forKey: "langeuage") as! String
- }
- LocalizationTool.shareInstance.setLanguage(langeuage: languege)
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-
- tableView.deselectRow(at: indexPath, animated: false)
- selectCol = indexPath.row
- tableView.reloadData()
- if indexPath.row == 0 {
-
- LocalizationTool.shareInstance.setLanguage(langeuage: "zh-Hans")
- } else {
-
- LocalizationTool.shareInstance.setLanguage(langeuage: "en")
- }
-
-
-
- }
点击切换语言之后,需要立即修改之前缓存的页面
NotificationCenter.default.addObserver(self, selector: #selector(changeLanguage), name: NSNotification.Name(rawValue: "LanguageChanged"), object: nil)
- func changeLanguage(){
- items = [["title": LocalizationTool.shareInstance.valueWithKey(key: "my_language_zh")],["title": LocalizationTool.shareInstance.valueWithKey(key: "trans_title_en")]]
- self.title = LocalizationTool.shareInstance.valueWithKey(key: "my_language_switch")//"切换语言"
-
- if (UserDefaults.standard.value(forKey: "langeuage") as! String) == "en"{ // 英语
- selectCol = 1
- } else {
- selectCol = 0
- }
- m_TableView.reloadData()
- }
- fileprivate lazy var datePicker:UIDatePicker = {[unowned self] in
- let datePic = UIDatePicker()
- datePic.backgroundColor = UIColor.white
- let languege=UserDefaults.standard.value(forKey: "langeuage") as! String
- var sexstr=UserAccountTool.shareIntance.account?.sex
- if languege=="en" {
- datePic.locale = Locale(identifier: "en")
-
- }else{
- datePic.locale = Locale(identifier: "zh_CN")
- }
- // datePic.locale = Locale(identifier: "zh_CN")
- return datePic
-
- }()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。