mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-31 04:07:56 +01:00
feat(locale): provide code (#2611)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { existsSync, promises as fsp } from 'node:fs'
|
import { existsSync, promises as fsp } from 'node:fs'
|
||||||
import { resolve } from 'pathe'
|
import { resolve } from 'pathe'
|
||||||
import { consola } from 'consola'
|
import { consola } from 'consola'
|
||||||
import { appendFile, sortFile } from '../../utils.mjs'
|
import { appendFile, sortFile, normalizeLocale } from '../../utils.mjs'
|
||||||
import { defineCommand } from 'citty'
|
import { defineCommand } from 'citty'
|
||||||
|
|
||||||
export default defineCommand({
|
export default defineCommand({
|
||||||
@@ -45,7 +45,7 @@ export default defineCommand({
|
|||||||
// Create new locale file
|
// Create new locale file
|
||||||
await fsp.copyFile(originLocaleFilePath, newLocaleFilePath)
|
await fsp.copyFile(originLocaleFilePath, newLocaleFilePath)
|
||||||
const localeFile = await fsp.readFile(newLocaleFilePath, 'utf-8')
|
const localeFile = await fsp.readFile(newLocaleFilePath, 'utf-8')
|
||||||
const rewrittenLocaleFile = localeFile.replace(/defineLocale\('(.*)'/, `defineLocale('${args.name}'`)
|
const rewrittenLocaleFile = localeFile.replace(/defineLocale\('(.*)'/, `defineLocale('${args.name}', '${normalizeLocale(args.code)}'`)
|
||||||
await fsp.writeFile(newLocaleFilePath, rewrittenLocaleFile)
|
await fsp.writeFile(newLocaleFilePath, rewrittenLocaleFile)
|
||||||
|
|
||||||
consola.success(`🪄 Generated ${newLocaleFilePath}`)
|
consola.success(`🪄 Generated ${newLocaleFilePath}`)
|
||||||
|
|||||||
@@ -15,3 +15,17 @@ export async function appendFile(path, contents) {
|
|||||||
await fsp.writeFile(path, file.trim() + '\n' + contents + '\n')
|
await fsp.writeFile(path, file.trim() + '\n' + contents + '\n')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function normalizeLocale(locale) {
|
||||||
|
if (!locale) {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
|
if (locale.includes('_')) {
|
||||||
|
return locale.split('_')
|
||||||
|
.map((part, index) => index === 0 ? part.toLowerCase() : part.toUpperCase())
|
||||||
|
.join('-')
|
||||||
|
}
|
||||||
|
|
||||||
|
return locale.toLowerCase()
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ You also have the option to add your own locale using `defineLocale`:
|
|||||||
|
|
||||||
```vue [app.vue]
|
```vue [app.vue]
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
const locale = defineLocale('My custom locale', {
|
const locale = defineLocale('My custom locale', 'en', {
|
||||||
// implement pairs
|
// implement pairs
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
@@ -50,6 +50,12 @@ const locale = defineLocale('My custom locale', {
|
|||||||
</template>
|
</template>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
::tip
|
||||||
|
Look at the second parameter, there you need to pass the iso code of the language. Example:
|
||||||
|
* `hi` Hindi (language)
|
||||||
|
* `de-AT`: German (language) as used in Austria (region)
|
||||||
|
::
|
||||||
|
|
||||||
### Dynamic locale
|
### Dynamic locale
|
||||||
|
|
||||||
To dynamically switch between languages, you can use the [Nuxt I18n](https://i18n.nuxtjs.org/) module.
|
To dynamically switch between languages, you can use the [Nuxt I18n](https://i18n.nuxtjs.org/) module.
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ You also have the option to add your locale using `defineLocale`:
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { defineLocale } from '@nuxt/ui/runtime/composables/defineLocale'
|
import { defineLocale } from '@nuxt/ui/runtime/composables/defineLocale'
|
||||||
|
|
||||||
const locale = defineLocale('My custom locale', {
|
const locale = defineLocale('My custom locale', 'en', {
|
||||||
// implement pairs
|
// implement pairs
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
@@ -52,6 +52,12 @@ const locale = defineLocale('My custom locale', {
|
|||||||
</template>
|
</template>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
::tip
|
||||||
|
Look at the second parameter, there you need to pass the iso code of the language. Example:
|
||||||
|
* `hi` Hindi (language)
|
||||||
|
* `de-AT`: German (language) as used in Austria (region)
|
||||||
|
::
|
||||||
|
|
||||||
### Dynamic locale
|
### Dynamic locale
|
||||||
|
|
||||||
To dynamically switch between languages, you can use the [Vue I18n](https://vue-i18n.intlify.dev/) plugin.
|
To dynamically switch between languages, you can use the [Vue I18n](https://vue-i18n.intlify.dev/) plugin.
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import type { Locale, LocalePair } from '../types/locale'
|
import type { Locale, LocalePair } from '../types/locale'
|
||||||
|
|
||||||
export function defineLocale(name: string, pair: LocalePair): Locale {
|
export function defineLocale(name: string, code: string, pair: LocalePair): Locale {
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
|
code,
|
||||||
ui: pair
|
ui: pair
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { defineLocale } from '../composables/defineLocale'
|
import { defineLocale } from '../composables/defineLocale'
|
||||||
|
|
||||||
export default defineLocale('العربية', {
|
export default defineLocale('العربية', 'ar', {
|
||||||
inputMenu: {
|
inputMenu: {
|
||||||
noMatch: 'لا توجد نتائج مطابقة',
|
noMatch: 'لا توجد نتائج مطابقة',
|
||||||
noData: 'لا توجد بيانات'
|
noData: 'لا توجد بيانات'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { defineLocale } from '../composables/defineLocale'
|
import { defineLocale } from '../composables/defineLocale'
|
||||||
|
|
||||||
export default defineLocale('Čeština', {
|
export default defineLocale('Čeština', 'cs', {
|
||||||
inputMenu: {
|
inputMenu: {
|
||||||
noMatch: 'Žádná shoda',
|
noMatch: 'Žádná shoda',
|
||||||
noData: 'Žádná data'
|
noData: 'Žádná data'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { defineLocale } from '../composables/defineLocale'
|
import { defineLocale } from '../composables/defineLocale'
|
||||||
|
|
||||||
export default defineLocale('Deutsch', {
|
export default defineLocale('Deutsch', 'de', {
|
||||||
inputMenu: {
|
inputMenu: {
|
||||||
noMatch: 'Nichts gefunden',
|
noMatch: 'Nichts gefunden',
|
||||||
noData: 'Keine Daten'
|
noData: 'Keine Daten'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { defineLocale } from '../composables/defineLocale'
|
import { defineLocale } from '../composables/defineLocale'
|
||||||
|
|
||||||
export default defineLocale('English', {
|
export default defineLocale('English', 'en', {
|
||||||
inputMenu: {
|
inputMenu: {
|
||||||
noMatch: 'No matching data',
|
noMatch: 'No matching data',
|
||||||
noData: 'No data'
|
noData: 'No data'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { defineLocale } from '../composables/defineLocale'
|
import { defineLocale } from '../composables/defineLocale'
|
||||||
|
|
||||||
export default defineLocale('Français', {
|
export default defineLocale('Français', 'fr', {
|
||||||
inputMenu: {
|
inputMenu: {
|
||||||
noMatch: 'Aucune donnée correspondante',
|
noMatch: 'Aucune donnée correspondante',
|
||||||
noData: 'Aucune donnée'
|
noData: 'Aucune donnée'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { defineLocale } from '../composables/defineLocale'
|
import { defineLocale } from '../composables/defineLocale'
|
||||||
|
|
||||||
export default defineLocale('Italiano', {
|
export default defineLocale('Italiano', 'it', {
|
||||||
inputMenu: {
|
inputMenu: {
|
||||||
noMatch: 'Nessun dato corrispondente',
|
noMatch: 'Nessun dato corrispondente',
|
||||||
noData: 'Nessun dato'
|
noData: 'Nessun dato'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { defineLocale } from '../composables/defineLocale'
|
import { defineLocale } from '../composables/defineLocale'
|
||||||
|
|
||||||
export default defineLocale('Русский', {
|
export default defineLocale('Русский', 'ru', {
|
||||||
inputMenu: {
|
inputMenu: {
|
||||||
noMatch: 'Совпадений не найдено',
|
noMatch: 'Совпадений не найдено',
|
||||||
noData: 'Нет данных'
|
noData: 'Нет данных'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { defineLocale } from '../composables/defineLocale'
|
import { defineLocale } from '../composables/defineLocale'
|
||||||
|
|
||||||
export default defineLocale('简体中文', {
|
export default defineLocale('简体中文', 'zh-Hans', {
|
||||||
inputMenu: {
|
inputMenu: {
|
||||||
noMatch: '没有匹配的数据',
|
noMatch: '没有匹配的数据',
|
||||||
noData: '没有数据'
|
noData: '没有数据'
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { defineLocale } from '../composables/defineLocale'
|
import { defineLocale } from '../composables/defineLocale'
|
||||||
|
|
||||||
export default defineLocale('繁体中文', {
|
export default defineLocale('繁体中文', 'zh-Hant', {
|
||||||
inputMenu: {
|
inputMenu: {
|
||||||
noMatch: '沒有匹配的資料',
|
noMatch: '沒有匹配的資料',
|
||||||
noData: '沒有資料'
|
noData: '沒有資料'
|
||||||
|
|||||||
@@ -36,5 +36,6 @@ export type LocalePair = {
|
|||||||
|
|
||||||
export type Locale = {
|
export type Locale = {
|
||||||
name: string
|
name: string
|
||||||
|
code: string
|
||||||
ui: LocalePair
|
ui: LocalePair
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ export type Translator = (path: string, option?: TranslatorOption) => string
|
|||||||
export type LocaleContext = {
|
export type LocaleContext = {
|
||||||
locale: Ref<Locale>
|
locale: Ref<Locale>
|
||||||
lang: Ref<string>
|
lang: Ref<string>
|
||||||
|
code: Ref<string>
|
||||||
t: Translator
|
t: Translator
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,10 +28,12 @@ export function translate(path: string, option: undefined | TranslatorOption, lo
|
|||||||
|
|
||||||
export function buildLocaleContext(locale: MaybeRef<Locale>): LocaleContext {
|
export function buildLocaleContext(locale: MaybeRef<Locale>): LocaleContext {
|
||||||
const lang = computed(() => unref(locale).name)
|
const lang = computed(() => unref(locale).name)
|
||||||
|
const code = computed(() => unref(locale).code)
|
||||||
const localeRef = isRef(locale) ? locale : ref(locale)
|
const localeRef = isRef(locale) ? locale : ref(locale)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
lang,
|
lang,
|
||||||
|
code,
|
||||||
locale: localeRef,
|
locale: localeRef,
|
||||||
t: buildTranslator(locale)
|
t: buildTranslator(locale)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user