@momo-kits/native-kits 0.163.1-sp.4-debug → 0.163.1-sp.5-debug
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +10 -0
- package/compose/build.gradle.kts +1 -1
- package/compose/compose.podspec +1 -1
- package/gradle.properties +1 -1
- package/ios/Application/ApplicationEnvironment.swift +6 -4
- package/ios/Application/LanguageSource.swift +93 -0
- package/ios/Application/Localize.swift +73 -4
- package/ios/Application/LocalizeModifier.swift +30 -0
- package/ios/Application/Navigation/NavigationContainer.swift +19 -18
- package/ios/native-kits.podspec +1 -1
- package/ios-demo/MoMoUIKitsDemo.podspec +1 -1
- package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/LocalizeDemo.swift +51 -4
- package/package.json +1 -1
package/CLAUDE.md
CHANGED
|
@@ -53,6 +53,16 @@ Publishing is automated (`publish.sh` + GitLab CI on `[ci build]` / `main`). Do
|
|
|
53
53
|
- Access in a `@Composable`: `LocalLocalize.current.translate("errorCode")`. Language is host-controlled, never read from the OS locale. `AppLanguage` is still provided (derived from `Localize.currentLanguage`) for older components that read it.
|
|
54
54
|
- Some older components still do ad-hoc `AppLanguage.current` + `Map` lookups; new/changed code should use `LocalLocalize`.
|
|
55
55
|
|
|
56
|
+
### SwiftUI (`ios/Application/Localize.swift`)
|
|
57
|
+
|
|
58
|
+
The SwiftUI port differs from Compose in one important way: **it follows the host app language by itself.**
|
|
59
|
+
|
|
60
|
+
- `Localize.init` takes `languageSource: LanguageSource? = UserDefaultsLanguageSource()`. The default source KVO-observes `UserDefaults.standard["language"]` — the key the MoMo host writes from all three of its writers (the `RNResource` RN bridge, `JSONLocalization`, and the KMP `LocalizationProvider`) — so a language switch anywhere in the app republishes to every consumer. Pass `languageSource: nil` to opt out. The host key literal lives only in `UserDefaultsLanguageSource.defaultKey`.
|
|
61
|
+
- Use `Localize.shared` unless a screen genuinely needs an isolated dictionary; it is also the `\.localize` environment default. Note `addTranslations` on it merges into a process-wide dictionary.
|
|
62
|
+
- **Never set `\.localize` directly.** `EnvironmentValues.localize` stores a `LocalizeBox` snapshot, so consumers invalidate only when the injecting view re-runs its body. Always inject via `.momoLocalize(_:)` (`ios/Application/LocalizeModifier.swift`), which holds the object as `@ObservedObject` — that is what makes the snapshot refresh. `NavigationContainer` already does this; any bare `UIHostingController` root must do it too.
|
|
63
|
+
- Read it with `@Environment(\.localize)`, not `@EnvironmentObject` — the environment default never traps, `@EnvironmentObject` fatal-errors when un-injected.
|
|
64
|
+
- `Localize.normalize(_:)` is the single place that copes with the legacy JSON-quoted values (`"\"vi\""`) the host persists; anything that is not `en` resolves to `vi`.
|
|
65
|
+
|
|
56
66
|
## Code conventions
|
|
57
67
|
|
|
58
68
|
- **Tokens, not literals:** use `Colors`, `Typography`, `Spacing`, `Radius` objects and `AppTheme.current` for colors/styles. Per-component sizing is bundled in enums (e.g. `Button.Size(val value: ButtonSpecs)`).
|
package/compose/build.gradle.kts
CHANGED
package/compose/compose.podspec
CHANGED
package/gradle.properties
CHANGED
|
@@ -38,10 +38,12 @@ public class KitConfig {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
public class ApplicationEnvironment: ObservableObject {
|
|
41
|
-
|
|
42
|
-
let
|
|
43
|
-
let
|
|
44
|
-
let
|
|
41
|
+
// AI-GENERATED START: expose the host bridge to modules outside MoMoUIKits (e.g. the demo pod)
|
|
42
|
+
public let applicationContext: MiniAppContext?
|
|
43
|
+
public let composeApi: KitComposeApi?
|
|
44
|
+
public let config: KitConfig?
|
|
45
|
+
public let maxApi: MaxApi?
|
|
46
|
+
// AI-GENERATED END: expose the host bridge to modules outside MoMoUIKits (e.g. the demo pod)
|
|
45
47
|
|
|
46
48
|
public init(applicationContext: MiniAppContext? = nil, composeApi: KitComposeApi? = nil, config: KitConfig? = nil, maxApi: MaxApi? = nil) {
|
|
47
49
|
self.applicationContext = applicationContext
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// File: ios/Application/LanguageSource.swift
|
|
2
|
+
// Created At: 2026-07-29 10:00:00 +07:00
|
|
3
|
+
// Created By: AI
|
|
4
|
+
// AI Agent: Claude Code
|
|
5
|
+
// Model: claude-opus-5[1m]
|
|
6
|
+
|
|
7
|
+
// AI-GENERATED START: observe the host app's active language so Localize can follow it
|
|
8
|
+
import Foundation
|
|
9
|
+
|
|
10
|
+
/// Read-only view of the host app's active language, plus change delivery.
|
|
11
|
+
/// Values handed to `onChange` are already normalized to `Localize.VI` / `Localize.EN`.
|
|
12
|
+
public protocol LanguageSource: AnyObject {
|
|
13
|
+
var currentLanguage: String { get }
|
|
14
|
+
/// `onChange` may be invoked on any thread; `Localize` dedupes and hops to main.
|
|
15
|
+
func startObserving(_ onChange: @escaping (String) -> Void)
|
|
16
|
+
func stopObserving()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// Reads the language the MoMo host app persists in `UserDefaults`. All three host writers
|
|
20
|
+
/// (the `RNResource` RN bridge, `JSONLocalization`, and the KMP `LocalizationProvider`) write the
|
|
21
|
+
/// same key on `UserDefaults.standard` in-process, so KVO on it sees every one of them.
|
|
22
|
+
public final class UserDefaultsLanguageSource: NSObject, LanguageSource {
|
|
23
|
+
public static let defaultKey = "language"
|
|
24
|
+
|
|
25
|
+
private let defaults: UserDefaults
|
|
26
|
+
private let key: String
|
|
27
|
+
private var onChange: ((String) -> Void)?
|
|
28
|
+
private var isObserving = false
|
|
29
|
+
private var kvoContext = 0
|
|
30
|
+
|
|
31
|
+
/// - Note: `key` must not contain `.` — `forKeyPath:` would read it as a key path.
|
|
32
|
+
public init(
|
|
33
|
+
defaults: UserDefaults = .standard,
|
|
34
|
+
key: String = UserDefaultsLanguageSource.defaultKey
|
|
35
|
+
) {
|
|
36
|
+
self.defaults = defaults
|
|
37
|
+
self.key = key
|
|
38
|
+
super.init()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public var currentLanguage: String {
|
|
42
|
+
Localize.normalize(defaults.string(forKey: key))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public func startObserving(_ onChange: @escaping (String) -> Void) {
|
|
46
|
+
guard !isObserving else { return }
|
|
47
|
+
isObserving = true
|
|
48
|
+
self.onChange = onChange
|
|
49
|
+
defaults.addObserver(self, forKeyPath: key, options: [.new], context: &kvoContext)
|
|
50
|
+
// Safety net for writes KVO can miss (e.g. `removeObject`). Double delivery is free:
|
|
51
|
+
// `Localize` dedupes before touching `@Published`.
|
|
52
|
+
NotificationCenter.default.addObserver(
|
|
53
|
+
self,
|
|
54
|
+
selector: #selector(emit),
|
|
55
|
+
name: UserDefaults.didChangeNotification,
|
|
56
|
+
object: defaults
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public func stopObserving() {
|
|
61
|
+
guard isObserving else { return }
|
|
62
|
+
isObserving = false
|
|
63
|
+
defaults.removeObserver(self, forKeyPath: key, context: &kvoContext)
|
|
64
|
+
NotificationCenter.default.removeObserver(
|
|
65
|
+
self,
|
|
66
|
+
name: UserDefaults.didChangeNotification,
|
|
67
|
+
object: defaults
|
|
68
|
+
)
|
|
69
|
+
onChange = nil
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
deinit {
|
|
73
|
+
stopObserving()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public override func observeValue(
|
|
77
|
+
forKeyPath keyPath: String?,
|
|
78
|
+
of object: Any?,
|
|
79
|
+
change: [NSKeyValueChangeKey: Any]?,
|
|
80
|
+
context: UnsafeMutableRawPointer?
|
|
81
|
+
) {
|
|
82
|
+
guard context == &kvoContext else {
|
|
83
|
+
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
emit()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@objc private func emit() {
|
|
90
|
+
onChange?(currentLanguage)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// AI-GENERATED END: observe the host app's active language so Localize can follow it
|
|
@@ -28,16 +28,69 @@ public final class Localize: ObservableObject {
|
|
|
28
28
|
public static let VI = "vi"
|
|
29
29
|
public static let EN = "en"
|
|
30
30
|
|
|
31
|
+
// AI-GENERATED START: follow the host app language and publish dictionary changes
|
|
32
|
+
/// App-wide instance bound to the host language; the environment default uses it too,
|
|
33
|
+
/// so every screen agrees on one language.
|
|
34
|
+
public static let shared = Localize()
|
|
35
|
+
// AI-GENERATED END: follow the host app language and publish dictionary changes
|
|
36
|
+
|
|
31
37
|
private var assets: LocalizationObject
|
|
32
38
|
@Published public private(set) var currentLanguage: String = Localize.VI
|
|
33
39
|
|
|
34
|
-
|
|
40
|
+
// AI-GENERATED START: follow the host app language and publish dictionary changes
|
|
41
|
+
/// Bumped by `addTranslations` — `assets` is not published, so without this the dictionary
|
|
42
|
+
/// can change without any consumer re-rendering.
|
|
43
|
+
@Published public private(set) var revision: Int = 0
|
|
44
|
+
|
|
45
|
+
private let languageSource: LanguageSource?
|
|
46
|
+
|
|
47
|
+
/// `NavigationContainer` uses this to skip its legacy one-shot `MaxApi` pull.
|
|
48
|
+
var observesAppLanguage: Bool { languageSource != nil }
|
|
49
|
+
|
|
50
|
+
/// Pass `languageSource: nil` to opt out and keep the old "starts at vi" behavior.
|
|
51
|
+
public init(
|
|
52
|
+
_ translations: LocalizationObject = LocalizationObject(),
|
|
53
|
+
languageSource: LanguageSource? = UserDefaultsLanguageSource()
|
|
54
|
+
) {
|
|
35
55
|
self.assets = Localize.merge(base: Localize.defaultLanguage, override: translations)
|
|
56
|
+
self.languageSource = languageSource
|
|
57
|
+
guard let languageSource else { return }
|
|
58
|
+
// Seed synchronously so the first frame is already correct (no vi -> en flash).
|
|
59
|
+
currentLanguage = languageSource.currentLanguage
|
|
60
|
+
languageSource.startObserving { [weak self] language in self?.apply(language) }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
deinit {
|
|
64
|
+
languageSource?.stopObserving()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// Host values are sometimes legacy JSON-quoted (`"\"vi\""`); anything that is not `en` is `vi`.
|
|
68
|
+
public static func normalize(_ raw: String?) -> String {
|
|
69
|
+
let cleaned = (raw ?? "")
|
|
70
|
+
.replacingOccurrences(of: "\"", with: "")
|
|
71
|
+
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
72
|
+
.lowercased()
|
|
73
|
+
return cleaned == Localize.EN ? Localize.EN : Localize.VI
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private func apply(_ language: String) {
|
|
77
|
+
// KVO and didChangeNotification both fire for one write; publish once, on main.
|
|
78
|
+
onMain {
|
|
79
|
+
guard self.currentLanguage != language else { return }
|
|
80
|
+
self.currentLanguage = language
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/// Synchronous when already on main — an async hop would defer the switch by a frame.
|
|
85
|
+
private func onMain(_ work: @escaping () -> Void) {
|
|
86
|
+
if Thread.isMainThread { work() } else { DispatchQueue.main.async(execute: work) }
|
|
36
87
|
}
|
|
37
88
|
|
|
89
|
+
/// A local override: the next host language change supersedes it.
|
|
38
90
|
public func changeLanguage(_ language: String?) {
|
|
39
|
-
|
|
91
|
+
apply(Localize.normalize(language))
|
|
40
92
|
}
|
|
93
|
+
// AI-GENERATED END: follow the host app language and publish dictionary changes
|
|
41
94
|
|
|
42
95
|
public func translate(_ key: String) -> String {
|
|
43
96
|
let dictionary = currentLanguage == Localize.EN ? assets.en : assets.vi
|
|
@@ -58,6 +111,9 @@ public final class Localize: ObservableObject {
|
|
|
58
111
|
/// Existing keys win (mirrors Compose: `merge(translations, assets)`).
|
|
59
112
|
public func addTranslations(_ translations: LocalizationObject) {
|
|
60
113
|
assets = Localize.merge(base: translations, override: assets)
|
|
114
|
+
// AI-GENERATED START: publish dictionary changes so consumers re-render
|
|
115
|
+
onMain { self.revision &+= 1 }
|
|
116
|
+
// AI-GENERATED END: publish dictionary changes so consumers re-render
|
|
61
117
|
}
|
|
62
118
|
|
|
63
119
|
private static func merge(base: LocalizationObject, override: LocalizationObject) -> LocalizationObject {
|
|
@@ -283,14 +339,22 @@ public final class Localize: ObservableObject {
|
|
|
283
339
|
private struct LocalizeBox: Equatable {
|
|
284
340
|
let localize: Localize
|
|
285
341
|
let language: String
|
|
342
|
+
// AI-GENERATED START: invalidate consumers when the dictionary changes, not just the language
|
|
343
|
+
let revision: Int
|
|
344
|
+
// AI-GENERATED END: invalidate consumers when the dictionary changes, not just the language
|
|
286
345
|
|
|
287
346
|
init(_ localize: Localize) {
|
|
288
347
|
self.localize = localize
|
|
289
348
|
self.language = localize.currentLanguage
|
|
349
|
+
// AI-GENERATED START: invalidate consumers when the dictionary changes, not just the language
|
|
350
|
+
self.revision = localize.revision
|
|
351
|
+
// AI-GENERATED END: invalidate consumers when the dictionary changes, not just the language
|
|
290
352
|
}
|
|
291
353
|
|
|
292
354
|
static func == (lhs: LocalizeBox, rhs: LocalizeBox) -> Bool {
|
|
293
|
-
|
|
355
|
+
// AI-GENERATED START: invalidate consumers when the dictionary changes, not just the language
|
|
356
|
+
lhs.localize === rhs.localize && lhs.language == rhs.language && lhs.revision == rhs.revision
|
|
357
|
+
// AI-GENERATED END: invalidate consumers when the dictionary changes, not just the language
|
|
294
358
|
}
|
|
295
359
|
}
|
|
296
360
|
|
|
@@ -298,7 +362,12 @@ private struct LocalizeKey: EnvironmentKey {
|
|
|
298
362
|
/// A default-constructed `Localize` already carries the full kit dictionary, so a
|
|
299
363
|
/// consumer that never injects one renders untranslated-but-correct text instead of
|
|
300
364
|
/// trapping the way `@EnvironmentObject` does.
|
|
301
|
-
|
|
365
|
+
// AI-GENERATED START: default to the app-language-observing shared instance
|
|
366
|
+
/// `defaultValue` is a `static let`, so this box is never rebuilt: without a provider the
|
|
367
|
+
/// language is right on first render but does not live-update. Wrap the root in
|
|
368
|
+
/// `.momoLocalize()` (or use `NavigationContainer`) to get updates.
|
|
369
|
+
static let defaultValue = LocalizeBox(Localize.shared)
|
|
370
|
+
// AI-GENERATED END: default to the app-language-observing shared instance
|
|
302
371
|
}
|
|
303
372
|
|
|
304
373
|
public extension EnvironmentValues {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// File: ios/Application/LocalizeModifier.swift
|
|
2
|
+
// Created At: 2026-07-29 10:00:00 +07:00
|
|
3
|
+
// Created By: AI
|
|
4
|
+
// AI Agent: Claude Code
|
|
5
|
+
// Model: claude-opus-5[1m]
|
|
6
|
+
|
|
7
|
+
// AI-GENERATED START: inject a language-observing Localize into any SwiftUI subtree
|
|
8
|
+
import SwiftUI
|
|
9
|
+
|
|
10
|
+
public extension View {
|
|
11
|
+
/// Injects an app-language-observing `Localize` and re-renders this subtree on change.
|
|
12
|
+
/// Use at every `UIHostingController` root that is not inside a `NavigationContainer`.
|
|
13
|
+
func momoLocalize(_ localize: Localize = .shared) -> some View {
|
|
14
|
+
modifier(MomoLocalizeModifier(localize: localize))
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/// `@ObservedObject` is load-bearing: `@Environment(\.localize)` only invalidates when the boxed
|
|
19
|
+
/// value changes, which requires *this* body to re-run on every publish. A plain helper that just
|
|
20
|
+
/// set `\.localize` would inject a snapshot that never updates.
|
|
21
|
+
private struct MomoLocalizeModifier: ViewModifier {
|
|
22
|
+
@ObservedObject var localize: Localize
|
|
23
|
+
|
|
24
|
+
func body(content: Content) -> some View {
|
|
25
|
+
content
|
|
26
|
+
.environmentObject(localize)
|
|
27
|
+
.environment(\.localize, localize)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// AI-GENERATED END: inject a language-observing Localize into any SwiftUI subtree
|
|
@@ -46,7 +46,9 @@ public struct NavigationContainer<Initial: View>: View {
|
|
|
46
46
|
maxApi: maxApi
|
|
47
47
|
)
|
|
48
48
|
)
|
|
49
|
-
|
|
49
|
+
// AI-GENERATED START: default to the app-language-observing shared Localize
|
|
50
|
+
_localize = StateObject(wrappedValue: localize ?? Localize.shared)
|
|
51
|
+
// AI-GENERATED END: default to the app-language-observing shared Localize
|
|
50
52
|
self.composeApi = composeApi
|
|
51
53
|
self.maxApi = maxApi
|
|
52
54
|
self.setNavigator = setNavigator
|
|
@@ -62,15 +64,17 @@ public struct NavigationContainer<Initial: View>: View {
|
|
|
62
64
|
.environmentObject(navigator)
|
|
63
65
|
.environmentObject(applicationEnvironment)
|
|
64
66
|
.environment(\.applicationEnvironment, applicationEnvironment)
|
|
65
|
-
|
|
66
|
-
.
|
|
67
|
+
// AI-GENERATED START: route Localize injection through the observing modifier
|
|
68
|
+
.momoLocalize(localize)
|
|
69
|
+
// AI-GENERATED END: route Localize injection through the observing modifier
|
|
67
70
|
.fullScreenCover(item: $navigator.presented) { route in
|
|
68
71
|
screenView(forDialog: route)
|
|
69
72
|
.environmentObject(navigator)
|
|
70
73
|
.environmentObject(applicationEnvironment)
|
|
71
74
|
.environment(\.applicationEnvironment, applicationEnvironment)
|
|
72
|
-
|
|
73
|
-
.
|
|
75
|
+
// AI-GENERATED START: route Localize injection through the observing modifier
|
|
76
|
+
.momoLocalize(localize)
|
|
77
|
+
// AI-GENERATED END: route Localize injection through the observing modifier
|
|
74
78
|
}
|
|
75
79
|
.overlay {
|
|
76
80
|
if let item = navigator.overplay {
|
|
@@ -78,8 +82,9 @@ public struct NavigationContainer<Initial: View>: View {
|
|
|
78
82
|
.environmentObject(navigator)
|
|
79
83
|
.environmentObject(applicationEnvironment)
|
|
80
84
|
.environment(\.applicationEnvironment, applicationEnvironment)
|
|
81
|
-
|
|
82
|
-
.
|
|
85
|
+
// AI-GENERATED START: route Localize injection through the observing modifier
|
|
86
|
+
.momoLocalize(localize)
|
|
87
|
+
// AI-GENERATED END: route Localize injection through the observing modifier
|
|
83
88
|
}
|
|
84
89
|
}
|
|
85
90
|
.onAppear {
|
|
@@ -87,13 +92,15 @@ public struct NavigationContainer<Initial: View>: View {
|
|
|
87
92
|
navigator.composeApi = composeApi
|
|
88
93
|
}
|
|
89
94
|
setNavigator?(navigator)
|
|
90
|
-
//
|
|
91
|
-
//
|
|
95
|
+
// AI-GENERATED START: demote the MaxApi language pull to a seed of last resort
|
|
96
|
+
// Only a seed: a Localize with a language source already tracks the host in both
|
|
97
|
+
// directions, so asking maxApi would just race it. `changeLanguage` normalizes the
|
|
98
|
+
// raw response and hops to main itself.
|
|
99
|
+
guard !localize.observesAppLanguage else { return }
|
|
92
100
|
maxApi?.getLanguage { data in
|
|
93
|
-
|
|
94
|
-
localize.changeLanguage(Localize.EN)
|
|
95
|
-
}
|
|
101
|
+
localize.changeLanguage(data?["response"] as? String)
|
|
96
102
|
}
|
|
103
|
+
// AI-GENERATED END: demote the MaxApi language pull to a seed of last resort
|
|
97
104
|
}
|
|
98
105
|
}
|
|
99
106
|
|
|
@@ -195,9 +202,3 @@ private func safeAreaBottomInset() -> CGFloat {
|
|
|
195
202
|
.compactMap { ($0 as? UIWindowScene)?.keyWindow?.safeAreaInsets.bottom }
|
|
196
203
|
.first ?? 0
|
|
197
204
|
}
|
|
198
|
-
|
|
199
|
-
private func parseLanguage(_ raw: [String: Any?]?) -> String? {
|
|
200
|
-
guard let response = (raw?["response"] as? String)?.trimmingCharacters(in: .whitespaces).lowercased(),
|
|
201
|
-
!response.isEmpty else { return nil }
|
|
202
|
-
return response.hasPrefix(Localize.EN) ? Localize.EN : Localize.VI
|
|
203
|
-
}
|
package/ios/native-kits.podspec
CHANGED
|
@@ -24,27 +24,72 @@ private let AppTranslations = LocalizationObject(
|
|
|
24
24
|
)
|
|
25
25
|
|
|
26
26
|
struct LocalizeDemo: View {
|
|
27
|
-
|
|
27
|
+
// AI-GENERATED START: observe the app language and drive it through the real platform API
|
|
28
|
+
@Environment(\.localize) private var localize
|
|
29
|
+
@Environment(\.applicationEnvironment) private var applicationEnvironment
|
|
30
|
+
// AI-GENERATED END: observe the app language and drive it through the real platform API
|
|
28
31
|
|
|
29
32
|
private var isEnglish: Bool { localize.currentLanguage == Localize.EN }
|
|
30
33
|
|
|
34
|
+
// AI-GENERATED START: observe the app language and drive it through the real platform API
|
|
35
|
+
private var storedLanguage: String {
|
|
36
|
+
UserDefaults.standard.string(forKey: UserDefaultsLanguageSource.defaultKey) ?? "nil"
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private func setLanguage(_ language: String) {
|
|
40
|
+
// Platform API -> KMP LanguageApi.SetLanguage -> LocalizationProvider.set, which also
|
|
41
|
+
// mirrors AppleLanguages. KVO then pushes the change back into Localize. The real wire.
|
|
42
|
+
if let composeApi = applicationEnvironment.composeApi {
|
|
43
|
+
_ = composeApi.request(funcName: "setLanguage", params: language)
|
|
44
|
+
} else {
|
|
45
|
+
localize.changeLanguage(language) // #Preview / sample app: no host bridge
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// AI-GENERATED END: observe the app language and drive it through the real platform API
|
|
49
|
+
|
|
31
50
|
var body: some View {
|
|
32
51
|
ScrollView {
|
|
33
52
|
VStack(alignment: .leading, spacing: 0) {
|
|
34
53
|
|
|
35
54
|
LocalizeSectionBox(title: "Đổi ngôn ngữ (toàn app)") {
|
|
36
55
|
MomoText(
|
|
37
|
-
"
|
|
56
|
+
"Switch gọi platform API setLanguage — đổi ngôn ngữ THẬT của app. Localize observe lại "
|
|
57
|
+
+ "qua UserDefaults nên mọi màn hình đang đọc translate() đều cập nhật.",
|
|
38
58
|
typography: .descriptionDefaultRegular
|
|
39
59
|
)
|
|
40
60
|
Switch(
|
|
41
61
|
.constant(isEnglish),
|
|
42
|
-
onChange: { _ in
|
|
62
|
+
onChange: { _ in setLanguage(isEnglish ? Localize.VI : Localize.EN) },
|
|
43
63
|
title: "English"
|
|
44
64
|
)
|
|
45
65
|
MomoText("currentLanguage = \(localize.currentLanguage)", typography: .labelDefaultMedium)
|
|
46
66
|
}
|
|
47
67
|
|
|
68
|
+
// AI-GENERATED START: prove the observation loop, not kit-local state, drives updates
|
|
69
|
+
LocalizeSectionBox(title: "Nguồn thật: UserDefaults") {
|
|
70
|
+
MomoText(
|
|
71
|
+
"Ghi thẳng vào UserDefaults, bỏ qua mọi API của kit — màn hình vẫn đổi, chứng minh "
|
|
72
|
+
+ "Localize đang observe ngôn ngữ app.",
|
|
73
|
+
typography: .descriptionDefaultRegular
|
|
74
|
+
)
|
|
75
|
+
TranslationRow(key: UserDefaultsLanguageSource.defaultKey, value: storedLanguage)
|
|
76
|
+
Button(
|
|
77
|
+
title: "Ghi thẳng UserDefaults = \"en\"",
|
|
78
|
+
action: {
|
|
79
|
+
// Quoted on purpose: proves normalize() strips the JSON quoting
|
|
80
|
+
// RN and KMP persist.
|
|
81
|
+
UserDefaults.standard.set(
|
|
82
|
+
"\"en\"",
|
|
83
|
+
forKey: UserDefaultsLanguageSource.defaultKey
|
|
84
|
+
)
|
|
85
|
+
},
|
|
86
|
+
type: .outline,
|
|
87
|
+
size: .medium,
|
|
88
|
+
isFull: false
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
// AI-GENERATED END: prove the observation loop, not kit-local state, drives updates
|
|
92
|
+
|
|
48
93
|
LocalizeSectionBox(title: "translate(key) — từ điển mặc định của kit") {
|
|
49
94
|
TranslationRow(key: "confirm", value: localize.translate("confirm"))
|
|
50
95
|
TranslationRow(key: "cancel", value: localize.translate("cancel"))
|
|
@@ -179,8 +224,10 @@ private struct TranslationRow: View {
|
|
|
179
224
|
#Preview {
|
|
180
225
|
if #available(iOS 16.0, *) {
|
|
181
226
|
NavigationStack {
|
|
227
|
+
// AI-GENERATED START: preview needs the observing provider or the toggle will not update
|
|
182
228
|
LocalizeDemo()
|
|
183
|
-
.
|
|
229
|
+
.momoLocalize(Localize())
|
|
230
|
+
// AI-GENERATED END: preview needs the observing provider or the toggle will not update
|
|
184
231
|
}
|
|
185
232
|
} else {
|
|
186
233
|
// Fallback on earlier versions
|