Skip to content

国际化 (i18n)

始于 1.6.0

用于国际化和本地化的类型。

概述

国际化类型提供了用于构建多语言应用程序的类型,包括语言环境管理、翻译、消息格式化和区域设置。它支持复数规则、日期/时间格式化、数字格式化和 RTL 支持。

此模块支持构建具有语言环境、翻译和格式化选项正确类型约束的类型安全 i18n 系统。

基本用法

typescript
import type { Locale, Translation, TranslationKey, Currency, TimeZone, DateFormat } from 'uni-types'

// 语言环境配置
type AppLocale = Locale<{
  code: 'en-US'
  language: 'en'
  country: 'US'
}>

// 翻译映射
type AppTranslation = Translation<{
  welcome: TranslationValue
  greeting: TranslationValue
}>

// 货币格式化
type PriceFormat = Currency<{
  code: 'USD'
  symbol: '$'
}>

核心类型

LocaleCode

语言环境代码类型。

typescript
type LocaleCode = 'en-US' | 'en-GB' | 'zh-CN' | 'zh-TW' | 'ja-JP' | 'ko-KR' | 'de-DE' | 'fr-FR' | 'es-ES' | 'pt-BR' | 'ru-RU' | 'ar-SA' | 'hi-IN' | string

LanguageCode

语言代码类型。

typescript
type LanguageCode = 'en' | 'zh' | 'ja' | 'ko' | 'de' | 'fr' | 'es' | 'pt' | 'ru' | 'ar' | 'hi' | 'it' | 'nl' | 'pl' | 'tr' | string

CountryCode

国家代码类型。

typescript
type CountryCode = 'US' | 'GB' | 'CN' | 'TW' | 'JP' | 'KR' | 'DE' | 'FR' | 'ES' | 'BR' | 'RU' | 'SA' | 'IN' | string

Locale

语言环境配置类型。

typescript
type Locale<T = unknown> = {
  code: LocaleCode
  language: LanguageCode
  country?: CountryCode
  direction: Direction
  config: T
}

Direction

文本方向类型。

typescript
type Direction = 'ltr' | 'rtl'

Translation

翻译映射类型。

typescript
type Translation<T = unknown> = {
  locale: LocaleCode
  messages: TranslationMap<T>
}

PluralForm

复数形式类型。

typescript
type PluralForm = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other'

Currency

货币类型。

typescript
type Currency = 'USD' | 'EUR' | 'GBP' | 'CNY' | 'JPY' | 'KRW' | 'INR' | 'AUD' | 'CAD' | 'CHF' | string

TimeZone

时区类型。

typescript
type TimeZone = 'UTC' | 'America/New_York' | 'Europe/London' | 'Asia/Shanghai' | 'Asia/Tokyo' | string

DateFormat

日期格式配置。

typescript
type DateFormat = {
  format: string
  locale: LocaleCode
  timeZone?: TimeZone
}

NumberFormat

数字格式配置。

typescript
type NumberFormat = {
  style: 'decimal' | 'currency' | 'percent'
  locale: LocaleCode
  currency?: Currency
}

相关

基于 MIT 许可发布