Skip to content

Commit

Permalink
fix: improve local language (#530)
Browse files Browse the repository at this point in the history
* fix: improve local language

* pref: optimize code
  • Loading branch information
choykarl authored May 2, 2024
1 parent aa9005e commit 1ae0164
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions Easydict/Swift/Feature/Localization/I18nHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion Easydict/Swift/Feature/Localization/LanguageState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1ae0164

Please sign in to comment.