-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* modify localized bundle pointer by runtime * swiftui localized * 删除log * use swift to implement EZLocalizedBundle * remove the EZ prefix in swift. * optimize code * Update Easydict/Feature/ViewController/Window/BaseQueryWindow/EZBaseQueryViewController.m Co-authored-by: Phillip Song <103433299+phlpsong@users.noreply.github.com> * Update Easydict/NewApp/LanguagePreference/I18nHelper.swift Co-authored-by: Phillip Song <103433299+phlpsong@users.noreply.github.com> * Update Easydict/NewApp/LanguagePreference/I18nHelper.swift Co-authored-by: Phillip Song <103433299+phlpsong@users.noreply.github.com> * rename --------- Co-authored-by: Phillip Song <103433299+phlpsong@users.noreply.github.com> Co-authored-by: Tisfeng <tisfeng@gmail.com>
- Loading branch information
1 parent
b90b336
commit a86f56c
Showing
15 changed files
with
207 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,5 @@ | |
#import "EZConst.h" | ||
#import "EZConstKey.h" | ||
#import "NSString+EZConvenience.h" | ||
|
||
#import "Easydict-Swift.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// I18nHelper.swift | ||
// Easydict | ||
// | ||
// Created by choykarl on 2024/3/4. | ||
// Copyright © 2024 izual. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
// MARK: - I18nHelper | ||
|
||
@objcMembers | ||
@objc(EZI18nHelper) | ||
class I18nHelper: NSObject { | ||
static let languagePreferenceChangedNotification = "LanguagePreferenceChangedNotification" | ||
static let shared = I18nHelper() | ||
|
||
var localizedBundle: Bundle { | ||
guard let path = Bundle.main.path(forResource: localizeCode, ofType: "lproj"), | ||
let bundle = Bundle(path: path) else { | ||
return .main | ||
} | ||
return bundle | ||
} | ||
|
||
var localizeCode: String { | ||
UserDefaults.standard.string(forKey: languagePreferenceLocalKey) ?? LanguageState.LanguageType | ||
.simplifiedChinese.rawValue | ||
} | ||
|
||
var languageType: LanguageState.LanguageType { | ||
LanguageState.LanguageType(rawValue: localizeCode) ?? .simplifiedChinese | ||
} | ||
|
||
var isSimplifiedChineseLocalize: Bool { | ||
localizeCode == LanguageState.LanguageType.simplifiedChinese.rawValue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// LanguageState.swift | ||
// Easydict | ||
// | ||
// Created by choykarl on 2024/3/3. | ||
// Copyright © 2024 izual. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
// MARK: - LanguageState | ||
|
||
let languagePreferenceLocalKey = "LanguagePreferenceLocalKey" | ||
|
||
// 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(languagePreferenceLocalKey) var language: LanguageType = (.init( | ||
rawValue: Locale.current.identifier | ||
) ?? .simplifiedChinese) { | ||
didSet { | ||
NotificationCenter.default.post(name: .languagePreferenceChanged, object: nil) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// LocalizedBundle.swift | ||
// Easydict | ||
// | ||
// Created by choykarl on 2024/3/23. | ||
// Copyright © 2024 izual. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
@objc(EZLocalizedBundle) | ||
class LocalizedBundle: Bundle { | ||
override func localizedString(forKey key: String, value: String?, table tableName: String?) -> String { | ||
I18nHelper.shared.localizedBundle.localizedString(forKey: key, value: value, table: tableName) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Easydict/NewApp/LanguagePreference/NSBundle+LanguagePreference.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// NSBundle+LanguagePreference.m | ||
// Easydict | ||
// | ||
// Created by choykarl on 2024/3/23. | ||
// Copyright © 2024 izual. All rights reserved. | ||
// | ||
|
||
#import <objc/runtime.h> | ||
|
||
@implementation NSBundle (LanguagePreference) | ||
|
||
+ (void)load { | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
object_setClass([NSBundle mainBundle], [EZLocalizedBundle class]); | ||
}); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.