| name | naive-ui-i18n |
| description | Configure internationalization (i18n) in Naive UI with support for multiple languages and custom locales |
| metadata | {"author":"jiaiyan","version":"1.0.0"} |
Naive UI Internationalization (i18n)
Configure internationalization in Naive UI to support multiple languages and customize component locales.
When to Use
Use this skill when you need to:
- Set up multi-language support in your application
- Change the language of Naive UI components
- Customize existing locale strings
- Add support for new languages
Prerequisites
- Basic understanding of Naive UI setup
- Vue 3 Composition API knowledge
- Understanding of internationalization concepts
Basic Usage
Setting Up Locale
Use n-config-provider to set the locale for all descendant components:
<template>
<n-config-provider :locale="enUS" :date-locale="dateEnUS">
<App />
</n-config-provider>
</template>
<script setup>
import { NConfigProvider } from 'naive-ui'
import { enUS, dateEnUS } from 'naive-ui'
</script>
Switching Languages Dynamically
<template>
<n-config-provider :locale="locale" :date-locale="dateLocale">
<n-space vertical>
<n-select
v-model:value="currentLang"
:options="langOptions"
@update:value="handleLangChange"
/>
<n-date-picker type="date" />
<n-pagination :page-count="10" />
</n-space>
</n-config-provider>
</template>
<script setup>
import { ref } from 'vue'
import { enUS, dateEnUS, zhCN, dateZhCN } from 'naive-ui'
const currentLang = ref('en')
const locale = ref(enUS)
const dateLocale = ref(dateEnUS)
const langOptions = [
{ label: 'English', value: 'en' },
{ label: '中文', value: 'zh' }
]
const handleLangChange = (lang) => {
if (lang === 'en') {
locale.value = enUS
dateLocale.value = dateEnUS
} else if (lang === 'zh') {
locale.value = zhCN
dateLocale.value = dateZhCN
}
}
</script>
Supported Languages
Naive UI supports the following languages (PRs are welcome for new languages):
| Language | Config | Date Config | Version |
|---|
| Arabic (العربية) | arDZ | dateArDZ | 2.34.0 |
| Azerbaijani (Azərbaycanca) | azAZ | dateAzAZ | 2.39.0 |
| Czech (Czechia) | csCZ | dateCsCz | 2.38.2 |
| Danish (Denmark) | daDK | dateDaDK | 2.43.0 |
| German (Germany) | deDE | dateDeDE | - |
| English (British) | enGB | dateEnGB | 2.25.1 |
| English | enUS | dateEnUS | - |
| Esperanto | eo | dateEo | 2.25.2 |
| Spanish (Argentina) | esAR | dateEsAR | 2.24.2 |
| Estonian | etEE | dateEtEE | 2.38.0 |
| Persian | faIR | dateFaIR | 2.34.4 |
| French | frFR | dateFrFR | - |
| Bahasa Indonesia | idID | dateIdID | - |
| Italiano | itIT | dateItIT | 2.24.2 |
| Japanese | jaJP | dateJaJP | - |
| Khmer (Cambodia) | kmKH | dateKmKH | 2.41.0 |
|
Customizing Locales
Using createLocale
Customize existing locale strings using createLocale:
<template>
<n-config-provider :locale="customLocale" :date-locale="dateEnUS">
<n-input placeholder="This should show 'Okay'" />
</n-config-provider>
</template>
<script setup>
import { NConfigProvider, createLocale, enUS, dateEnUS } from 'naive-ui'
const customLocale = createLocale(
{
Input: {
placeholder: 'Okay'
}
},
enUS
)
</script>
Full Locale Customization Example
<template>
<n-config-provider :locale="customLocale">
<n-space vertical>
<n-input :placeholder="customLocale.Input.placeholder" />
<n-empty :description="customLocale.Empty.description" />
<n-pagination :page-count="5" />
</n-space>
</n-config-provider>
</template>
<script setup>
import { createLocale, enUS } from 'naive-ui'
const customLocale = createLocale(
{
Input: {
placeholder: 'Enter text here...'
},
Empty: {
description: 'No data available'
},
Pagination: {
goto: 'Go to',
pagesize: '/page',
total: 'Total {count} items',
pageSeparator: 'of',
page: 'Page',
prev: 'Previous',
next: 'Next',
prevPage: 'Previous page',
nextPage: 'Next page',
prev: 'Prev',
next: 'Next'
}
},
enUS
)
</script>
API Reference
n-config-provider Locale Props
| Property | Type | Default | Description |
|---|
locale | object | enUS | Locale configuration for components |
date-locale | object | dateEnUS | Date format locale configuration |
createLocale Function
function createLocale(
overrides: PartialLocale,
baseLocale: Locale
): Locale
Creates a new locale by merging custom overrides with a base locale.
Common Patterns
Persistent Language Selection
<template>
<n-config-provider :locale="locale" :date-locale="dateLocale">
<n-select
v-model:value="currentLang"
:options="langOptions"
style="width: 200px; margin-bottom: 20px;"
/>
<router-view />
</n-config-provider>
</template>
<script setup>
import { ref, onMounted, watch } from 'vue'
import { enUS, dateEnUS, zhCN, dateZhCN, jaJP, dateJaJP } from 'naive-ui'
const langMap = {
en: { locale: enUS, dateLocale: dateEnUS },
zh: { locale: zhCN, dateLocale: dateZhCN },
ja: { locale: jaJP, dateLocale: dateJaJP }
}
const currentLang = ref('en')
const locale = ref(enUS)
const dateLocale = ref(dateEnUS)
const langOptions = [
{ label: 'English', value: 'en' },
{ label: '中文', value: 'zh' },
{ label: '日本語', value: 'ja' }
]
watch(currentLang, (newLang) => {
const config = langMap[newLang]
if (config) {
locale.value = config.locale
dateLocale.value = config.dateLocale
localStorage.setItem('app-language', newLang)
}
})
onMounted(() => {
const savedLang = localStorage.getItem('app-language') || 'en'
currentLang.value = savedLang
})
</script>
Integration with vue-i18n
<template>
<n-config-provider :locale="naiveLocale" :date-locale="naiveDateLocale">
<App />
</n-config-provider>
</template>
<script setup>
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { enUS, dateEnUS, zhCN, dateZhCN } from 'naive-ui'
const { locale } = useI18n()
const naiveLocale = computed(() => {
return locale.value === 'zh' ? zhCN : enUS
})
const naiveDateLocale = computed(() => {
return locale.value === 'zh' ? dateZhCN : dateEnUS
})
</script>
Best Practices
- Persist Language Preference: Store the user's language choice in localStorage
- Separate Date Locale: Always set both
locale and date-locale for consistent formatting
- Use Base Locale: When customizing, always extend from an existing locale using
createLocale
- Test All Languages: Verify your application works correctly with all supported languages
- Consider RTL: Some languages (Arabic, Persian) require right-to-left layout support
- Sync with App i18n: Keep Naive UI locale in sync with your application's i18n system
Component-Specific Locale Keys
Different components have their own locale keys that can be customized:
const customLocale = createLocale({
Button: {
loadingText: 'Loading...'
},
Input: {
placeholder: 'Please input',
clear: 'Clear'
},
DatePicker: {
placeholder: 'Select date',
clear: 'Clear',
confirm: 'OK',
now: 'Now'
},
DataTable: {
check: 'Check',
clear: 'Clear',
clearFilters: 'Clear filters',
clearSorter: 'Clear sorter'
},
Transfer: {
source: 'Source',
target: 'Target',
searchPlaceholder: 'Search',
clear: 'Clear'
},
Upload: {
draggerClick: 'Click to upload',
draggerDrag: 'Drag files here',
fileDrag: 'Drag files here',
accept: 'Accept: {accept}',
: ,
: ,
: ,
:
}
}, enUS)