From 9e83e535fa4e87e27fda9a52c1ae2bec7794d11b Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Fri, 22 Mar 2024 21:46:19 +0800 Subject: [PATCH 01/10] modify localized bundle pointer by runtime --- Easydict.xcodeproj/project.pbxproj | 30 ++++++++ Easydict/App/Localizable.xcstrings | 51 ++++++++----- Easydict/App/PrefixHeader.pch | 2 + .../Service/Language/EZLanguageManager.m | 2 +- .../EZCategory/NSObject+EZWindowType.m | 6 +- .../LanguagePreference/EZLocalizedBundle.h | 17 +++++ .../LanguagePreference/EZLocalizedBundle.m | 44 ++++++++++++ .../LanguageButton/EZSelectLanguageButton.m | 4 +- .../EZBaseQueryViewController.m | 6 ++ Easydict/NewApp/EasydictApp.swift | 5 +- .../LanguagePreference/EZI18nHelper.swift | 71 +++++++++++++++++++ .../LanguagePreference/LanguageState.swift | 44 ++++++++++++ .../NewApp/View/SettingView/SettingView.swift | 1 + .../View/SettingView/Tabs/AboutTab.swift | 2 + .../SettingView/Tabs/DisabledAppTab.swift | 2 + .../View/SettingView/Tabs/GeneralTab.swift | 7 ++ .../View/SettingView/Tabs/PrivacyTab.swift | 1 + .../View/SettingView/Tabs/ServiceTab.swift | 2 + 18 files changed, 272 insertions(+), 25 deletions(-) create mode 100644 Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.h create mode 100644 Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m create mode 100644 Easydict/NewApp/LanguagePreference/EZI18nHelper.swift create mode 100644 Easydict/NewApp/LanguagePreference/LanguageState.swift diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index 83d55ca7a..bd0c1f9d3 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -273,6 +273,9 @@ 62E2BF4B2B4082BA00E42D38 /* AliResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E2BF482B4082BA00E42D38 /* AliResponse.swift */; }; 62E2BF4C2B4082BA00E42D38 /* AliTranslateType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E2BF492B4082BA00E42D38 /* AliTranslateType.swift */; }; 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 */; }; 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 */; }; @@ -800,6 +803,10 @@ 62ED29A02B15F1F500901F51 /* EZWrapView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZWrapView.h; sourceTree = ""; }; 62ED29A12B15F1F500901F51 /* EZWrapView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZWrapView.m; sourceTree = ""; }; 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 = ""; }; 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 = ""; }; @@ -1237,6 +1244,7 @@ 038954372A25A94E00EFFDC3 /* Utility */ = { isa = PBXGroup; children = ( + 6A8C98902BAC8BDD00DB835A /* LanguagePreference */, 03CF88602B137ECB0030C199 /* Swift */, 03CAB9522ADBEF5000DA94A3 /* SystemUtility */, 030570DF2ADB916E00C9905E /* AppleScript */, @@ -2156,6 +2164,7 @@ 27FE98032B3DCA9F000AD654 /* NewApp */ = { isa = PBXGroup; children = ( + 6A8C988B2BAC88A600DB835A /* LanguagePreference */, 967712EB2B5B93E200105E0F /* Feature */, EA9943E12B534C2900EE7B97 /* Model */, EA9943DD2B534BAE00EE7B97 /* Utility */, @@ -2240,6 +2249,24 @@ path = EZWrapView; sourceTree = ""; }; + 6A8C988B2BAC88A600DB835A /* LanguagePreference */ = { + isa = PBXGroup; + children = ( + 6A8C988D2BAC88B500DB835A /* EZI18nHelper.swift */, + 6A8C988C2BAC88B500DB835A /* LanguageState.swift */, + ); + path = LanguagePreference; + sourceTree = ""; + }; + 6A8C98902BAC8BDD00DB835A /* LanguagePreference */ = { + isa = PBXGroup; + children = ( + 6A8C98912BAC8C0A00DB835A /* EZLocalizedBundle.h */, + 6A8C98922BAC8C0A00DB835A /* EZLocalizedBundle.m */, + ); + path = LanguagePreference; + sourceTree = ""; + }; 713A345D86B5BC86D158B68F /* Frameworks */ = { isa = PBXGroup; children = ( @@ -2880,6 +2907,7 @@ 960835502B6791F200C6A931 /* Shortcut+Validator.swift in Sources */, 03542A522937B69200C34C33 /* EZYoudaoTranslateResponse.m in Sources */, 03B0230129231FA6001C7E63 /* EZQueryView.m in Sources */, + 6A8C988E2BAC88B500DB835A /* LanguageState.swift in Sources */, 03542A3D2937AF4F00C34C33 /* EZQueryResult.m in Sources */, 03262C1F29EF8EE500EFECA0 /* EZPrivacyViewController.m in Sources */, 9672D7D22B4008B40023B8FB /* MASShortcutBinder+EZMASShortcutBinder.m in Sources */, @@ -2898,6 +2926,7 @@ 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 */, @@ -3007,6 +3036,7 @@ 03B0232829231FA6001C7E63 /* NSTextView+Height.m in Sources */, 03B0232129231FA6001C7E63 /* NSPasteboard+MM.m in Sources */, 03D043522928935300E7559E /* EZMainQueryWindow.m in Sources */, + 6A8C988F2BAC88B500DB835A /* EZI18nHelper.swift in Sources */, 03D8B26E292DBD2000D5A811 /* EZCoordinateUtils.m in Sources */, 03B0232029231FA6001C7E63 /* NSWindow+MM.m in Sources */, 0AC8A8432B6957B0006DA5CC /* BingService+ConfigurableService.swift in Sources */, diff --git a/Easydict/App/Localizable.xcstrings b/Easydict/App/Localizable.xcstrings index e49d75387..2972fb27c 100644 --- a/Easydict/App/Localizable.xcstrings +++ b/Easydict/App/Localizable.xcstrings @@ -1761,6 +1761,23 @@ } } }, + "language_preference" : { + "extractionState" : "manual", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Display Language" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "显示语言" + } + } + } + }, "large" : { "extractionState" : "manual", "localizations" : { @@ -2675,22 +2692,6 @@ } } }, - "service.configuration.custom_openai.name.title" : { - "localizations" : { - "en" : { - "stringUnit" : { - "state" : "translated", - "value" : "Custom Service Name" - } - }, - "zh-Hans" : { - "stringUnit" : { - "state" : "translated", - "value" : "自定义服务名称" - } - } - } - }, "service.configuration.custom_openai.model.placeholder" : { "localizations" : { "en" : { @@ -2707,6 +2708,22 @@ } } }, + "service.configuration.custom_openai.name.title" : { + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "Custom Service Name" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "自定义服务名称" + } + } + } + }, "service.configuration.custom_openai.supported_models.title" : { "localizations" : { "en" : { @@ -4634,4 +4651,4 @@ } }, "version" : "1.0" -} +} \ No newline at end of file diff --git a/Easydict/App/PrefixHeader.pch b/Easydict/App/PrefixHeader.pch index 9f259206c..0f48bed10 100644 --- a/Easydict/App/PrefixHeader.pch +++ b/Easydict/App/PrefixHeader.pch @@ -40,3 +40,5 @@ #import "EZConst.h" #import "EZConstKey.h" #import "NSString+EZConvenience.h" + +#import "Easydict-Swift.h" diff --git a/Easydict/Feature/Service/Language/EZLanguageManager.m b/Easydict/Feature/Service/Language/EZLanguageManager.m index 6650763d5..29b69f511 100644 --- a/Easydict/Feature/Service/Language/EZLanguageManager.m +++ b/Easydict/Feature/Service/Language/EZLanguageManager.m @@ -355,7 +355,7 @@ - (NSString *)languageLocalName:(EZLanguage)language { /// Showing language name according user first language, Chinese: English -> 英语, English: English -> English. - (NSString *)showingLanguageName:(EZLanguage)language { NSString *languageName = language ?: EZLanguageAuto; - if ([self isSystemChineseFirstLanguage]) { + if (EZI18nHelper.shared.isSimplifiedChineseLocalize) { languageName = [self languageChineseName:language]; } else { if ([language isEqualToString:EZLanguageAuto]) { diff --git a/Easydict/Feature/Utility/EZCategory/NSObject+EZWindowType.m b/Easydict/Feature/Utility/EZCategory/NSObject+EZWindowType.m index 48567504a..a1ae71f4c 100644 --- a/Easydict/Feature/Utility/EZCategory/NSObject+EZWindowType.m +++ b/Easydict/Feature/Utility/EZCategory/NSObject+EZWindowType.m @@ -8,16 +8,16 @@ #import "NSObject+EZWindowType.h" -static NSString *EZWindowTypeKey = @"EZWindowTypeKey"; +static NSString *_EZWindowTypeKey = @"EZWindowTypeKey"; @implementation NSObject (EZWindowType) - (void)setWindowType:(EZWindowType)windowType { - objc_setAssociatedObject(self, (__bridge const void *)(EZWindowTypeKey), @(windowType), OBJC_ASSOCIATION_RETAIN_NONATOMIC); + objc_setAssociatedObject(self, (__bridge const void *)(_EZWindowTypeKey), @(windowType), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (EZWindowType)windowType { - return [objc_getAssociatedObject(self, (__bridge const void *)(EZWindowTypeKey)) integerValue]; + return [objc_getAssociatedObject(self, (__bridge const void *)(_EZWindowTypeKey)) integerValue]; } @end diff --git a/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.h b/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.h new file mode 100644 index 000000000..cc32625fd --- /dev/null +++ b/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.h @@ -0,0 +1,17 @@ +// +// 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 new file mode 100644 index 000000000..91383da0c --- /dev/null +++ b/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m @@ -0,0 +1,44 @@ +// +// 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 { + NSLog(@"localizedStringForKey super"); + 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/Feature/ViewController/View/CustomButton/LanguageButton/EZSelectLanguageButton.m b/Easydict/Feature/ViewController/View/CustomButton/LanguageButton/EZSelectLanguageButton.m index faa8b1e83..c3a8eb9cf 100644 --- a/Easydict/Feature/ViewController/View/CustomButton/LanguageButton/EZSelectLanguageButton.m +++ b/Easydict/Feature/ViewController/View/CustomButton/LanguageButton/EZSelectLanguageButton.m @@ -117,7 +117,7 @@ - (void)setupMenu { NSString *languageFlag = [languageManager languageFlagEmoji:language]; if ([language isEqualToString:EZLanguageAuto]) { - if ([languageManager isSystemChineseFirstLanguage] && self.autoChineseSelectedTitle.length) { + if (EZI18nHelper.shared.isSimplifiedChineseLocalize && self.autoChineseSelectedTitle.length) { languageName = self.autoChineseSelectedTitle; } } @@ -170,7 +170,7 @@ - (void)setSelectedLanguage:(EZLanguage)selectedLanguage { NSString *toolTip = nil; if ([selectedLanguage isEqualToString:EZLanguageAuto]) { - if ([languageManager isSystemChineseFirstLanguage] && self.autoChineseSelectedTitle.length) { + if (EZI18nHelper.shared.isSimplifiedChineseLocalize && self.autoChineseSelectedTitle.length) { languageName = self.autoChineseSelectedTitle; } languageFlag = [languageManager languageFlagEmoji:self.autoSelectedLanguage]; diff --git a/Easydict/Feature/ViewController/Window/BaseQueryWindow/EZBaseQueryViewController.m b/Easydict/Feature/ViewController/Window/BaseQueryWindow/EZBaseQueryViewController.m index 11181a029..5b6388600 100644 --- a/Easydict/Feature/ViewController/Window/BaseQueryWindow/EZBaseQueryViewController.m +++ b/Easydict/Feature/ViewController/Window/BaseQueryWindow/EZBaseQueryViewController.m @@ -192,6 +192,12 @@ - (void)setupUI { [self updateAllResultCellHeight]; }]; }]; + + [defaultCenter addObserver:self selector:@selector(modifyLanduage:) name:EZI18nHelper.languagePreferenceChangedNotification object:nil]; +} + +- (void)modifyLanduage:(NSNotification *)notification { + [self.tableView reloadData]; } diff --git a/Easydict/NewApp/EasydictApp.swift b/Easydict/NewApp/EasydictApp.swift index 55a676234..9a556fbc2 100644 --- a/Easydict/NewApp/EasydictApp.swift +++ b/Easydict/NewApp/EasydictApp.swift @@ -33,7 +33,7 @@ struct EasydictApp: App { var body: some Scene { if #available(macOS 13, *) { MenuBarExtra(isInserted: $hideMenuBar.toggledValue) { - MenuItemView() + MenuItemView().environmentObject(languageState) } label: { Label { Text("Easydict") @@ -62,7 +62,7 @@ struct EasydictApp: App { .windowResizability(.contentSize) Settings { - SettingView() + SettingView().environmentObject(languageState) } } } @@ -77,6 +77,7 @@ struct EasydictApp: App { private var hideMenuBar = Defaults.Key.hideMenuBarIcon.defaultValue @Default(.selectedMenuBarIcon) private var menuBarIcon + @StateObject private var languageState = LanguageState() } // MARK: - FakeViewToOpenSettingsInSonoma diff --git a/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift b/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift new file mode 100644 index 000000000..eac8fe616 --- /dev/null +++ b/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift @@ -0,0 +1,71 @@ +// +// EZI18nHelper.swift +// Easydict +// +// Created by choykarl on 2024/3/4. +// Copyright © 2024 izual. All rights reserved. +// + +import SwiftUI + +var localizedBundle: Bundle { + EZI18nHelper.shared.localizedBundle +} + +// MARK: - EZI18nHelper + +@objcMembers +class EZI18nHelper: NSObject { + static let languagePreferenceChangedNotification = "EZLanguagePreferenceChangedNotification" + static let shared = EZI18nHelper() + + var localizedBundle: Bundle { + let res = localizeCode + let path = Bundle.main.path(forResource: res, ofType: "lproj") + let bundle: Bundle + if let path = path { + bundle = Bundle(path: path) ?? .main + } else { + bundle = .main + } + return bundle + } + + var localizeCode: String { + UserDefaults.standard.string(forKey: kEZLanguagePreferenceLocalKey) ?? LanguageState.LanguageType + .simplifiedChinese.rawValue + } + + var languageType: LanguageState.LanguageType { + LanguageState.LanguageType(rawValue: localizeCode) ?? .simplifiedChinese + } + + var isSimplifiedChineseLocalize: Bool { + localizeCode == LanguageState.LanguageType.simplifiedChinese.rawValue + } + + class func localized(key: String) -> String { + key.localized + } +} + +extension String { + var localized: String { + NSLocalizedString(self, bundle: localizedBundle, value: "", comment: "") + } +} + +/// https://stackoverflow.com/questions/60841915/how-to-change-localizedstringkey-to-string-in-swiftui +extension LocalizedStringKey { + // This will mirror the `LocalizedStringKey` so it can access its + // internal `key` property. Mirroring is rather expensive, but it + // should be fine performance-wise, unless you are + // using it too much or doing something out of the norm. + var stringKey: String? { + Mirror(reflecting: self).children.first(where: { $0.label == "key" })?.value as? String + } + + var stringKeyLocalized: String { + (stringKey ?? "").localized + } +} diff --git a/Easydict/NewApp/LanguagePreference/LanguageState.swift b/Easydict/NewApp/LanguagePreference/LanguageState.swift new file mode 100644 index 000000000..51732665e --- /dev/null +++ b/Easydict/NewApp/LanguagePreference/LanguageState.swift @@ -0,0 +1,44 @@ +// +// LanguageState.swift +// Easydict +// +// Created by choykarl on 2024/3/3. +// Copyright © 2024 izual. All rights reserved. +// + +import SwiftUI + +// MARK: - LanguageState + +let kEZLanguagePreferenceLocalKey = "kEZLanguagePreferenceLocalKey" + +// MARK: - LanguageState + +class LanguageState: ObservableObject { + enum LanguageType: String, CaseIterable { + case english = "en" + case simplifiedChinese = "zh-Hans" + + // MARK: Internal + + var name: String { + switch self { + case .english: + "English" + case .simplifiedChinese: + "简体中文" + } + } + } + + @AppStorage(kEZLanguagePreferenceLocalKey) var language: LanguageType = (.init( + rawValue: Locale.current.identifier + ) ?? .simplifiedChinese) { + didSet { + NotificationCenter.default.post( + name: NSNotification.Name(rawValue: EZI18nHelper.languagePreferenceChangedNotification), + object: nil + ) + } + } +} diff --git a/Easydict/NewApp/View/SettingView/SettingView.swift b/Easydict/NewApp/View/SettingView/SettingView.swift index c5033557c..229b16fd1 100644 --- a/Easydict/NewApp/View/SettingView/SettingView.swift +++ b/Easydict/NewApp/View/SettingView/SettingView.swift @@ -100,6 +100,7 @@ struct SettingView: View { @State private var selection = SettingTab.general @State private var window: NSWindow? + @EnvironmentObject private var languageState: LanguageState } @available(macOS 13, *) diff --git a/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift b/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift index f761cf664..c196941f4 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift @@ -42,6 +42,8 @@ struct AboutTab: View { // MARK: Private + @EnvironmentObject private var languageState: LanguageState + private var appName: String { Bundle.main.infoDictionary?["CFBundleName"] as? String ?? "" } diff --git a/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift b/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift index 5545d2678..8d0c53891 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift @@ -150,6 +150,8 @@ struct DisabledAppTab: View { // MARK: Private @StateObject private var disabledAppViewModel = DisabledAppViewModel() + + @EnvironmentObject private var languageState: LanguageState } // MARK: - ListToolbar diff --git a/Easydict/NewApp/View/SettingView/Tabs/GeneralTab.swift b/Easydict/NewApp/View/SettingView/Tabs/GeneralTab.swift index 1a1554198..e4674ff4f 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/GeneralTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/GeneralTab.swift @@ -212,6 +212,12 @@ struct GeneralTab: View { .foregroundStyle(.primary) } } + Picker("language_preference", selection: $languageState.language) { + ForEach(LanguageState.LanguageType.allCases, id: \.rawValue) { language in + Text(language.name) + .tag(language) + } + } } header: { Text("setting.general.other.header") } @@ -246,6 +252,7 @@ struct GeneralTab: View { // MARK: Private + @EnvironmentObject private var languageState: LanguageState @Default(.autoSelectText) private var autoSelectText @Default(.forceAutoGetSelectedText) private var forceAutoGetSelectedText @Default(.clickQuery) private var clickQuery diff --git a/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift b/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift index 74d1be5f2..9591b9d97 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift @@ -40,6 +40,7 @@ struct PrivacyTab: View { @Default(.allowCrashLog) private var allowCollectCrashLog @Default(.allowAnalytics) private var allowCollectAnalytics + @EnvironmentObject private var languageState: LanguageState } @available(macOS 13, *) diff --git a/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift b/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift index 76d3486c3..96ab926f6 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift @@ -230,6 +230,8 @@ private struct ServiceItemView: View { @EnvironmentObject private var viewModel: ServiceTabViewModel @ObservedObject private var serviceItemViewModel: ServiceItemViewModel + + @EnvironmentObject private var languageState: LanguageState } // MARK: - WindowTypePicker From fb0ab79a1db2502c8656bb768c1530f6a08a7b10 Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Sat, 23 Mar 2024 00:19:38 +0800 Subject: [PATCH 02/10] swiftui localized --- Easydict/NewApp/EasydictApp.swift | 9 +++++-- .../LanguagePreference/EZI18nHelper.swift | 25 ------------------- .../Extensions/LanguageExtensions.swift | 2 +- .../NewApp/View/SettingView/SettingView.swift | 1 - .../View/SettingView/Tabs/AboutTab.swift | 2 -- .../SettingView/Tabs/DisabledAppTab.swift | 2 -- .../View/SettingView/Tabs/PrivacyTab.swift | 1 - .../View/SettingView/Tabs/ServiceTab.swift | 2 -- 8 files changed, 8 insertions(+), 36 deletions(-) diff --git a/Easydict/NewApp/EasydictApp.swift b/Easydict/NewApp/EasydictApp.swift index 9a556fbc2..a6ad3e1b7 100644 --- a/Easydict/NewApp/EasydictApp.swift +++ b/Easydict/NewApp/EasydictApp.swift @@ -33,7 +33,9 @@ struct EasydictApp: App { var body: some Scene { if #available(macOS 13, *) { MenuBarExtra(isInserted: $hideMenuBar.toggledValue) { - MenuItemView().environmentObject(languageState) + MenuItemView() + .environmentObject(languageState) + .environment(\.locale, .init(identifier: EZI18nHelper.shared.localizeCode)) } label: { Label { Text("Easydict") @@ -62,7 +64,10 @@ struct EasydictApp: App { .windowResizability(.contentSize) Settings { - SettingView().environmentObject(languageState) + SettingView().environmentObject(languageState).environment( + \.locale, + .init(identifier: EZI18nHelper.shared.localizeCode) + ) } } } diff --git a/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift b/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift index eac8fe616..b38e94459 100644 --- a/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift +++ b/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift @@ -43,29 +43,4 @@ class EZI18nHelper: NSObject { var isSimplifiedChineseLocalize: Bool { localizeCode == LanguageState.LanguageType.simplifiedChinese.rawValue } - - class func localized(key: String) -> String { - key.localized - } -} - -extension String { - var localized: String { - NSLocalizedString(self, bundle: localizedBundle, value: "", comment: "") - } -} - -/// https://stackoverflow.com/questions/60841915/how-to-change-localizedstringkey-to-string-in-swiftui -extension LocalizedStringKey { - // This will mirror the `LocalizedStringKey` so it can access its - // internal `key` property. Mirroring is rather expensive, but it - // should be fine performance-wise, unless you are - // using it too much or doing something out of the norm. - var stringKey: String? { - Mirror(reflecting: self).children.first(where: { $0.label == "key" })?.value as? String - } - - var stringKeyLocalized: String { - (stringKey ?? "").localized - } } diff --git a/Easydict/NewApp/Utility/Extensions/LanguageExtensions.swift b/Easydict/NewApp/Utility/Extensions/LanguageExtensions.swift index 48ab3358b..f28adea1c 100644 --- a/Easydict/NewApp/Utility/Extensions/LanguageExtensions.swift +++ b/Easydict/NewApp/Utility/Extensions/LanguageExtensions.swift @@ -55,7 +55,7 @@ extension Language { } public var localizedName: String { - if EZLanguageManager.shared().isSystemChineseFirstLanguage() { + if EZI18nHelper.shared.isSimplifiedChineseLocalize { chineseName } else { if self == .auto { diff --git a/Easydict/NewApp/View/SettingView/SettingView.swift b/Easydict/NewApp/View/SettingView/SettingView.swift index 229b16fd1..c5033557c 100644 --- a/Easydict/NewApp/View/SettingView/SettingView.swift +++ b/Easydict/NewApp/View/SettingView/SettingView.swift @@ -100,7 +100,6 @@ struct SettingView: View { @State private var selection = SettingTab.general @State private var window: NSWindow? - @EnvironmentObject private var languageState: LanguageState } @available(macOS 13, *) diff --git a/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift b/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift index c196941f4..f761cf664 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift @@ -42,8 +42,6 @@ struct AboutTab: View { // MARK: Private - @EnvironmentObject private var languageState: LanguageState - private var appName: String { Bundle.main.infoDictionary?["CFBundleName"] as? String ?? "" } diff --git a/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift b/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift index 8d0c53891..5545d2678 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift @@ -150,8 +150,6 @@ struct DisabledAppTab: View { // MARK: Private @StateObject private var disabledAppViewModel = DisabledAppViewModel() - - @EnvironmentObject private var languageState: LanguageState } // MARK: - ListToolbar diff --git a/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift b/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift index 9591b9d97..74d1be5f2 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/PrivacyTab.swift @@ -40,7 +40,6 @@ struct PrivacyTab: View { @Default(.allowCrashLog) private var allowCollectCrashLog @Default(.allowAnalytics) private var allowCollectAnalytics - @EnvironmentObject private var languageState: LanguageState } @available(macOS 13, *) diff --git a/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift b/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift index 96ab926f6..76d3486c3 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift @@ -230,8 +230,6 @@ private struct ServiceItemView: View { @EnvironmentObject private var viewModel: ServiceTabViewModel @ObservedObject private var serviceItemViewModel: ServiceItemViewModel - - @EnvironmentObject private var languageState: LanguageState } // MARK: - WindowTypePicker From c984f2b78ba9eeaf07459bf2b5d8fe18a29379ce Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Sat, 23 Mar 2024 00:25:32 +0800 Subject: [PATCH 03/10] =?UTF-8?q?=E5=88=A0=E9=99=A4log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m | 1 - 1 file changed, 1 deletion(-) diff --git a/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m b/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m index 91383da0c..791b5d6bd 100644 --- a/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m +++ b/Easydict/Feature/Utility/LanguagePreference/EZLocalizedBundle.m @@ -26,7 +26,6 @@ - (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value tabl if (localizedBundle) { return [localizedBundle localizedStringForKey:key value:value table:tableName]; } else { - NSLog(@"localizedStringForKey super"); return [super localizedStringForKey:key value:value table:tableName]; } } 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 04/10] 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 From 2f98f6043a22aa9408bbb1309e6c561bbe38fdff Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Sat, 23 Mar 2024 15:13:46 +0800 Subject: [PATCH 05/10] remove the EZ prefix in swift. --- Easydict.xcodeproj/project.pbxproj | 16 ++++++++-------- .../Swift/Notification/Notification+Name.swift | 4 ++++ Easydict/NewApp/EasydictApp.swift | 4 ++-- .../{EZI18nHelper.swift => I18nHelper.swift} | 7 ++++--- .../LanguagePreference/LanguageState.swift | 5 +---- ...calizedBundle.swift => LocalizedBundle.swift} | 5 +++-- .../Utility/Extensions/LanguageExtensions.swift | 2 +- 7 files changed, 23 insertions(+), 20 deletions(-) rename Easydict/NewApp/LanguagePreference/{EZI18nHelper.swift => I18nHelper.swift} (90%) rename Easydict/NewApp/LanguagePreference/{EZLocalizedBundle.swift => LocalizedBundle.swift} (63%) diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index 6a2f0ed94..b0a6ae21a 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -274,8 +274,8 @@ 62E2BF4C2B4082BA00E42D38 /* AliTranslateType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E2BF492B4082BA00E42D38 /* AliTranslateType.swift */; }; 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 */; }; - 6A8C98952BAE841600DB835A /* EZLocalizedBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A8C98942BAE841600DB835A /* EZLocalizedBundle.swift */; }; + 6A8C988F2BAC88B500DB835A /* I18nHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A8C988D2BAC88B500DB835A /* I18nHelper.swift */; }; + 6A8C98952BAE841600DB835A /* LocalizedBundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A8C98942BAE841600DB835A /* LocalizedBundle.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 */; }; @@ -805,8 +805,8 @@ 62ED29A12B15F1F500901F51 /* EZWrapView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZWrapView.m; sourceTree = ""; }; 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 = ""; }; - 6A8C98942BAE841600DB835A /* EZLocalizedBundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EZLocalizedBundle.swift; sourceTree = ""; }; + 6A8C988D2BAC88B500DB835A /* I18nHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = I18nHelper.swift; sourceTree = ""; }; + 6A8C98942BAE841600DB835A /* LocalizedBundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizedBundle.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 = ""; }; @@ -2252,9 +2252,9 @@ 6A8C988B2BAC88A600DB835A /* LanguagePreference */ = { isa = PBXGroup; children = ( - 6A8C988D2BAC88B500DB835A /* EZI18nHelper.swift */, + 6A8C988D2BAC88B500DB835A /* I18nHelper.swift */, 6A8C988C2BAC88B500DB835A /* LanguageState.swift */, - 6A8C98942BAE841600DB835A /* EZLocalizedBundle.swift */, + 6A8C98942BAE841600DB835A /* LocalizedBundle.swift */, 6ADED1542BAE8809004A15BE /* NSBundle+LanguagePreference.m */, ); path = LanguagePreference; @@ -2948,7 +2948,7 @@ 03B0230229231FA6001C7E63 /* EZWordResultView.m in Sources */, 0399C6A529A747E600B4AFCC /* EZDeepLTranslateResponse.m in Sources */, 17BCAEF82B0DFF9000A7D372 /* EZNiuTransTranslate.m in Sources */, - 6A8C98952BAE841600DB835A /* EZLocalizedBundle.swift in Sources */, + 6A8C98952BAE841600DB835A /* LocalizedBundle.swift in Sources */, 039F5506294B6E29004AB940 /* EZSettingViewController.m in Sources */, 03BD281E29481C0400F5891A /* EZAudioPlayer.m in Sources */, 0A8685C82B552A590022534F /* DisabledAppTab.swift in Sources */, @@ -3030,7 +3030,7 @@ 03B0232829231FA6001C7E63 /* NSTextView+Height.m in Sources */, 03B0232129231FA6001C7E63 /* NSPasteboard+MM.m in Sources */, 03D043522928935300E7559E /* EZMainQueryWindow.m in Sources */, - 6A8C988F2BAC88B500DB835A /* EZI18nHelper.swift in Sources */, + 6A8C988F2BAC88B500DB835A /* I18nHelper.swift in Sources */, 03D8B26E292DBD2000D5A811 /* EZCoordinateUtils.m in Sources */, 03B0232029231FA6001C7E63 /* NSWindow+MM.m in Sources */, 0AC8A8432B6957B0006DA5CC /* BingService+ConfigurableService.swift in Sources */, diff --git a/Easydict/Feature/Utility/Swift/Notification/Notification+Name.swift b/Easydict/Feature/Utility/Swift/Notification/Notification+Name.swift index bae75eb23..7addc58c7 100644 --- a/Easydict/Feature/Utility/Swift/Notification/Notification+Name.swift +++ b/Easydict/Feature/Utility/Swift/Notification/Notification+Name.swift @@ -12,6 +12,8 @@ extension Notification.Name { static let serviceHasUpdated = Notification.Name(EZServiceHasUpdatedNotification) static let openSettings = Notification.Name(EZOpenSettingsNotification) + + static let languagePreferenceChanged = Notification.Name(I18nHelper.languagePreferenceChangedNotification) } @objc @@ -19,4 +21,6 @@ extension NSNotification { public static let serviceHasUpdated = Notification.Name.serviceHasUpdated public static let openSettings = Notification.Name.openSettings + + public static let languagePreferenceChanged = Notification.Name.languagePreferenceChanged } diff --git a/Easydict/NewApp/EasydictApp.swift b/Easydict/NewApp/EasydictApp.swift index a6ad3e1b7..5d77f14fe 100644 --- a/Easydict/NewApp/EasydictApp.swift +++ b/Easydict/NewApp/EasydictApp.swift @@ -35,7 +35,7 @@ struct EasydictApp: App { MenuBarExtra(isInserted: $hideMenuBar.toggledValue) { MenuItemView() .environmentObject(languageState) - .environment(\.locale, .init(identifier: EZI18nHelper.shared.localizeCode)) + .environment(\.locale, .init(identifier: I18nHelper.shared.localizeCode)) } label: { Label { Text("Easydict") @@ -66,7 +66,7 @@ struct EasydictApp: App { Settings { SettingView().environmentObject(languageState).environment( \.locale, - .init(identifier: EZI18nHelper.shared.localizeCode) + .init(identifier: I18nHelper.shared.localizeCode) ) } } diff --git a/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift b/Easydict/NewApp/LanguagePreference/I18nHelper.swift similarity index 90% rename from Easydict/NewApp/LanguagePreference/EZI18nHelper.swift rename to Easydict/NewApp/LanguagePreference/I18nHelper.swift index 47a6c053e..e98a14297 100644 --- a/Easydict/NewApp/LanguagePreference/EZI18nHelper.swift +++ b/Easydict/NewApp/LanguagePreference/I18nHelper.swift @@ -8,12 +8,13 @@ import SwiftUI -// MARK: - EZI18nHelper +// MARK: - I18nHelper @objcMembers -class EZI18nHelper: NSObject { +@objc(EZI18nHelper) +class I18nHelper: NSObject { static let languagePreferenceChangedNotification = "EZLanguagePreferenceChangedNotification" - static let shared = EZI18nHelper() + static let shared = I18nHelper() var localizedBundle: Bundle { let res = localizeCode diff --git a/Easydict/NewApp/LanguagePreference/LanguageState.swift b/Easydict/NewApp/LanguagePreference/LanguageState.swift index 51732665e..2ebb4f3fc 100644 --- a/Easydict/NewApp/LanguagePreference/LanguageState.swift +++ b/Easydict/NewApp/LanguagePreference/LanguageState.swift @@ -35,10 +35,7 @@ class LanguageState: ObservableObject { rawValue: Locale.current.identifier ) ?? .simplifiedChinese) { didSet { - NotificationCenter.default.post( - name: NSNotification.Name(rawValue: EZI18nHelper.languagePreferenceChangedNotification), - object: nil - ) + NotificationCenter.default.post(name: .languagePreferenceChanged, object: nil) } } } diff --git a/Easydict/NewApp/LanguagePreference/EZLocalizedBundle.swift b/Easydict/NewApp/LanguagePreference/LocalizedBundle.swift similarity index 63% rename from Easydict/NewApp/LanguagePreference/EZLocalizedBundle.swift rename to Easydict/NewApp/LanguagePreference/LocalizedBundle.swift index 36426650d..e532e181c 100644 --- a/Easydict/NewApp/LanguagePreference/EZLocalizedBundle.swift +++ b/Easydict/NewApp/LanguagePreference/LocalizedBundle.swift @@ -8,8 +8,9 @@ import Foundation -class EZLocalizedBundle: Bundle { +@objc(EZLocalizedBundle) +class LocalizedBundle: Bundle { override func localizedString(forKey key: String, value: String?, table tableName: String?) -> String { - EZI18nHelper.shared.localizedBundle.localizedString(forKey: key, value: value, table: tableName) + I18nHelper.shared.localizedBundle.localizedString(forKey: key, value: value, table: tableName) } } diff --git a/Easydict/NewApp/Utility/Extensions/LanguageExtensions.swift b/Easydict/NewApp/Utility/Extensions/LanguageExtensions.swift index f28adea1c..1aae6f356 100644 --- a/Easydict/NewApp/Utility/Extensions/LanguageExtensions.swift +++ b/Easydict/NewApp/Utility/Extensions/LanguageExtensions.swift @@ -55,7 +55,7 @@ extension Language { } public var localizedName: String { - if EZI18nHelper.shared.isSimplifiedChineseLocalize { + if I18nHelper.shared.isSimplifiedChineseLocalize { chineseName } else { if self == .auto { From aa4bd2871ef80e70fbdd61ec404b4b7694b972a6 Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Sat, 23 Mar 2024 15:22:36 +0800 Subject: [PATCH 06/10] optimize code --- Easydict/NewApp/LanguagePreference/I18nHelper.swift | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Easydict/NewApp/LanguagePreference/I18nHelper.swift b/Easydict/NewApp/LanguagePreference/I18nHelper.swift index e98a14297..47ebaa348 100644 --- a/Easydict/NewApp/LanguagePreference/I18nHelper.swift +++ b/Easydict/NewApp/LanguagePreference/I18nHelper.swift @@ -17,13 +17,9 @@ class I18nHelper: NSObject { static let shared = I18nHelper() var localizedBundle: Bundle { - let res = localizeCode - let path = Bundle.main.path(forResource: res, ofType: "lproj") - let bundle: Bundle - if let path = path { - bundle = Bundle(path: path) ?? .main - } else { - bundle = .main + guard let path = Bundle.main.path(forResource: localizeCode, ofType: "lproj"), + let bundle = Bundle(path: path) else { + return .main } return bundle } From b6ed91ef6a49b02d606faf5ee4c9476b22729dcd Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Sat, 23 Mar 2024 17:03:59 +0800 Subject: [PATCH 07/10] Update Easydict/Feature/ViewController/Window/BaseQueryWindow/EZBaseQueryViewController.m Co-authored-by: Phillip Song <103433299+phlpsong@users.noreply.github.com> --- .../Window/BaseQueryWindow/EZBaseQueryViewController.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Easydict/Feature/ViewController/Window/BaseQueryWindow/EZBaseQueryViewController.m b/Easydict/Feature/ViewController/Window/BaseQueryWindow/EZBaseQueryViewController.m index 5b6388600..ff6bcbdad 100644 --- a/Easydict/Feature/ViewController/Window/BaseQueryWindow/EZBaseQueryViewController.m +++ b/Easydict/Feature/ViewController/Window/BaseQueryWindow/EZBaseQueryViewController.m @@ -193,7 +193,7 @@ - (void)setupUI { }]; }]; - [defaultCenter addObserver:self selector:@selector(modifyLanduage:) name:EZI18nHelper.languagePreferenceChangedNotification object:nil]; + [defaultCenter addObserver:self selector:@selector(modifyLanduage:) name:NSNotification.languagePreferenceChanged object:nil]; } - (void)modifyLanduage:(NSNotification *)notification { From 74d17103d38daaf54d094513025fda3f556844cb Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Sat, 23 Mar 2024 17:04:09 +0800 Subject: [PATCH 08/10] Update Easydict/NewApp/LanguagePreference/I18nHelper.swift Co-authored-by: Phillip Song <103433299+phlpsong@users.noreply.github.com> --- Easydict/NewApp/LanguagePreference/I18nHelper.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Easydict/NewApp/LanguagePreference/I18nHelper.swift b/Easydict/NewApp/LanguagePreference/I18nHelper.swift index 47ebaa348..30b8077b8 100644 --- a/Easydict/NewApp/LanguagePreference/I18nHelper.swift +++ b/Easydict/NewApp/LanguagePreference/I18nHelper.swift @@ -13,7 +13,7 @@ import SwiftUI @objcMembers @objc(EZI18nHelper) class I18nHelper: NSObject { - static let languagePreferenceChangedNotification = "EZLanguagePreferenceChangedNotification" + static let languagePreferenceChangedNotification = "LanguagePreferenceChangedNotification" static let shared = I18nHelper() var localizedBundle: Bundle { From 8b9c82e6cb9c8ed517b5dcdef41cc315c6ab610a Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Sat, 23 Mar 2024 17:04:16 +0800 Subject: [PATCH 09/10] Update Easydict/NewApp/LanguagePreference/I18nHelper.swift Co-authored-by: Phillip Song <103433299+phlpsong@users.noreply.github.com> --- Easydict/NewApp/LanguagePreference/I18nHelper.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Easydict/NewApp/LanguagePreference/I18nHelper.swift b/Easydict/NewApp/LanguagePreference/I18nHelper.swift index 30b8077b8..3fe4d667c 100644 --- a/Easydict/NewApp/LanguagePreference/I18nHelper.swift +++ b/Easydict/NewApp/LanguagePreference/I18nHelper.swift @@ -1,5 +1,5 @@ // -// EZI18nHelper.swift +// I18nHelper.swift // Easydict // // Created by choykarl on 2024/3/4. From 2937538fd9362c9acca5b34b64cea5cc2f673dda Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Sat, 23 Mar 2024 17:14:56 +0800 Subject: [PATCH 10/10] rename --- Easydict/NewApp/LanguagePreference/I18nHelper.swift | 2 +- Easydict/NewApp/LanguagePreference/LanguageState.swift | 4 ++-- Easydict/NewApp/LanguagePreference/LocalizedBundle.swift | 2 +- .../NewApp/LanguagePreference/NSBundle+LanguagePreference.m | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Easydict/NewApp/LanguagePreference/I18nHelper.swift b/Easydict/NewApp/LanguagePreference/I18nHelper.swift index 3fe4d667c..957bd7790 100644 --- a/Easydict/NewApp/LanguagePreference/I18nHelper.swift +++ b/Easydict/NewApp/LanguagePreference/I18nHelper.swift @@ -25,7 +25,7 @@ class I18nHelper: NSObject { } var localizeCode: String { - UserDefaults.standard.string(forKey: kEZLanguagePreferenceLocalKey) ?? LanguageState.LanguageType + UserDefaults.standard.string(forKey: languagePreferenceLocalKey) ?? LanguageState.LanguageType .simplifiedChinese.rawValue } diff --git a/Easydict/NewApp/LanguagePreference/LanguageState.swift b/Easydict/NewApp/LanguagePreference/LanguageState.swift index 2ebb4f3fc..b780ec730 100644 --- a/Easydict/NewApp/LanguagePreference/LanguageState.swift +++ b/Easydict/NewApp/LanguagePreference/LanguageState.swift @@ -10,7 +10,7 @@ import SwiftUI // MARK: - LanguageState -let kEZLanguagePreferenceLocalKey = "kEZLanguagePreferenceLocalKey" +let languagePreferenceLocalKey = "LanguagePreferenceLocalKey" // MARK: - LanguageState @@ -31,7 +31,7 @@ class LanguageState: ObservableObject { } } - @AppStorage(kEZLanguagePreferenceLocalKey) var language: LanguageType = (.init( + @AppStorage(languagePreferenceLocalKey) var language: LanguageType = (.init( rawValue: Locale.current.identifier ) ?? .simplifiedChinese) { didSet { diff --git a/Easydict/NewApp/LanguagePreference/LocalizedBundle.swift b/Easydict/NewApp/LanguagePreference/LocalizedBundle.swift index e532e181c..8e5e3b23d 100644 --- a/Easydict/NewApp/LanguagePreference/LocalizedBundle.swift +++ b/Easydict/NewApp/LanguagePreference/LocalizedBundle.swift @@ -1,5 +1,5 @@ // -// EZLocalizedBundle.swift +// LocalizedBundle.swift // Easydict // // Created by choykarl on 2024/3/23. diff --git a/Easydict/NewApp/LanguagePreference/NSBundle+LanguagePreference.m b/Easydict/NewApp/LanguagePreference/NSBundle+LanguagePreference.m index 0fc7976ad..a6faed7c5 100644 --- a/Easydict/NewApp/LanguagePreference/NSBundle+LanguagePreference.m +++ b/Easydict/NewApp/LanguagePreference/NSBundle+LanguagePreference.m @@ -1,5 +1,5 @@ // -// LanguagePreference.m +// NSBundle+LanguagePreference.m // Easydict // // Created by choykarl on 2024/3/23.