Skip to content

Commit d1dad8c

Browse files
ggzgliAbySwifter
authored andcommitted
【TUILiveKit】【iOS】Support localized string override from main bundle
1 parent fa91f5f commit d1dad8c

1 file changed

Lines changed: 28 additions & 11 deletions

File tree

‎atomic_x/Sources/Widgets/Utils/BundleLoader.swift‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public class BundleLoader {
4242

4343

4444
private static let placeholders = ["xxx", "yyy", "zzz", "mmm", "nnn"]
45-
/// 统一的国际化核心方法
45+
/// 字段未覆盖时,走内置兜底
46+
private static let notFoundValue = "com.atomicx.localized.key.notfound"
47+
/// 统一的国际化核心方法,支持主 bundle 同名表覆盖文案
4648
/// - Parameters:
4749
/// - key: Localizable.strings 中的 Key
4850
/// - bundle: 所在模块的 Bundle
@@ -54,25 +56,40 @@ public class BundleLoader {
5456
in bundle: Bundle,
5557
tableName: String,
5658
arguments: [CVarArg] = []) -> String {
57-
var localizedString = ""
58-
59-
if let path = bundle.path(forResource: getPreferredLanguage(), ofType: "lproj"),
60-
let langBundle = Bundle(path: path) {
61-
localizedString = langBundle.localizedString(forKey: key, value: nil, table: tableName)
62-
} else {
63-
localizedString = bundle.localizedString(forKey: key, value: nil, table: tableName)
64-
}
65-
59+
let localizedString = mainBundleOverride(key: key, tableName: tableName)
60+
?? sdkBundleLocalized(key: key, in: bundle, tableName: tableName)
61+
6662
if arguments.isEmpty {
6763
return localizedString
6864
}
69-
65+
7066
if localizedString.contains("xxx") {
7167
return applyReplacement(origin: localizedString, args: arguments)
7268
} else {
7369
return String(format: localizedString, arguments: arguments)
7470
}
7571
}
72+
73+
private static func mainBundleOverride(key: String, tableName: String) -> String? {
74+
let mainBundle = Bundle.main
75+
if let path = mainBundle.path(forResource: getPreferredLanguage(), ofType: "lproj"),
76+
let langBundle = Bundle(path: path) {
77+
let value = langBundle.localizedString(forKey: key, value: notFoundValue, table: tableName)
78+
if value != notFoundValue && !value.isEmpty {
79+
return value
80+
}
81+
}
82+
let value = mainBundle.localizedString(forKey: key, value: notFoundValue, table: tableName)
83+
return (value != notFoundValue && !value.isEmpty) ? value : nil
84+
}
85+
86+
private static func sdkBundleLocalized(key: String, in bundle: Bundle, tableName: String) -> String {
87+
if let path = bundle.path(forResource: getPreferredLanguage(), ofType: "lproj"),
88+
let langBundle = Bundle(path: path) {
89+
return langBundle.localizedString(forKey: key, value: nil, table: tableName)
90+
}
91+
return bundle.localizedString(forKey: key, value: nil, table: tableName)
92+
}
7693

7794
private static func applyReplacement(origin: String, args: [CVarArg]) -> String {
7895
var result = origin

0 commit comments

Comments
 (0)