diff --git a/FireworkVideoUI.podspec b/FireworkVideoUI.podspec index 0e20e2f..8fbf8dd 100644 --- a/FireworkVideoUI.podspec +++ b/FireworkVideoUI.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'FireworkVideoUI' - s.version = '0.1.0' + s.version = '0.1.1' s.summary = 'An extension library meant to provide easier interfaces for the FireworkVideoSDK.' s.homepage = 'https://github.com/loopsocial/firework_ios_sdk_ui_extensions' s.license = 'Apache License, Version 2.0' diff --git a/Sources/FireworkVideoUI/AppLanguage/AppLanguageManager.swift b/Sources/FireworkVideoUI/AppLanguage/AppLanguageManager.swift index 89cac35..b227c8a 100644 --- a/Sources/FireworkVideoUI/AppLanguage/AppLanguageManager.swift +++ b/Sources/FireworkVideoUI/AppLanguage/AppLanguageManager.swift @@ -103,13 +103,13 @@ public class AppLanguageManager { UIViewController.swizzleViewControllerMethodsForAppLanguage() Bundle.swizzleBundleMethodsForAppLanguage() URLSession.swizzleURLSessionMethodsForAppLanguage() - NumberFormatter.swizzleNumberFormatterMethodsForAppLanguage() UIImageView.swizzleImageViewMethodsForAppLanguage() UILabel.swizzleLabelMethodsForAppLanguage() UITextField.swizzleTextFieldMethodsForAppLanguage() UITextView.swizzleTextViewMethodsForAppLanguage() UIWindow.swizzleWindowMethodsForAppLanguage() UIView.swizzleViewMethodsForAppLanguage() + NSLocale.swizzleNSLocaleMethodsForAppLanguage() } LayoutFlipManager.swizzelMethods() diff --git a/Sources/FireworkVideoUI/AppLanguage/Extensions/Foundation/NSLocale+AppLanguage.swift b/Sources/FireworkVideoUI/AppLanguage/Extensions/Foundation/NSLocale+AppLanguage.swift new file mode 100644 index 0000000..748b97b --- /dev/null +++ b/Sources/FireworkVideoUI/AppLanguage/Extensions/Foundation/NSLocale+AppLanguage.swift @@ -0,0 +1,49 @@ +// +// Locale+AppLanguage.swift +// +// Created by linjie jiang on 5/7/24. +// + +import Foundation + +extension NSLocale { + static func swizzleNSLocaleMethodsForAppLanguage() { + Swizzle.swizzleClassSelector( + cls: self, + originalSelector: #selector(getter: NSLocale.current), + customSelector: #selector(NSLocale.fw_current)) + Swizzle.swizzleClassSelector( + cls: self, + originalSelector: #selector(getter: NSLocale.autoupdatingCurrent), + customSelector: #selector(NSLocale.fw_autoupdatingCurrent)) + Swizzle.swizzleClassSelector( + cls: self, + originalSelector: #selector(getter: NSLocale.preferredLanguages), + customSelector: #selector(NSLocale.fw_preferredLanguages)) + } + + @objc class func fw_current() -> Locale { + if let language = AppLanguageManager.shared.appLanguage { + return Locale(identifier: language) + } + + return fw_current() + } + + @objc class func fw_autoupdatingCurrent() -> Locale { + if let language = AppLanguageManager.shared.appLanguage { + return Locale(identifier: language) + } + + return fw_autoupdatingCurrent() + } + + @objc class func fw_preferredLanguages() -> [String] { + let languages = fw_preferredLanguages() + if let language = AppLanguageManager.shared.appLanguage { + return [language] + languages + } + + return languages + } +} diff --git a/Sources/FireworkVideoUI/AppLanguage/Extensions/Foundation/NumberFormatter+AppLanguage.swift b/Sources/FireworkVideoUI/AppLanguage/Extensions/Foundation/NumberFormatter+AppLanguage.swift deleted file mode 100644 index 8a0735e..0000000 --- a/Sources/FireworkVideoUI/AppLanguage/Extensions/Foundation/NumberFormatter+AppLanguage.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// NumberFormatter+AppLanguage.swift -// -// Created by linjie jiang on 2023/2/20. -// - -import Foundation - -extension NumberFormatter { - static func swizzleNumberFormatterMethodsForAppLanguage() { - Swizzle.swizzleSelector( - cls: self, - originalSelector: #selector(NumberFormatter.string(from:)), - customSelector: #selector(NumberFormatter.fw_string(from:))) - } - - @objc func fw_string(from number: NSNumber) -> String? { - if let language = AppLanguageManager.shared.appLanguage, - self.numberStyle == .currency { - self.locale = Locale(identifier: language) - } - - return fw_string(from: number) - } -} diff --git a/Sources/FireworkVideoUI/Extensions/ObjCRuntime/Swizzle.swift b/Sources/FireworkVideoUI/Extensions/ObjCRuntime/Swizzle.swift index 401326a..a886f93 100644 --- a/Sources/FireworkVideoUI/Extensions/ObjCRuntime/Swizzle.swift +++ b/Sources/FireworkVideoUI/Extensions/ObjCRuntime/Swizzle.swift @@ -32,6 +32,21 @@ public class Swizzle { guard let originalMethod = class_getClassMethod(cls, originalSelector) else { return } guard let customMethod = class_getClassMethod(cls, customSelector) else { return } - method_exchangeImplementations(originalMethod, customMethod) + let clsType: AnyClass? = object_getClass(cls) + if class_addMethod( + clsType, + originalSelector, + method_getImplementation(customMethod), + method_getTypeEncoding(customMethod) + ) { + class_replaceMethod( + clsType, + customSelector, + method_getImplementation(originalMethod), + method_getTypeEncoding(originalMethod) + ) + } else { + method_exchangeImplementations(originalMethod, customMethod) + } } }