diff --git a/cli/commands/make/locale.mjs b/cli/commands/make/locale.mjs index 98581759..3d46d567 100644 --- a/cli/commands/make/locale.mjs +++ b/cli/commands/make/locale.mjs @@ -1,7 +1,7 @@ import { existsSync, promises as fsp } from 'node:fs' import { resolve } from 'pathe' import { consola } from 'consola' -import { appendFile, sortFile } from '../../utils.mjs' +import { appendFile, sortFile, normalizeLocale } from '../../utils.mjs' import { defineCommand } from 'citty' export default defineCommand({ @@ -45,7 +45,7 @@ export default defineCommand({ // Create new locale file await fsp.copyFile(originLocaleFilePath, newLocaleFilePath) 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) consola.success(`🪄 Generated ${newLocaleFilePath}`) diff --git a/cli/utils.mjs b/cli/utils.mjs index 3e3c1c80..788a4fea 100644 --- a/cli/utils.mjs +++ b/cli/utils.mjs @@ -15,3 +15,17 @@ export async function appendFile(path, contents) { 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() +} diff --git a/docs/content/1.getting-started/7.i18n/1.nuxt.md b/docs/content/1.getting-started/7.i18n/1.nuxt.md index 1d1d367f..aa797741 100644 --- a/docs/content/1.getting-started/7.i18n/1.nuxt.md +++ b/docs/content/1.getting-started/7.i18n/1.nuxt.md @@ -38,7 +38,7 @@ You also have the option to add your own locale using `defineLocale`: ```vue [app.vue] @@ -50,6 +50,12 @@ const locale = defineLocale('My custom locale', { ``` +::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 To dynamically switch between languages, you can use the [Nuxt I18n](https://i18n.nuxtjs.org/) module. diff --git a/docs/content/1.getting-started/7.i18n/2.vue.md b/docs/content/1.getting-started/7.i18n/2.vue.md index 5f37d0ba..a2d89d05 100644 --- a/docs/content/1.getting-started/7.i18n/2.vue.md +++ b/docs/content/1.getting-started/7.i18n/2.vue.md @@ -40,7 +40,7 @@ You also have the option to add your locale using `defineLocale`: @@ -52,6 +52,12 @@ const locale = defineLocale('My custom locale', { ``` +::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 To dynamically switch between languages, you can use the [Vue I18n](https://vue-i18n.intlify.dev/) plugin. diff --git a/src/runtime/composables/defineLocale.ts b/src/runtime/composables/defineLocale.ts index 49e1f78d..3ac438a1 100644 --- a/src/runtime/composables/defineLocale.ts +++ b/src/runtime/composables/defineLocale.ts @@ -1,8 +1,9 @@ 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 { name, + code, ui: pair } } diff --git a/src/runtime/locale/ar.ts b/src/runtime/locale/ar.ts index 8c17b575..8292cbfc 100644 --- a/src/runtime/locale/ar.ts +++ b/src/runtime/locale/ar.ts @@ -1,6 +1,6 @@ import { defineLocale } from '../composables/defineLocale' -export default defineLocale('العربية', { +export default defineLocale('العربية', 'ar', { inputMenu: { noMatch: 'لا توجد نتائج مطابقة', noData: 'لا توجد بيانات' diff --git a/src/runtime/locale/cs.ts b/src/runtime/locale/cs.ts index bf50ac7a..78790645 100644 --- a/src/runtime/locale/cs.ts +++ b/src/runtime/locale/cs.ts @@ -1,6 +1,6 @@ import { defineLocale } from '../composables/defineLocale' -export default defineLocale('Čeština', { +export default defineLocale('Čeština', 'cs', { inputMenu: { noMatch: 'Žádná shoda', noData: 'Žádná data' diff --git a/src/runtime/locale/de.ts b/src/runtime/locale/de.ts index d834d079..2af80817 100644 --- a/src/runtime/locale/de.ts +++ b/src/runtime/locale/de.ts @@ -1,6 +1,6 @@ import { defineLocale } from '../composables/defineLocale' -export default defineLocale('Deutsch', { +export default defineLocale('Deutsch', 'de', { inputMenu: { noMatch: 'Nichts gefunden', noData: 'Keine Daten' diff --git a/src/runtime/locale/en.ts b/src/runtime/locale/en.ts index 65160cc0..a71aafa0 100644 --- a/src/runtime/locale/en.ts +++ b/src/runtime/locale/en.ts @@ -1,6 +1,6 @@ import { defineLocale } from '../composables/defineLocale' -export default defineLocale('English', { +export default defineLocale('English', 'en', { inputMenu: { noMatch: 'No matching data', noData: 'No data' diff --git a/src/runtime/locale/fr.ts b/src/runtime/locale/fr.ts index eb8e2a84..bc2b578d 100644 --- a/src/runtime/locale/fr.ts +++ b/src/runtime/locale/fr.ts @@ -1,6 +1,6 @@ import { defineLocale } from '../composables/defineLocale' -export default defineLocale('Français', { +export default defineLocale('Français', 'fr', { inputMenu: { noMatch: 'Aucune donnée correspondante', noData: 'Aucune donnée' diff --git a/src/runtime/locale/it.ts b/src/runtime/locale/it.ts index e9299d9a..26c79929 100644 --- a/src/runtime/locale/it.ts +++ b/src/runtime/locale/it.ts @@ -1,6 +1,6 @@ import { defineLocale } from '../composables/defineLocale' -export default defineLocale('Italiano', { +export default defineLocale('Italiano', 'it', { inputMenu: { noMatch: 'Nessun dato corrispondente', noData: 'Nessun dato' diff --git a/src/runtime/locale/ru.ts b/src/runtime/locale/ru.ts index 4a3c145a..2f26910a 100644 --- a/src/runtime/locale/ru.ts +++ b/src/runtime/locale/ru.ts @@ -1,6 +1,6 @@ import { defineLocale } from '../composables/defineLocale' -export default defineLocale('Русский', { +export default defineLocale('Русский', 'ru', { inputMenu: { noMatch: 'Совпадений не найдено', noData: 'Нет данных' diff --git a/src/runtime/locale/zh_hans.ts b/src/runtime/locale/zh_hans.ts index 9da67b5e..799257f7 100644 --- a/src/runtime/locale/zh_hans.ts +++ b/src/runtime/locale/zh_hans.ts @@ -1,6 +1,6 @@ import { defineLocale } from '../composables/defineLocale' -export default defineLocale('简体中文', { +export default defineLocale('简体中文', 'zh-Hans', { inputMenu: { noMatch: '没有匹配的数据', noData: '没有数据' diff --git a/src/runtime/locale/zh_hant.ts b/src/runtime/locale/zh_hant.ts index d9fc299d..4e0baae7 100644 --- a/src/runtime/locale/zh_hant.ts +++ b/src/runtime/locale/zh_hant.ts @@ -1,6 +1,6 @@ import { defineLocale } from '../composables/defineLocale' -export default defineLocale('繁体中文', { +export default defineLocale('繁体中文', 'zh-Hant', { inputMenu: { noMatch: '沒有匹配的資料', noData: '沒有資料' diff --git a/src/runtime/types/locale.ts b/src/runtime/types/locale.ts index e1f72a12..9fa0aebb 100644 --- a/src/runtime/types/locale.ts +++ b/src/runtime/types/locale.ts @@ -36,5 +36,6 @@ export type LocalePair = { export type Locale = { name: string + code: string ui: LocalePair } diff --git a/src/runtime/utils/locale.ts b/src/runtime/utils/locale.ts index 67cdfe35..672ba9eb 100644 --- a/src/runtime/utils/locale.ts +++ b/src/runtime/utils/locale.ts @@ -9,6 +9,7 @@ export type Translator = (path: string, option?: TranslatorOption) => string export type LocaleContext = { locale: Ref lang: Ref + code: Ref t: Translator } @@ -27,10 +28,12 @@ export function translate(path: string, option: undefined | TranslatorOption, lo export function buildLocaleContext(locale: MaybeRef): LocaleContext { const lang = computed(() => unref(locale).name) + const code = computed(() => unref(locale).code) const localeRef = isRef(locale) ? locale : ref(locale) return { lang, + code, locale: localeRef, t: buildTranslator(locale) }