From 1a43d30f588b148e72bcc28c878f5f346a721f8e Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Sat, 23 Mar 2024 12:00:48 +0800 Subject: [PATCH] use swift to implement EZLocalizedBundle --- Easydict.xcodeproj/project.pbxproj | 22 ++++------ .../LanguagePreference/EZLocalizedBundle.h | 17 -------- .../LanguagePreference/EZLocalizedBundle.m | 43 ------------------- .../LanguagePreference/EZI18nHelper.swift | 4 -- .../EZLocalizedBundle.swift | 15 +++++++ .../NSBundle+LanguagePreference.m | 20 +++++++++ 6 files changed, 43 insertions(+), 78 deletions(-) delete mode 100644 Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.h delete mode 100644 Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m create mode 100644 Easydict/NewApp/LanguagePreference/EZLocalizedBundle.swift create mode 100644 Easydict/NewApp/LanguagePreference/NSBundle+LanguagePreference.m diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index bd0c1f9d3..6a2f0ed94 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -275,7 +275,8 @@ 62ED29A22B15F1F500901F51 /* EZWrapView.m in Sources */ = {isa = PBXBuildFile; fileRef = 62ED29A12B15F1F500901F51 /* EZWrapView.m */; }; 6A8C988E2BAC88B500DB835A /* LanguageState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A8C988C2BAC88B500DB835A /* LanguageState.swift */; }; 6A8C988F2BAC88B500DB835A /* EZI18nHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A8C988D2BAC88B500DB835A /* EZI18nHelper.swift */; }; - 6A8C98932BAC8C0A00DB835A /* EZLocalizedBundle.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A8C98922BAC8C0A00DB835A /* EZLocalizedBundle.m */; }; + 6A8C98952BAE841600DB835A /* EZLocalizedBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A8C98942BAE841600DB835A /* EZLocalizedBundle.swift */; }; + 6ADED1552BAE8809004A15BE /* NSBundle+LanguagePreference.m in Sources */ = {isa = PBXBuildFile; fileRef = 6ADED1542BAE8809004A15BE /* NSBundle+LanguagePreference.m */; }; 960835502B6791F200C6A931 /* Shortcut+Validator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9608354F2B6791F200C6A931 /* Shortcut+Validator.swift */; }; 96099AE22B5D40330055C4DD /* ShortcutTab.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96099AE12B5D40330055C4DD /* ShortcutTab.swift */; }; 9627F9382B59956800B1E999 /* GlobalShortcutSetting.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9627F9352B59956800B1E999 /* GlobalShortcutSetting.swift */; }; @@ -805,8 +806,8 @@ 6372B33DFF803C7096A82250 /* Pods_Easydict.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Easydict.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 6A8C988C2BAC88B500DB835A /* LanguageState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LanguageState.swift; sourceTree = ""; }; 6A8C988D2BAC88B500DB835A /* EZI18nHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EZI18nHelper.swift; sourceTree = ""; }; - 6A8C98912BAC8C0A00DB835A /* EZLocalizedBundle.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZLocalizedBundle.h; sourceTree = ""; }; - 6A8C98922BAC8C0A00DB835A /* EZLocalizedBundle.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZLocalizedBundle.m; sourceTree = ""; }; + 6A8C98942BAE841600DB835A /* EZLocalizedBundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EZLocalizedBundle.swift; sourceTree = ""; }; + 6ADED1542BAE8809004A15BE /* NSBundle+LanguagePreference.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSBundle+LanguagePreference.m"; sourceTree = ""; }; 91E3E579C6DB88658B4BB102 /* Pods-Easydict.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Easydict.release.xcconfig"; path = "Target Support Files/Pods-Easydict/Pods-Easydict.release.xcconfig"; sourceTree = ""; }; 9608354F2B6791F200C6A931 /* Shortcut+Validator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Shortcut+Validator.swift"; sourceTree = ""; }; 96099AE12B5D40330055C4DD /* ShortcutTab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutTab.swift; sourceTree = ""; }; @@ -1244,7 +1245,6 @@ 038954372A25A94E00EFFDC3 /* Utility */ = { isa = PBXGroup; children = ( - 6A8C98902BAC8BDD00DB835A /* LanguagePreference */, 03CF88602B137ECB0030C199 /* Swift */, 03CAB9522ADBEF5000DA94A3 /* SystemUtility */, 030570DF2ADB916E00C9905E /* AppleScript */, @@ -2254,15 +2254,8 @@ children = ( 6A8C988D2BAC88B500DB835A /* EZI18nHelper.swift */, 6A8C988C2BAC88B500DB835A /* LanguageState.swift */, - ); - path = LanguagePreference; - sourceTree = ""; - }; - 6A8C98902BAC8BDD00DB835A /* LanguagePreference */ = { - isa = PBXGroup; - children = ( - 6A8C98912BAC8C0A00DB835A /* EZLocalizedBundle.h */, - 6A8C98922BAC8C0A00DB835A /* EZLocalizedBundle.m */, + 6A8C98942BAE841600DB835A /* EZLocalizedBundle.swift */, + 6ADED1542BAE8809004A15BE /* NSBundle+LanguagePreference.m */, ); path = LanguagePreference; sourceTree = ""; @@ -2926,7 +2919,6 @@ 03B0230429231FA6001C7E63 /* EZHoverButton.m in Sources */, 0342A9812AD64924002A9F5F /* NSString+EZSplit.m in Sources */, EAED41EF2B54B1430005FE0A /* ConfigurableService.swift in Sources */, - 6A8C98932BAC8C0A00DB835A /* EZLocalizedBundle.m in Sources */, 03BD2825294875AE00F5891A /* EZMyLabel.m in Sources */, 03B0233029231FA6001C7E63 /* MMCrashUncaughtExceptionHandler.m in Sources */, 03D5FCFF2A5EF4E400AD26BE /* EZDeviceSystemInfo.m in Sources */, @@ -2956,6 +2948,7 @@ 03B0230229231FA6001C7E63 /* EZWordResultView.m in Sources */, 0399C6A529A747E600B4AFCC /* EZDeepLTranslateResponse.m in Sources */, 17BCAEF82B0DFF9000A7D372 /* EZNiuTransTranslate.m in Sources */, + 6A8C98952BAE841600DB835A /* EZLocalizedBundle.swift in Sources */, 039F5506294B6E29004AB940 /* EZSettingViewController.m in Sources */, 03BD281E29481C0400F5891A /* EZAudioPlayer.m in Sources */, 0A8685C82B552A590022534F /* DisabledAppTab.swift in Sources */, @@ -3033,6 +3026,7 @@ 03B0231729231FA6001C7E63 /* Snip.m in Sources */, 03BFFC6E295FE59C004E033E /* EZQueryResult+EZYoudaoDictModel.m in Sources */, DC3C643F2B187119008EEDD8 /* ChangeFontSizeView.swift in Sources */, + 6ADED1552BAE8809004A15BE /* NSBundle+LanguagePreference.m in Sources */, 03B0232829231FA6001C7E63 /* NSTextView+Height.m in Sources */, 03B0232129231FA6001C7E63 /* NSPasteboard+MM.m in Sources */, 03D043522928935300E7559E /* EZMainQueryWindow.m in Sources */, diff --git a/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.h b/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.h deleted file mode 100644 index cc32625fd..000000000 --- a/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.h +++ /dev/null @@ -1,17 +0,0 @@ -// -// EZLocalizedBundle.h -// Easydict -// -// Created by choykarl on 2024/3/21. -// Copyright © 2024 izual. All rights reserved. -// - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface EZLocalizedBundle : NSBundle - -@end - -NS_ASSUME_NONNULL_END diff --git a/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m b/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m deleted file mode 100644 index 791b5d6bd..000000000 --- a/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m +++ /dev/null @@ -1,43 +0,0 @@ -// -// EZLocalizedBundle.m -// Easydict -// -// Created by choykarl on 2024/3/21. -// Copyright © 2024 izual. All rights reserved. -// - -#import "EZLocalizedBundle.h" -#import - -@implementation NSBundle (localized) - -+ (void)load { - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - object_setClass([NSBundle mainBundle], [EZLocalizedBundle class]); - }); -} - -@end - -@implementation EZLocalizedBundle -- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName { - NSBundle *localizedBundle = [self localizedBundle]; - if (localizedBundle) { - return [localizedBundle localizedStringForKey:key value:value table:tableName]; - } else { - return [super localizedStringForKey:key value:value table:tableName]; - } -} - -- (NSBundle *)localizedBundle { - NSString *localizeCode = EZI18nHelper.shared.localizeCode; - if (localizeCode.length) { - NSString *path = [[NSBundle mainBundle] pathForResource:localizeCode ofType:@"lproj"]; - if (path.length) { - return [NSBundle bundleWithPath:path]; - } - } - return nil; -} -@end diff --git a/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift b/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift index b38e94459..47a6c053e 100644 --- a/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift +++ b/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift @@ -8,10 +8,6 @@ import SwiftUI -var localizedBundle: Bundle { - EZI18nHelper.shared.localizedBundle -} - // MARK: - EZI18nHelper @objcMembers diff --git a/Easydict/NewApp/LanguagePreference/EZLocalizedBundle.swift b/Easydict/NewApp/LanguagePreference/EZLocalizedBundle.swift new file mode 100644 index 000000000..36426650d --- /dev/null +++ b/Easydict/NewApp/LanguagePreference/EZLocalizedBundle.swift @@ -0,0 +1,15 @@ +// +// EZLocalizedBundle.swift +// Easydict +// +// Created by choykarl on 2024/3/23. +// Copyright © 2024 izual. All rights reserved. +// + +import Foundation + +class EZLocalizedBundle: Bundle { + override func localizedString(forKey key: String, value: String?, table tableName: String?) -> String { + EZI18nHelper.shared.localizedBundle.localizedString(forKey: key, value: value, table: tableName) + } +} diff --git a/Easydict/NewApp/LanguagePreference/NSBundle+LanguagePreference.m b/Easydict/NewApp/LanguagePreference/NSBundle+LanguagePreference.m new file mode 100644 index 000000000..0fc7976ad --- /dev/null +++ b/Easydict/NewApp/LanguagePreference/NSBundle+LanguagePreference.m @@ -0,0 +1,20 @@ +// +// LanguagePreference.m +// Easydict +// +// Created by choykarl on 2024/3/23. +// Copyright © 2024 izual. All rights reserved. +// + +#import + +@implementation NSBundle (LanguagePreference) + ++ (void)load { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + object_setClass([NSBundle mainBundle], [EZLocalizedBundle class]); + }); +} + +@end