104 lines
3.4 KiB
Swift
104 lines
3.4 KiB
Swift
//
|
||
// SPLocalizedManager.swift
|
||
// MoviaBox
|
||
//
|
||
// Created by 曾觉新 on 2025/4/8.
|
||
//
|
||
|
||
import UIKit
|
||
|
||
class SPLocalizedManager: NSObject {
|
||
static let shared = SPLocalizedManager()
|
||
private let LocalizedUserDefaultsKey = "SPLocalizedManager.LocalizedUserDefaultsKey"
|
||
private let LocalizedDataUserDefaultsKey = "SPLocalizedManager.LocalizedDataUserDefaultsKey"
|
||
|
||
|
||
///语言列表
|
||
var languageList: [SPLanguageModel]?
|
||
|
||
///多语言数据
|
||
private lazy var localizedData: [String : String]? = UserDefaults.standard.object(forKey: LocalizedDataUserDefaultsKey) as? [String : String]
|
||
{
|
||
didSet {
|
||
UserDefaults.standard.set(localizedData, forKey: LocalizedDataUserDefaultsKey)
|
||
UserDefaults.standard.synchronize()
|
||
}
|
||
}
|
||
|
||
// 获取当前语言代码(如果用户未手动设置,则返回系统语言)
|
||
var currentLocalizedKey: String {
|
||
get {
|
||
// return UserDefaults.standard.string(forKey: userDefaultsKey) ?? Locale.preferredLanguages.first ?? "en"
|
||
return UserDefaults.standard.string(forKey: LocalizedUserDefaultsKey) ?? "en"
|
||
}
|
||
set {
|
||
UserDefaults.standard.set(newValue, forKey: LocalizedUserDefaultsKey)
|
||
UserDefaults.standard.synchronize()
|
||
}
|
||
}
|
||
|
||
// 判断是否跟随系统
|
||
var isFollowingSystem: Bool {
|
||
return UserDefaults.standard.string(forKey: LocalizedUserDefaultsKey) == nil
|
||
}
|
||
|
||
// 还原为系统默认语言
|
||
func resetToSystemLanguage() {
|
||
UserDefaults.standard.removeObject(forKey: LocalizedUserDefaultsKey)
|
||
UserDefaults.standard.synchronize()
|
||
}
|
||
|
||
// 获取本地化字符串
|
||
func localizedString(forKey key: String, tableName: String? = nil) -> String {
|
||
if let localizedData = localizedData,
|
||
let text = localizedData[key] {
|
||
return text
|
||
|
||
} else if let selectedLanguage = UserDefaults.standard.string(forKey: LocalizedUserDefaultsKey),
|
||
let bundlePath = Bundle.main.path(forResource: selectedLanguage, ofType: "lproj"),
|
||
let bundle = Bundle(path: bundlePath) {
|
||
|
||
return bundle.localizedString(forKey: key, value: nil, table: tableName)
|
||
} else {
|
||
return NSLocalizedString(key, tableName: tableName, bundle: .main, value: "", comment: "")
|
||
}
|
||
}
|
||
}
|
||
|
||
extension SPLocalizedManager {
|
||
///获取本地化数据
|
||
func updateLocalizedData(key: String = SPLocalizedManager.shared.currentLocalizedKey, completer: ((_ finish: Bool) -> Void)?) {
|
||
SPSettingAPI.requestLocalizedData(key: key) { [weak self] model in
|
||
guard let self = self else { return }
|
||
guard let model = model else {
|
||
completer?(false)
|
||
return
|
||
}
|
||
if let languageList = model.languages, languageList.count > 0 {
|
||
self.languageList = languageList
|
||
}
|
||
|
||
if let localizedData = model.translates {
|
||
self.localizedData = localizedData
|
||
completer?(true)
|
||
} else {
|
||
completer?(false)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
extension SPLocalizedManager {
|
||
|
||
static let localizedDidChange = Notification.Name(rawValue: "SPLocalizedManager.localizedDidChange")
|
||
|
||
}
|
||
|
||
extension String {
|
||
var localized: String {
|
||
return SPLocalizedManager.shared.localizedString(forKey: self)
|
||
}
|
||
}
|