Skip to content

Internationalization ​

The component library ships built-in text packs for 10 languages and lets hosts register custom languages. The mechanism mirrors the web bloodline's @lh-ui/share LhLocale semantics and is structurally identical to the iOS LhColors theming layer.

Language coverage ​

CodeLanguageNotes
zh-CN简体中文Default language
zh-TW繁體中文
en-USEnglish
ja-JP日本語
ko-KR한국어
de-DEDeutschRuntime only (the 9 doc-site languages exclude German)
fr-FRFrançais
esEspañol
arالعربيةRTL; layout flips automatically via the \.locale injected by LhTheme
ruРусский

The codes match the web bloodline lh-ui-share LH_LANGUAGE_CONST verbatim; the zh-CN confirm/cancel values come from specs/*.json ("确认" / "取消", R-n5).

Scope (important) ​

LhTexts only carries the component library's own built-in strings:

  • Dialog / Picker default confirm/cancel button text (when confirmText / cancelText is nil)
  • Loading / Switch accessibility labels, and the Input / Textarea accessibility fallback when no placeholder is set

Localization of caller-supplied text (title / content / label / options …) is the host app's responsibility — those strings come from business code and the library does not manage them.

Switching languages ​

swift
// Global switch: LhTheme holds the singleton via @ObservedObject,
// so switching re-renders the whole tree
LhLocale.shared.setLocale("en-US")

// Current language code
LhLocale.shared.current  // "en-US"

LhTheme automatically injects the current pack into Environment(\.lhTexts) and drives \.locale (ar is RTL automatically). Unregistered languages fall back to the Simplified Chinese pack content (same as the web bloodline).

Custom languages ​

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 packs shadow the built-in registry (built-in packs are never overwritten); if the registered language is the current one, it applies immediately.

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    // accessibility label
  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

The initializer is public so unit tests can construct packs directly. Without a LhTheme wrapper the environment key's defaultValue falls back to ZhCnLhTexts (defaults to Chinese, mirroring the theme layer's "defaults to light").

Relation to the theming layer ​

ColorsTexts
Value typeLhColorsLhTexts
PresetsLightLhColors / DarkLhColorsZhCnLhTexts / EnUsLhTexts …
InjectionEnvironment(\.lhColors)Environment(\.lhTexts)
ContainerLhTheme(isDark:)LhTheme internally (LhLocale.shared)
Scoped override.lhColors(_).lhTexts(_)

The texts parameter of LhTheme(isDark:colors:texts:) provides an explicit override entry point (preset text packs, reserved extension).

Docs app demo ​

The native docs host (LhDocsView) provides theme (system / light / dark) and language (10 built-ins plus a vi custom-pack demo) switching in the sidebar "设置" section for verifying multilingual behavior directly.

lh-ui native bloodline · iOS (Swift + SwiftUI)