定价

LocaleProvider 使用手册

v1.0.0 · @snowui-design-system/components

许可阅读:首次阅读组件文档、源码或 npm 使用流程,先读完整 LICENSE;同任务同版本去重。公开 API 配置与内部实现修改分别按许可处理;全部组件使用统一运行包,源码权益以许可为准。

组件介绍

为应用或局部区域统一设置 SnowUI 内置界面文案的语言,支持按需覆盖消息内容。

使用选择

为后代组件设置内置文案语言或局部消息覆盖;业务标题和数据仍由应用处理。组件显式文案优先于 Provider,Provider 不产生可见外壳。

  • locale 选择内置语言,messages 覆盖局部组件文案;组件显式文案优先。

  • children 在原位置渲染,Provider 不增加外壳;业务标题、数据翻译仍由应用管理。

快速开始

以下示例展示推荐的基本组合。更多场景与组合限制见后文。

<script lang="ts">	import { LocaleProvider, Frame, Pagination, Text } from '@snowui-design-system/components';</script> <LocaleProvider locale="en-US">	<Frame column style="align-items: stretch; gap: var(--gap-2); width: min(100%, 32rem);">		<Text weight={600}>English documentation controls</Text>		<Pagination page={2} total={6} widthMode="hug" ariaLabel="Component documentation pages" />	</Frame></LocaleProvider>

API

关键字 类型 默认值 描述
locale string "zh-CN" 当前语言标识;内置 zh-CN、en-US,未知语言按语言主标签再回退 zh-CN。
messages SnowUIMessageOverrides {} 仅覆盖当前 Provider 子树的内置 message key。
children Snippet — 当前 locale 和 messages 生效的子树,无参数;Provider 不添加 DOM 外壳。

未重新声明的继承能力请查所属基础组件文档;本表只列本组件源码具名公开属性。

使用边界与详细说明

示例

<LocaleProvider locale="en-US">    <SidePanel open editable /></LocaleProvider>

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.

import { LocaleProvider } from '@snowui-design-system/components';

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.

<script lang="ts">  import { ColorSchemeButton, LocaleProvider } from "@snowui-design-system/components";</script> <LocaleProvider locale="en-US">  <ColorSchemeButton /></LocaleProvider>

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.

<script lang="ts">  import { ColorSchemeButton, LocaleProvider } from "@snowui-design-system/components";  let locale = $state("en-US");</script> <label>  Interface language  <select bind:value={locale}>    <option value="en-US">English</option>    <option value="zh-CN">简体中文</option>  </select></label> <LocaleProvider {locale}>  <ColorSchemeButton /></LocaleProvider>

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.

<script lang="ts">  import { ColorSchemeButton, LocaleProvider } from "@snowui-design-system/components";</script> <LocaleProvider locale="en-US" messages={{  "colorSchemeButton.toLight": "Use light appearance",  "colorSchemeButton.toDark": "Use dark appearance"}}>  <ColorSchemeButton /></LocaleProvider>

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.

<script lang="ts">  import { ColorSchemeButton, LocaleProvider } from "@snowui-design-system/components";</script> <LocaleProvider locale="en-US">  <ColorSchemeButton />  <LocaleProvider locale="zh-CN">    <ColorSchemeButton />  </LocaleProvider></LocaleProvider>

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 配置;不要凭名称猜测变量。

设计指南 · Token · 组合边界

LocaleProvider 类型与状态

必填属性:无具名必填属性。

可双向绑定:本组件未声明具名双向绑定属性;状态更新通过公开回调或所属宿主处理。

继承声明:无接口继承。继承能力以基类公开文档为准;Omit 中的字段已排除,不能按基类原样使用。

共享 Frame API · 返回 API

以下为公开属性实际引用的类型声明,来自源码;Svelte Snippet 表示内容片段,ComponentProps 表示对应组件的 Props。

SnowUIMessageOverrides

来源:components/src/i18n/messages.ts

export type SnowUIMessageOverrides = Partial<Record<SnowUIMessageKey, string>>;

SnowUIMessageKey

来源:components/src/i18n/messages.ts

export type SnowUIMessageKey =    | "copyButton.copy"    | "copyButton.copied"    | "codeBlock.copyCode"    | "codeBlock.fullscreen"    | "codeBlock.exitFullscreen"    | "codeBlock.resize"    | "codeBlock.fullscreenDialog"    | "expandButton.expand"    | "expandButton.collapse"    | "sidebarSection.expand"    | "sidebarSection.collapse"    | "breadcrumb.navigation"    | "pagination.navigation"    | "spinner.loading"    | "scrollArea.content"    | "barChart.name"    | "barChart.cells"    | "lineChart.name"    | "lineChart.cells"    | "circularChart.name"    | "gaugeChart.name"    | "chartGroup.name"    | "chartGroup.cells"    | "chartGroup.cell"    | "searchBox.placeholder"    | "searchBox.clear"    | "sidePanel.title"    | "slider.title"    | "menuItem.deleteCustom"    | "listbox.search"    | "tag.close"    | "sidebar.navigation"    | "sidebar.expand"    | "sidebar.collapse"    | "sidebarItem.updated"    | "sidebar.resize"    | "pagination.pageSize"    | "pagination.previous"    | "pagination.next"    | "pagination.page"    | "otpInput.invalidCode"    | "input.clear"    | "input.increment"    | "input.decrement"    | "input.openTime"    | "input.openDate"    | "input.year"    | "input.month"    | "input.day"    | "input.week"    | "input.hour"    | "input.minute"    | "input.second"    | "input.yearPlaceholder"    | "input.monthPlaceholder"    | "input.dayPlaceholder"    | "input.weekPlaceholder"    | "imageViewer.previous"    | "imageViewer.next"    | "imageViewer.close"    | "toast.close"    | "table.scrollRegion"    | "table.scrollRegionNamed"    | "tableHead.selectAll"    | "tableHead.resizeColumn"    | "tableHead.resizeColumnGeneric"    | "tableCell.selectRow"    | "text.expand"    | "textarea.resize"    | "indicator.resizeWidth"    | "indicator.resizeHeight"    | "indicator.resizeBoth"    | "colorSchemeButton.toLight"    | "colorSchemeButton.toDark"    | "styleButton.toSnowUI"    | "styleButton.toApple"    | "sidePanel.placement.toInline"    | "sidePanel.placement.toFloating"    | "sidePanel.previous"    | "sidePanel.next"    | "sidePanel.copy"    | "sidePanel.delete"    | "sidePanel.cancel"    | "sidePanel.save"    | "sidePanel.close"    | "sidePanel.resize"    | "tableToolbar.name"    | "tableToolbar.search"    | "tableToolbar.sort"    | "tableToolbar.filter"    | "tableToolbar.clear"    | "tableToolbar.copy"    | "tableToolbar.delete"    | "tableToolbar.selected"    | "tableToolbar.deleting"    | "tableToolbar.defaultSort"    | "tableToolbar.ascending"    | "tableToolbar.descending"    | "tableToolbar.defaultFilter"    | "overflowActions.more"    | "dialog.close";