From 1ae01641f89086f94efcc322689725961cc9c7a0 Mon Sep 17 00:00:00 2001 From: choykarl <253440030@qq.com> Date: Thu, 2 May 2024 21:00:40 +0800 Subject: [PATCH] fix: improve local language (#530) * fix: improve local language * pref: optimize code --- .../Swift/Feature/Localization/I18nHelper.swift | 17 ++++++++++++++--- .../Feature/Localization/LanguageState.swift | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Easydict/Swift/Feature/Localization/I18nHelper.swift b/Easydict/Swift/Feature/Localization/I18nHelper.swift index 957bd7790..0b33e73b5 100644 --- a/Easydict/Swift/Feature/Localization/I18nHelper.swift +++ b/Easydict/Swift/Feature/Localization/I18nHelper.swift @@ -25,12 +25,23 @@ class I18nHelper: NSObject { } var localizeCode: String { - UserDefaults.standard.string(forKey: languagePreferenceLocalKey) ?? LanguageState.LanguageType - .simplifiedChinese.rawValue + if let code = UserDefaults.standard.string(forKey: languagePreferenceLocalKey) { + return code + } + + if let localLanguageType = LanguageState.LanguageType(rawValue: Locale.current.identifier) { + return localLanguageType.rawValue + } + + if Locale.current.identifier == "zh_CN" { + return LanguageState.LanguageType.simplifiedChinese.rawValue + } + + return LanguageState.LanguageType.english.rawValue } var languageType: LanguageState.LanguageType { - LanguageState.LanguageType(rawValue: localizeCode) ?? .simplifiedChinese + LanguageState.LanguageType(rawValue: localizeCode) ?? .english } var isSimplifiedChineseLocalize: Bool { diff --git a/Easydict/Swift/Feature/Localization/LanguageState.swift b/Easydict/Swift/Feature/Localization/LanguageState.swift index b780ec730..5ff231133 100644 --- a/Easydict/Swift/Feature/Localization/LanguageState.swift +++ b/Easydict/Swift/Feature/Localization/LanguageState.swift @@ -32,7 +32,7 @@ class LanguageState: ObservableObject { } @AppStorage(languagePreferenceLocalKey) var language: LanguageType = (.init( - rawValue: Locale.current.identifier + rawValue: I18nHelper.shared.localizeCode ) ?? .simplifiedChinese) { didSet { NotificationCenter.default.post(name: .languagePreferenceChanged, object: nil)