Internationalization
All UI texts in the components come from a locale pack: placeholders, OK and Cancel buttons, empty states, pagination, date panels, form validation messages and texts for screen readers. The default is Simplified Chinese, so nothing changes until you set a locale.
79 languages are built in:
- Simplified Chinese, Traditional Chinese and English are proofread and imported from
@nscode/handy-uidirectly. - The other 76 are imported from
@nscode/handy-ui/locale/<language>, so only the languages you use end up in your bundle.
Setting the Locale for the Whole App
With full registration, pass locale to app.use:
import { createApp } from 'vue'
import HandyUI, { localeEn } from '@nscode/handy-ui'
import '@nscode/handy-ui/style.css'
import App from './App.vue'
createApp(App).use(HandyUI, { locale: localeEn }).mount('#app')
With on-demand import, put an HuiConfigProvider at the outermost level of the root component and place the app inside it:
<script setup lang="ts">
import { HuiConfigProvider, localeEn } from '@nscode/handy-ui'
</script>
<template>
<HuiConfigProvider :locale="localeEn">
<!-- ... -->
</HuiConfigProvider>
</template>
The outermost HuiConfigProvider also sets the language of components created by function calls: HuiMessage, HuiMessageBox, HuiNotification, HuiLoadingBar and the Loading service. If you use both, HuiConfigProvider wins.
Showing Multiple Languages
mergeLocales(locales, separator = ' / ') joins texts in the supplied order on one line, such as “确定 / OK”. Identical texts appear once; empty texts and missing keys are skipped. Arrays such as weekdays and months are merged by position. The original packs stay unchanged. Supply at least one pack; an empty list throws a RangeError.
<script setup lang="ts">
import {
HuiConfigProvider,
HuiEmpty,
HuiPagination,
HuiSpace,
localeEn,
localeZhCn,
mergeLocales
} from '@nscode/handy-ui'
const locale = mergeLocales([localeZhCn, localeEn])
</script>
<template>
<HuiConfigProvider :locale="locale">
<HuiSpace direction="vertical" fill>
<HuiEmpty />
<HuiPagination :total="100" />
</HuiSpace>
</HuiConfigProvider>
</template>
The resulting name is a combined identifier such as zh-cn+en, which cannot be passed to Intl directly. Use the first pack's name when formatting dates or numbers in your app. Component values and date formats remain unchanged. Longer texts need more horizontal space.
Switching at Runtime
locale is reactive. Give it another pack and the texts on the page change at once.
<script setup lang="ts">
import { computed, ref } from 'vue'
import {
HuiConfigProvider,
HuiDatePicker,
HuiPagination,
HuiRadio,
HuiRadioGroup,
HuiSelect,
HuiSpace,
HuiUpload,
localeEn,
localeZhCn,
localeZhTw
} from '@nscode/handy-ui'
const locales = { 'zh-cn': localeZhCn, 'zh-tw': localeZhTw, en: localeEn }
const lang = ref<keyof typeof locales>('en')
const locale = computed(() => locales[lang.value])
const date = ref<Date | null>(null)
const city = ref<string>()
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiRadioGroup v-model="lang">
<HuiRadio value="zh-cn">简体中文</HuiRadio>
<HuiRadio value="zh-tw">繁體中文</HuiRadio>
<HuiRadio value="en">English</HuiRadio>
</HuiRadioGroup>
<HuiConfigProvider :locale="locale">
<HuiSpace direction="vertical" fill>
<HuiSpace>
<HuiDatePicker v-model="date" />
<HuiSelect v-model="city" :options="[]" />
</HuiSpace>
<HuiPagination :total="100" />
<HuiUpload drag :auto-upload="false" />
</HuiSpace>
</HuiConfigProvider>
</HuiSpace>
</template>
If your app has its own translations, derive the component library's locale from the app's current language:
import { computed, ref } from 'vue'
import { localeEn, localeZhCn, localeZhTw } from '@nscode/handy-ui'
// The variable that holds the app's current language
const appLang = ref('zh-cn')
const huiLocales = { 'zh-cn': localeZhCn, 'zh-tw': localeZhTw, en: localeEn }
export const huiLocale = computed(() => huiLocales[appLang.value] ?? localeZhCn)
Then bind it on the root: <HuiConfigProvider :locale="huiLocale">.
Using Other Languages
The other 76 languages are imported from subpaths. The default export is the pack:
import { createApp } from 'vue'
import HandyUI from '@nscode/handy-ui'
import jaJp from '@nscode/handy-ui/locale/ja-jp'
import App from './App.vue'
createApp(App).use(HandyUI, { locale: jaJp }).mount('#app')
To support many languages, load each one only when it is needed. Write every import path as a fixed string, not built from a variable, so the bundler can split each language into its own file:
import { shallowRef } from 'vue'
import { localeZhCn, type HuiLocale } from '@nscode/handy-ui'
const loaders: Record<string, () => Promise<{ default: HuiLocale }>> = {
'ja-jp': () => import('@nscode/handy-ui/locale/ja-jp'),
'ko-kr': () => import('@nscode/handy-ui/locale/ko-kr'),
'fr-fr': () => import('@nscode/handy-ui/locale/fr-fr')
}
export const huiLocale = shallowRef<HuiLocale>(localeZhCn)
export async function switchLanguage(name: string) {
const load = loaders[name]
huiLocale.value = load ? (await load()).default : localeZhCn
}
Supported Languages
Sorted by import path. Machine-translated languages have not been checked by native speakers. To fix an awkward phrase, override it as shown in "Overriding Texts" below.
| Language | Import | Notes |
|---|---|---|
| Afrikaans | @nscode/handy-ui/locale/af-za | Machine-translated |
| Amharic | @nscode/handy-ui/locale/am-et | Machine-translated |
| Arabic | @nscode/handy-ui/locale/ar | Machine-translated; right-to-left (texts only); Modern Standard Arabic |
| Azerbaijani | @nscode/handy-ui/locale/az-az | Machine-translated; Latin script |
| Belarusian | @nscode/handy-ui/locale/be-by | Machine-translated |
| Bulgarian | @nscode/handy-ui/locale/bg-bg | Machine-translated |
| Bengali | @nscode/handy-ui/locale/bn-bd | Machine-translated |
| Catalan | @nscode/handy-ui/locale/ca-es | Machine-translated |
| Czech | @nscode/handy-ui/locale/cs-cz | Machine-translated |
| Danish | @nscode/handy-ui/locale/da-dk | Machine-translated |
| German | @nscode/handy-ui/locale/de-de | Machine-translated |
| Greek | @nscode/handy-ui/locale/el-gr | Machine-translated |
| English | localeEn or @nscode/handy-ui/locale/en | Proofread |
| English (UK) | @nscode/handy-ui/locale/en-gb | Machine-translated; British spelling of the English pack |
| Esperanto | @nscode/handy-ui/locale/eo | Machine-translated |
| Spanish (Argentina) | @nscode/handy-ui/locale/es-ar | Machine-translated |
| Spanish (Spain) | @nscode/handy-ui/locale/es-es | Machine-translated |
| Estonian | @nscode/handy-ui/locale/et-ee | Machine-translated |
| Basque | @nscode/handy-ui/locale/eu-es | Machine-translated |
| Persian | @nscode/handy-ui/locale/fa-ir | Machine-translated; right-to-left (texts only) |
| Finnish | @nscode/handy-ui/locale/fi-fi | Machine-translated |
| Filipino | @nscode/handy-ui/locale/fil-ph | Machine-translated |
| French | @nscode/handy-ui/locale/fr-fr | Machine-translated |
| Galician | @nscode/handy-ui/locale/gl-es | Machine-translated |
| Gujarati | @nscode/handy-ui/locale/gu-in | Machine-translated |
| Hebrew | @nscode/handy-ui/locale/he-il | Machine-translated; right-to-left (texts only) |
| Hindi | @nscode/handy-ui/locale/hi-in | Machine-translated |
| Croatian | @nscode/handy-ui/locale/hr-hr | Machine-translated |
| Hungarian | @nscode/handy-ui/locale/hu-hu | Machine-translated |
| Armenian | @nscode/handy-ui/locale/hy-am | Machine-translated |
| Indonesian | @nscode/handy-ui/locale/id-id | Machine-translated |
| Icelandic | @nscode/handy-ui/locale/is-is | Machine-translated |
| Italian | @nscode/handy-ui/locale/it-it | Machine-translated |
| Japanese | @nscode/handy-ui/locale/ja-jp | Machine-translated |
| Georgian | @nscode/handy-ui/locale/ka-ge | Machine-translated |
| Kazakh | @nscode/handy-ui/locale/kk-kz | Machine-translated; Cyrillic script |
| Khmer | @nscode/handy-ui/locale/km-kh | Machine-translated |
| Kannada | @nscode/handy-ui/locale/kn-in | Machine-translated |
| Korean | @nscode/handy-ui/locale/ko-kr | Machine-translated |
| Kyrgyz | @nscode/handy-ui/locale/ky-kg | Machine-translated |
| Lao | @nscode/handy-ui/locale/lo-la | Machine-translated |
| Lithuanian | @nscode/handy-ui/locale/lt-lt | Machine-translated |
| Latvian | @nscode/handy-ui/locale/lv-lv | Machine-translated |
| Macedonian | @nscode/handy-ui/locale/mk-mk | Machine-translated |
| Malayalam | @nscode/handy-ui/locale/ml-in | Machine-translated |
| Mongolian | @nscode/handy-ui/locale/mn-mn | Machine-translated; Cyrillic script |
| Marathi | @nscode/handy-ui/locale/mr-in | Machine-translated |
| Malay | @nscode/handy-ui/locale/ms-my | Machine-translated |
| Burmese | @nscode/handy-ui/locale/my-mm | Machine-translated |
| Norwegian Bokmål | @nscode/handy-ui/locale/nb-no | Machine-translated |
| Nepali | @nscode/handy-ui/locale/ne-np | Machine-translated |
| Dutch | @nscode/handy-ui/locale/nl-nl | Machine-translated |
| Norwegian Nynorsk | @nscode/handy-ui/locale/nn-no | Machine-translated |
| Punjabi | @nscode/handy-ui/locale/pa-in | Machine-translated; Gurmukhi script |
| Polish | @nscode/handy-ui/locale/pl-pl | Machine-translated |
| Portuguese (Brazil) | @nscode/handy-ui/locale/pt-br | Machine-translated |
| Portuguese (Portugal) | @nscode/handy-ui/locale/pt-pt | Machine-translated |
| Romansh | @nscode/handy-ui/locale/rm-ch | Machine-translated |
| Romanian | @nscode/handy-ui/locale/ro-ro | Machine-translated |
| Russian | @nscode/handy-ui/locale/ru-ru | Machine-translated |
| Sinhala | @nscode/handy-ui/locale/si-lk | Machine-translated |
| Slovak | @nscode/handy-ui/locale/sk-sk | Machine-translated |
| Slovenian | @nscode/handy-ui/locale/sl-si | Machine-translated |
| Albanian | @nscode/handy-ui/locale/sq-al | Machine-translated |
| Serbian | @nscode/handy-ui/locale/sr-rs | Machine-translated; Cyrillic script |
| Swedish | @nscode/handy-ui/locale/sv-se | Machine-translated |
| Swahili | @nscode/handy-ui/locale/sw-ke | Machine-translated |
| Tamil | @nscode/handy-ui/locale/ta-in | Machine-translated |
| Telugu | @nscode/handy-ui/locale/te-in | Machine-translated |
| Thai | @nscode/handy-ui/locale/th-th | Machine-translated |
| Turkish | @nscode/handy-ui/locale/tr-tr | Machine-translated |
| Uyghur | @nscode/handy-ui/locale/ug-cn | Machine-translated; right-to-left (texts only); Arabic script |
| Ukrainian | @nscode/handy-ui/locale/uk-ua | Machine-translated |
| Urdu | @nscode/handy-ui/locale/ur-pk | Machine-translated; right-to-left (texts only) |
| Uzbek | @nscode/handy-ui/locale/uz-uz | Machine-translated; Latin script |
| Vietnamese | @nscode/handy-ui/locale/vi-vn | Machine-translated |
| Simplified Chinese | localeZhCn or @nscode/handy-ui/locale/zh-cn | Proofread; Default |
| Traditional Chinese | localeZhTw or @nscode/handy-ui/locale/zh-tw | Proofread; Taiwan usage |
| Zulu | @nscode/handy-ui/locale/zu-za | Machine-translated |
Using Another Language in Part of a Page
Wrap any part of a page in HuiConfigProvider. Components inside use its language, and nothing outside changes.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiConfigProvider, HuiDatePicker, HuiSpace, localeZhCn } from '@nscode/handy-ui'
const start = ref<Date | null>(null)
const end = ref<Date | null>(null)
</script>
<template>
<HuiSpace>
<HuiDatePicker v-model="start" />
<HuiConfigProvider :locale="localeZhCn">
<HuiDatePicker v-model="end" />
</HuiConfigProvider>
</HuiSpace>
</template>
Overriding Texts
locale can be partial. Keys you leave out fall back to the outer language: the outer HuiConfigProvider, then the locale passed to app.use, then Simplified Chinese. Placeholders such as {total} are replaced with the actual values.
<script setup lang="ts">
import { HuiConfigProvider, HuiPagination } from '@nscode/handy-ui'
const locale = { pagination: { total: '{total} items in total' } }
</script>
<template>
<HuiConfigProvider :locale="locale">
<HuiPagination :total="100" />
</HuiConfigProvider>
</template>
To change a few texts of another language, merge the two into one object, or nest two HuiConfigProviders:
import jaJp from '@nscode/handy-ui/locale/ja-jp'
export const myJa = {
...jaJp,
pagination: { ...jaJp.pagination, total: '全 {total} 件' }
}
To see the keys and their Simplified Chinese texts, look at the HuiLocale type in your editor, or open dist/locale/zh-cn.js in the package.
Props Come First
A text prop set on a component takes priority over the locale pack:
<script setup lang="ts">
import { HuiSelect } from '@nscode/handy-ui'
</script>
<template>
<HuiSelect placeholder="Choose a city" :options="[]" />
</template>
When a text prop is not set, its default follows the language. The Props tables in the component docs show it as "Follows the locale (Simplified Chinese: 请选择)".
Writing Your Own Pack
For a language that is not built in, write a complete pack. Type it as HuiLocale, so type checking reports missing or extra keys. Copying dist/locale/en.js is a good start.
import type { HuiLocale } from '@nscode/handy-ui'
export const localeYue: HuiLocale = {
name: 'yue-hk',
common: { confirm: '確定', cancel: '取消' /* … */ }
// …
}
Lookup Order
A component looks for each text in this order and stops at the first match:
- The text prop set on the component.
- The
localeof the nearestHuiConfigProvider, then each outer one. - The
localeof the outermostHuiConfigProvider, then thelocalepassed toapp.use. - The built-in Simplified Chinese.
Components Created by Function Calls
HuiMessage, HuiMessageBox, HuiNotification, HuiLoadingBar and the Loading service are created separately, so they cannot see the HuiConfigProvider around the place where you call them. They use the outermost HuiConfigProvider's language, then the one passed to app.use, then Simplified Chinese.
If one part of the page uses another language, a message box opened from that part still uses the app's language. To match that part, pass the texts:
HuiMessageBox.confirm('Delete this item?', 'Confirm', {
confirmButtonText: 'OK',
cancelButtonText: 'Cancel'
}).catch(() => {})
Notes
- Dates: The date display format and the first day of the week do not follow the language. Set them with props such as
formatandfirst-day-of-week. - Lunar calendar and solar terms: These are in Chinese only. They stay in Chinese in every language.
- Right-to-left languages: Arabic, Persian, Hebrew, Uyghur and Urdu have their texts translated, but the layout direction of the components does not change.
- Form validation messages: Default messages are generated when validation runs. After switching languages, a message already on screen changes at the next validation.
- Server-side rendering: Set the language with
HuiConfigProvider, not thelocaleoption ofapp.use.app.usesets global state, which requests handled by the server would share. - Several outermost
HuiConfigProviders: This happens, for example, when another app is mounted withcreateApp. Components created by function calls then follow the one mounted last.
Extension locale schema
Extension packages can augment the top-level message schema. Core locale packs gain no new required keys, and locale overrides can still supply partial fields.
import type { HuiLocaleInput } from '@nscode/handy-ui'
declare module '@nscode/handy-ui' {
interface HuiLocaleExtensions {
businessMessages: { saved: string }
}
}
const locale: HuiLocaleInput = { businessMessages: { saved: 'Saved successfully' } }
Companion packages read the current Provider, application and global locale chain through useLocaleChain() from the internal @nscode/handy-ui/internal entry, then use generic createTranslator with their own fallback packs. Call the composable synchronously in setup. It does not register a new global locale root. Empty strings and whole-array overrides retain their existing meaning.
Types
| Name | Description |
|---|---|
HuiLocale | A complete locale pack |
HuiLocaleInput | A pack that may be partial. Used by locale on HuiConfigProvider and app.use |
HandyUIInstallOptions | The options of app.use(HandyUI, options): { locale?: HuiLocaleInput } |