LocaleProvider 使用手册
v1.0.0 · @snowui-design-system/components
许可阅读:首次阅读组件文档、源码或 npm 使用流程,先读完整 LICENSE;同任务同版本去重。公开 API 配置与内部实现修改分别按许可处理;全部组件使用统一运行包,源码权益以许可为准。
组件介绍
为应用或局部区域统一设置 SnowUI 内置界面文案的语言,支持按需覆盖消息内容。
使用选择
为后代组件设置内置文案语言或局部消息覆盖;业务标题和数据仍由应用处理。组件显式文案优先于 Provider,Provider 不产生可见外壳。
locale 选择内置语言,messages 覆盖局部组件文案;组件显式文案优先。
children 在原位置渲染,Provider 不增加外壳;业务标题、数据翻译仍由应用管理。
快速开始
以下示例展示推荐的基本组合。更多场景与组合限制见后文。
API
未重新声明的继承能力请查所属基础组件文档;本表只列本组件源码具名公开属性。
使用边界与详细说明
示例
Site guide: locale-provider
How LocaleProvider works {#overview}
LocaleProvider supplies language context to descendant components. It adds no HTML wrapper, visual style, or layout of its own. Place it around the part of your application that should share a language.
It translates supported built-in messages, such as dialog close labels and color-scheme controls. Your page titles, content, and application messages remain owned by your application.
Import {#import}
LocaleProvider is included in the publicly downloadable Components package under the SnowUI license. Follow Quick Start to install the package and load the shared stylesheet.
Choose a language {#locale}
The default language is zh-CN. Set locale="en-US" for the built-in English catalog. The following color-scheme control receives its accessible label from the surrounding provider.
Change language at runtime {#switch-language}
Pass reactive state to locale to update supported built-in messages without recreating the provider. Store the selected language in your application if it should persist between visits.
Customize built-in messages {#messages}
Use messages to override specific catalog keys inside the provider. Omitted keys continue to use the selected language. Use the component’s explicit label property when the wording belongs to just one instance.
This example changes the two accessible labels used by ColorSchemeButton. Message keys are specific to the component; arbitrary application translation keys are not part of this catalog.
Scope and nested providers {#scope}
Each component reads the nearest LocaleProvider. A nested provider establishes its own locale and messages; it does not merge overrides from its parent. Declare the nested locale explicitly to make that boundary clear.
Here, the first control uses English and the second uses Chinese. Neither setting changes components outside its provider.
Overrides and fallbacks {#fallbacks}
Messages resolve in this order: an explicit component label, provider messages, the selected language catalog, the default zh-CN catalog, and finally the message key.
The current built-in catalogs are zh-CN and en-US. Resolution tries the exact locale, then a registered language-only catalog, then zh-CN. No language-only catalogs are currently registered: use en-US rather than assuming en or en-GB will resolve to English.
Application content and formatting {#application-content}
LocaleProvider does not translate arbitrary children or change the document’s lang or dir attributes. Keep those attributes and your application translations in sync with the language selected by your application.
Use the documented locale and formatting options of date and number components for formatted values. This provider’s public contract covers built-in UI messages; it does not promise to reformat every date or number in the application.
Next steps {#next-steps}
LocaleProvider 样式覆盖
先阅读 完整 LICENSE;同任务、同版本、同范围去重。
优先用组件公开 Props 匹配合适样式,尽可能保留原有视觉。确需改写时按 样式选择与改写顺序 使用自己的 class(含项目已有 Tailwind)或实例 style。以下名称来自源码实际消费的公开 override;先核对目标作用域,不修改私有 DOM。采用 SnowUI Token 时选择正确类别,外部应用不被要求安装一方 utilities。
本组件源文件没有直接消费已登记的专属 override。组合子组件的样式通过公开 parts 或显式 child 配置;不要凭名称猜测变量。
LocaleProvider 类型与状态
必填属性:无具名必填属性。
可双向绑定:本组件未声明具名双向绑定属性;状态更新通过公开回调或所属宿主处理。
继承声明:无接口继承。继承能力以基类公开文档为准;Omit 中的字段已排除,不能按基类原样使用。
以下为公开属性实际引用的类型声明,来自源码;Svelte Snippet 表示内容片段,ComponentProps 表示对应组件的 Props。
SnowUIMessageOverrides
来源:components/src/i18n/messages.ts
SnowUIMessageKey
来源:components/src/i18n/messages.ts