多語言
元件庫內建 10 種語言文案包,並支援宿主註冊自訂語言。機制與 web 血脈 @lh-ui/share 的 LhLocale 語義對齊,與 iOS 端的 LhColors 主題層同構。
語言範圍
| 代碼 | 語言 | 備註 |
|---|---|---|
zh-CN | 简体中文 | 缺省語言 |
zh-TW | 繁體中文 | |
en-US | English | |
ja-JP | 日本語 | |
ko-KR | 한국어 | |
de-DE | Deutsch | 僅執行時(文件站九語言不含德語) |
fr-FR | Français | |
es | Español | |
ar | العربية | RTL,由 LhTheme 注入 \.locale 自動翻轉版面 |
ru | Русский |
代碼與 web 血脈
lh-ui-share的LH_LANGUAGE_CONST逐字一致;zh-CN 的 確認/取消取specs/*.json的"确认"/"取消"(R-n5)。
作用範圍(重要)
LhTexts 只承載元件庫自身的內建字串:
- Dialog / Picker 的確認/取消按鈕預設文字(
confirmText/cancelText為nil時) - Loading / Switch 的無障礙標籤,Input / Textarea 未設 placeholder 時的無障礙回退
呼叫方傳入的文字(title / content / label / options …)本地化是宿主 App 的職責—— 這些字串由業務產生,元件庫不代管。
切換語言
swift
// 全域切換:LhTheme 內部以 @ObservedObject 持有單例,切換即整樹重渲染
LhLocale.shared.setLocale("en-US")
// 當前語言代碼
LhLocale.shared.current // "en-US"LhTheme 自動將當前語言包注入 Environment(\.lhTexts) 並聯動 \.locale(ar 自動 RTL)。 未註冊的語言回退簡體中文包內容(與 web 血脈一致)。
自訂語言
swift
let vietnamese = LhTexts(
dialogConfirm: "Xác nhận", dialogCancel: "Hủy",
pickerConfirm: "Xác nhận", pickerCancel: "Hủy",
loadingLabel: "Đang tải", switchLabel: "Công tắc",
inputLabelFallback: "Trường văn bản",
textareaLabelFallback: "Trường văn bản nhiều dòng"
)
LhLocale.shared.register("vi", pack: vietnamese)
LhLocale.shared.setLocale("vi")register 註冊的包遮蔽內建註冊表(同語言的內建包永不被覆寫);若註冊的正是當前語言則立即生效。
LhTexts
swift
public struct LhTexts: Equatable, Sendable {
public let dialogConfirm: String // "确认" / "OK" / "確定" …
public let dialogCancel: String
public let pickerConfirm: String
public let pickerCancel: String
public let loadingLabel: String // 無障礙標籤
public let switchLabel: String
public let inputLabelFallback: String
public let textareaLabelFallback: String
}
public let ZhCnLhTexts = LhTexts(dialogConfirm: "确认", /* … */)
public let EnUsLhTexts = LhTexts(dialogConfirm: "Confirm", /* … */)
// … ZhTw / JaJp / KoKr / DeDe / FrFr / Es / Ar / Ru建構器公開:單測可直接建構文案包做斷言。無 LhTheme 包裹時 Environment 鍵的 defaultValue 回退 ZhCnLhTexts(缺省即中文,呼應主題層的「缺省即明色」)。
與主題層的關係
| 顏色 | 文案 | |
|---|---|---|
| 值型別 | LhColors | LhTexts |
| 預設 | LightLhColors / DarkLhColors | ZhCnLhTexts / EnUsLhTexts … |
| 注入 | Environment(\.lhColors) | Environment(\.lhTexts) |
| 容器 | LhTheme(isDark:) | LhTheme 內部自動(LhLocale.shared) |
| 逐級覆蓋 | .lhColors(_) | .lhTexts(_) |
LhTheme(isDark:colors:texts:) 的 texts 參數提供顯式覆蓋入口(預設文案包,預留擴充)。
文件 App 演示
原生文件宿主(LhDocsView)側邊欄「設定」區提供主題(跟隨系統 / 明色 / 暗色) 與語言(10 內建 + 一個 vi 自訂包演示)切換,可直接驗證多語言行為。

