赞
踩
title = NSLocalizedString("equipment", comment: "")
iOS(swift)storyboard的国际化(本地化,全球化,多语言)
import UIKit enum LanguageType { case chinese,english,auto } class LocalizableManager: NSObject { static func localValue(_ str:String) -> String { LocalizableManager.shared.localValue(str: str) } static func setLanguage(_ type:LanguageType){ LocalizableManager.shared.setLanguage(type) } //单例 static let shared = LocalizableManager() private override init() { } var bundle:Bundle = Bundle.main private func localValue(str:String) -> String{ //table参数值传nil也是可以的,传nil系统就会默认为Localizable bundle.localizedString(forKey: str, value: nil, table: "Localizable") } private func setLanguage(_ type:LanguageType){ var typeStr = "" switch type { case .chinese: typeStr = "zh-Hans" UserDefaults.standard.setValue("zh-Hans", forKey: "language") case .english: typeStr = "en" UserDefaults.standard.setValue("en", forKey: "language") default: break } //返回项目中 en.lproj 文件的路径 let path = Bundle.main.path(forResource: typeStr, ofType: "lproj") //用这个路径生成新的bundle bundle = Bundle(path: path!)! if type == .auto { //和系统语言一致 bundle = Bundle.main UserDefaults.standard.removeObject(forKey: "language") } NotificationCenter.default.post(name: NSNotification.Name(rawValue: "languageChanged"), object: nil) } }
import UIKit class ViewController: UIViewController { @IBOutlet var languageLabel: UILabel! @IBOutlet var chineseBtn: UIButton! @IBOutlet var englishBtn: UIButton! @IBOutlet var autoBtn: UIButton! func setupUI(){ languageLabel.text = LocalizableManager.localValue("english") chineseBtn.setTitle(LocalizableManager.localValue("chinese"), for: .normal) englishBtn.setTitle(LocalizableManager.localValue("english"), for: .normal) autoBtn.setTitle(LocalizableManager.localValue("auto"), for: .normal) } @IBAction func chineseBtnClick(_ sender: UIButton) { LocalizableManager.setLanguage(.chinese) } @IBAction func englishBtnClick(_ sender: Any) { LocalizableManager.setLanguage(.english) } @IBAction func autoBtnClick(_ sender: Any) { LocalizableManager.setLanguage(.auto) } override func viewDidLoad() { super.viewDidLoad() setupUI() NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "languageChanged"), object: nil, queue: nil) { (notification) in self.setupUI() } } }
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. if let languageType = UserDefaults.standard.value(forKey: "language"){ print("进来") print(languageType) let type = languageType as! String switch type { case "en": LocalizableManager.setLanguage(.english) case "zh-Hans": LocalizableManager.setLanguage(.chinese) default: break } } return true }
下载地址:XXyueliang / iosMultiLanguage - gitee
swift 国际化,app内部中英语言切换 - csdn(swift)
/// 获取系统语言方法
private func getCurrentLanguage() -> String {
// let preferredLang = Bundle.main.preferredLocalizations.first! as NSString 这句不行
let preferredLang = NSLocale.preferredLanguages.first! as NSString
switch String(describing: preferredLang) {
case "en-US", "en-CN":
return "en"//英文
case "zh-Hans-US","zh-Hans-CN","zh-Hant-CN","zh-TW","zh-HK","zh-Hans":
return "cn"//中文
default:
return "en"
}
}
// // LocalizableManager.swift // MultiLanguage // // Created by macvivi on 2022/3/20. // import UIKit enum LanguageType { case chinese,english,auto } class LocalizableManager: NSObject { static func localValue(_ str:String) -> String { LocalizableManager.shared.localValue(str: str) } static func setLanguage(_ type:LanguageType){ LocalizableManager.shared.setLanguage(type) } //单例 static let shared = LocalizableManager() private override init() { } var bundle:Bundle = Bundle.main private func localValue(str:String) -> String{ //table参数值传nil也是可以的,传nil系统就会默认为Localizable bundle.localizedString(forKey: str, value: nil, table: "Localizable") } /// 获取系统语言方法 private func getCurrentLanguage() -> String { let preferredLang = NSLocale.preferredLanguages.first! as NSString switch String(describing: preferredLang) { case "en-US", "en-CN": return "en"//英文 case "zh-Hans-US","zh-Hans-CN","zh-Hant-CN","zh-TW","zh-HK","zh-Hans": return "cn"//中文 default: return "en" } } private func setLanguage(_ type:LanguageType){ var typeStr = getCurrentLanguage() // cn: 中文 print(typeStr) switch type { case .chinese: typeStr = "zh-Hans" case .english: typeStr = "en" default: break } //返回项目中 en.lproj 文件的路径 let path = Bundle.main.path(forResource: typeStr, ofType: "lproj") //用这个路径生成新的bundle bundle = Bundle(path: path!)! } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。