赞
踩
INTERNATIONALIZATION = "Internationalization";
CHANGE = "switch";
HANDSOME = "Handsome Boy";
INTERNATIONALIZATION = "国际化";
CHANGE = "切换";
HANDSOME = "帅哥";
INTERNATIONALIZATION = "國際化";
CHANGE = "切換";
HANDSOME = "帥哥";
该工具类可以直接拖入工程中使用, 主要实现功能:
import UIKit class YDWLocalizationHelper: NSObject { fileprivate static let kSharedSettingsKey = "DefaultUserSettings" var language: Language enum Language: String { case Chinese = "zh-Hans" case English = "en" case Tradition = "zh-Hant" case Vietnamese = "vi" var code: String { return rawValue } } // MARK: 创建单例 static let shared: YDWLocalizationHelper = { let appSettings: YDWLocalizationHelper if let savedData = UserDefaults.standard.object(forKey: YDWLocalizationHelper.kSharedSettingsKey) as? Data, let defaultSettings = NSKeyedUnarchiver.unarchiveObject(with: savedData) as? YDWLocalizationHelper { appSettings = defaultSettings } else { appSettings = YDWLocalizationHelper() } return appSettings }() // MARK: 获取键值 func valueWithKey(key: String!) -> String { let str = NSLocalizedString(key, comment: "") return str } // MARK: - 保存用户当前语言 static func saveSharedInstance() { let data = NSKeyedArchiver.archivedData(withRootObject: YDWLocalizationHelper.shared) UserDefaults.standard.set(data, forKey: YDWLocalizationHelper.kSharedSettingsKey) } static func localeIsChinese() -> Language { if let lang = Locale.preferredLanguages.first { if lang.hasPrefix("zh") { if lang.contains("Hant") || lang.contains("Trad") { return .Tradition } return .Chinese } else if lang.contains("en") { return .English } else if lang.hasPrefix("viet") { return .Vietnamese } else { return .English } } else { return .English } } // MARK: 首次进入语言 override init() { language = YDWLocalizationHelper.localeIsChinese() super.init() } } private var bundleByLanguageCode: [String: Foundation.Bundle] = [:] extension YDWLocalizationHelper.Language { var bundle: Foundation.Bundle? { if let bundle = bundleByLanguageCode[code] { return bundle } else { let mainBundle = Foundation.Bundle.main if let path = mainBundle.path(forResource: code, ofType: "lproj"), let bundle = Foundation.Bundle(path: path) { bundleByLanguageCode[code] = bundle return bundle } else { return nil } } } } // 当调用YDWLocalizationHelper.Language切换语言后替换掉mainBundle为当前语言的bundle class Bundle: Foundation.Bundle { override func localizedString(forKey key: String, value: String?, table tableName: String?) -> String { if let bundle = YDWLocalizationHelper.shared.language.bundle { return bundle.localizedString(forKey: key, value: value, table: tableName) } else { return super.localizedString(forKey: key, value: value, table: tableName) } } }
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
object_setClass(Foundation.Bundle.main, Bundle.self)
return true
}
// comment 是传参, 假如value中需要外界来决定内容,可以用%@,然后comment传参
func valueWithKey(key: String!) -> String {
let str = NSLocalizedString(key, comment: "")
return str
}
// 切换语言时,只要设置就可以:
YDWLocalizationHelper.shared.language = .Chinese // .Chinese
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.setupRootViewController()
func resetRootViewController() {
if let appdelegate = UIApplication.shared.delegate {
let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
if let mainController = storyBoard.instantiateViewController(withIdentifier: "rootViewController") as? UINavigationController {
appdelegate.window??.rootViewController = mainController
}
}
}
对于所有界面的刷新最方便的就是重新设置 rootViewController, 将keyWindow先变黑,假装loading个几秒,再变回来即可。如果需要再次回到之前所在页面,再添加相应的跳转VC的方法;
func chooseLanguage(menu: [String : Any]) { let language = menu["language"] as! String var noticeContent = "正在设置语言..." if language == "中文(简体)" { noticeContent = "正在设置语言..." YDWLocalizationHelper.shared.language = .Chinese } else if language == "中文(繁體)" { noticeContent = "正在設置語言..." YDWLocalizationHelper.shared.language = .Tradition } else if language == "English" { noticeContent = "Setting Language..." YDWLocalizationHelper.shared.language = .English } else if language == "Tiếng Việt" { noticeContent = "Thiết lập ngôn ngữ..." YDWLocalizationHelper.shared.language = .Vietnamese } else { noticeContent = "正在设置语言..." YDWLocalizationHelper.shared.language = .Chinese } if UIApplication.shared.delegate != nil { let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.setupRootViewController() } let backView = UIView(frame: UIScreen.main.bounds) backView.backgroundColor = .black UIApplication.shared.keyWindow?.addSubview(backView) YDWToast.showToast(text: noticeContent, duration: 2, location: .center) DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) { UIView.animate(withDuration: 0.2, animations: { backView.alpha = 0 }) { (isFinished) in backView.removeFromSuperview() } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。