From a143060122551a2799c43a81be448a6fc2384105 Mon Sep 17 00:00:00 2001 From: userquin Date: Sun, 2 Jul 2023 14:07:42 +0200 Subject: [PATCH 01/11] feat: add i18n support --- .eslintignore | 1 - README.md | 1 + package.json | 9 +- playground/config/i18n.config.mts | 10 + playground/config/i18n.ts | 79 ++++ playground/locales/en-GB.json | 3 + playground/locales/en-US.json | 1 + playground/locales/en.json | 3 + playground/locales/es-419.json | 1 + playground/locales/es-ES.json | 1 + playground/locales/es.json | 3 + .../{nuxt.config.ts => nuxt.config.mts} | 17 +- playground/package.json | 3 + playground/pages/index.vue | 19 +- pnpm-lock.yaml | 336 ++++++++++++++++++ src/module.ts | 22 +- src/runtime/plugins/i18n.ts | 201 +++++++++++ src/runtime/plugins/vuetify-i18n.mts | 10 + .../plugin.mts => plugins/vuetify.mts} | 2 +- src/types.ts | 1 - 20 files changed, 705 insertions(+), 18 deletions(-) create mode 100644 playground/config/i18n.config.mts create mode 100644 playground/config/i18n.ts create mode 100644 playground/locales/en-GB.json create mode 100644 playground/locales/en-US.json create mode 100644 playground/locales/en.json create mode 100644 playground/locales/es-419.json create mode 100644 playground/locales/es-ES.json create mode 100644 playground/locales/es.json rename playground/{nuxt.config.ts => nuxt.config.mts} (73%) create mode 100644 src/runtime/plugins/i18n.ts create mode 100644 src/runtime/plugins/vuetify-i18n.mts rename src/runtime/{templates/plugin.mts => plugins/vuetify.mts} (92%) diff --git a/.eslintignore b/.eslintignore index 381cb01..de4d1f0 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,2 @@ dist node_modules -templates diff --git a/README.md b/README.md index 03d2596..773e2f7 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Zero-config Nuxt module for Vuetify - 🛠️ **Versatile**: custom Vuetify [directives](https://vuetifyjs.com/en/getting-started/installation/#manual-steps) and [labs components](https://vuetifyjs.com/en/labs/introduction/) registration - ✨ **Configurable styles**: configure your variables using [Vuetify SASS Variables](https://vuetifyjs.com/en/features/sass-variables/) - 💥 **SSR**: automatic SSR detection and configuration +- 🌍 **I18n ready**: install [@nuxtjs/i18n](https://v8.i18n.nuxtjs.org/) Nuxt module, and you're ready to use Vuetify [internationalization](https://vuetifyjs.com/en/features/internationalization/) features - 🦾 **Type Strong**: written in [TypeScript](https://www.typescriptlang.org/) ## 📦 Install diff --git a/package.json b/package.json index 6e5be20..66b34c3 100644 --- a/package.json +++ b/package.json @@ -36,16 +36,16 @@ "*.d.ts" ], "scripts": { - "prepack": "nuxt-module-build", + "build": "pnpm dev:prepare && nuxt-module-build", "dev": "nuxi dev playground", "dev:build": "nuxi build playground", "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground", - "prepublishOnly": "npm run prepack", - "release": "bumpp && npm publish", "lint": "eslint .", "lint:fix": "nr lint --fix", "test": "vitest run", - "test:watch": "vitest watch" + "test:watch": "vitest watch", + "prepublishOnly": "pnpm build", + "release": "bumpp && npm publish" }, "peerDependencies": { "@nuxt/kit": "^3.5.3", @@ -65,6 +65,7 @@ "@nuxt/module-builder": "^0.4.0", "@nuxt/schema": "^3.6.1", "@nuxt/test-utils": "^3.6.1", + "@nuxtjs/i18n": "^8.0.0-beta.12", "@parcel/watcher": "^2.1.0", "@types/node": "^18", "bumpp": "^9.1.1", diff --git a/playground/config/i18n.config.mts b/playground/config/i18n.config.mts new file mode 100644 index 0000000..589bdb7 --- /dev/null +++ b/playground/config/i18n.config.mts @@ -0,0 +1,10 @@ +import { availableLocales } from './i18n' + +export default defineI18nConfig(() => ({ + locale: 'en-US', + availableLocales: availableLocales.map(l => l.code), + fallbackLocale: 'en-US', + fallbackWarn: false, + missingWarn: false, + globalInjection: true, +})) diff --git a/playground/config/i18n.ts b/playground/config/i18n.ts new file mode 100644 index 0000000..f14e1e4 --- /dev/null +++ b/playground/config/i18n.ts @@ -0,0 +1,79 @@ +import type { DateTimeFormats, NumberFormats, PluralizationRule } from '@intlify/core-base' +import type { LocaleObject } from '#i18n' + +interface LocaleObjectData extends LocaleObject { + numberFormats?: NumberFormats + dateTimeFormats?: DateTimeFormats + pluralRule?: PluralizationRule +} + +const countryLocaleVariants: Record = { + en: [ + // en.json contains en-US translations + { code: 'en-US', name: 'English (US)' }, + { code: 'en-GB', name: 'English (UK)' }, + ], + es: [ + // es.json contains es-ES translations + // { code: 'es-AR', name: 'Español (Argentina)' }, + // { code: 'es-BO', name: 'Español (Bolivia)' }, + // { code: 'es-CL', name: 'Español (Chile)' }, + // { code: 'es-CO', name: 'Español (Colombia)' }, + // { code: 'es-CR', name: 'Español (Costa Rica)' }, + // { code: 'es-DO', name: 'Español (República Dominicana)' }, + // { code: 'es-EC', name: 'Español (Ecuador)' }, + { code: 'es-ES', name: 'Español (España)' }, + // TODO: Support es-419, if we include spanish country variants remove also fix on utils/language.ts module + { code: 'es-419', name: 'Español (Latinoamérica)' }, + // { code: 'es-GT', name: 'Español (Guatemala)' }, + // { code: 'es-HN', name: 'Español (Honduras)' }, + // { code: 'es-MX', name: 'Español (México)' }, + // { code: 'es-NI', name: 'Español (Nicaragua)' }, + // { code: 'es-PA', name: 'Español (Panamá)' }, + // { code: 'es-PE', name: 'Español (Perú)' }, + // { code: 'es-PR', name: 'Español (Puerto Rico)' }, + // { code: 'es-SV', name: 'Español (El Salvador)' }, + // { code: 'es-US', name: 'Español (Estados Unidos)' }, + // { code: 'es-UY', name: 'Español (Uruguay)' }, + // { code: 'es-VE', name: 'Español (Venezuela)' }, + ], +} + +const locales: LocaleObjectData[] = [ + { + code: 'en', + file: 'en.json', + name: 'English', + }, + { + code: 'es', + file: 'es.json', + name: 'Español', + }, +] + +function buildLocales() { + const useLocales = Object.values(locales).reduce((acc, data) => { + const locales = countryLocaleVariants[data.code] + if (locales) { + locales.forEach((l) => { + const entry: LocaleObjectData = { + ...data, + code: l.code, + name: l.name, + files: [data.file!, `${l.code}.json`], + } + delete entry.file + acc.push(entry) + }) + } + else { + acc.push(data) + } + return acc + }, []) + + return useLocales.sort((a, b) => a.code.localeCompare(b.code)) +} + +export const availableLocales = buildLocales() diff --git a/playground/locales/en-GB.json b/playground/locales/en-GB.json new file mode 100644 index 0000000..686e85e --- /dev/null +++ b/playground/locales/en-GB.json @@ -0,0 +1,3 @@ +{ + "favourite": "Favourite" +} diff --git a/playground/locales/en-US.json b/playground/locales/en-US.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/playground/locales/en-US.json @@ -0,0 +1 @@ +{} diff --git a/playground/locales/en.json b/playground/locales/en.json new file mode 100644 index 0000000..fac114c --- /dev/null +++ b/playground/locales/en.json @@ -0,0 +1,3 @@ +{ + "favourite": "Favorite" +} diff --git a/playground/locales/es-419.json b/playground/locales/es-419.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/playground/locales/es-419.json @@ -0,0 +1 @@ +{} diff --git a/playground/locales/es-ES.json b/playground/locales/es-ES.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/playground/locales/es-ES.json @@ -0,0 +1 @@ +{} diff --git a/playground/locales/es.json b/playground/locales/es.json new file mode 100644 index 0000000..768c48c --- /dev/null +++ b/playground/locales/es.json @@ -0,0 +1,3 @@ +{ + "favourite": "Favorito" +} diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.mts similarity index 73% rename from playground/nuxt.config.ts rename to playground/nuxt.config.mts index 27c773e..be79c30 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.mts @@ -1,4 +1,5 @@ import { md3 } from 'vuetify/blueprints' +import { availableLocales } from './config/i18n' export default defineNuxtConfig({ ssr: true, @@ -10,7 +11,21 @@ export default defineNuxtConfig({ }, }, }, - modules: ['../src/module'], + imports: { + autoImport: true, + injectAtEnd: true, + }, + modules: ['@nuxtjs/i18n', '../src/module'], + i18n: { + locales: availableLocales, + lazy: true, + strategy: 'no_prefix', + detectBrowserLanguage: false, + langDir: 'locales', + defaultLocale: 'en-US', + // debug: true, + vueI18n: './config/i18n.config.mts', + }, vuetify: { moduleOptions: { styles: { configFile: '/settings.scss' }, diff --git a/playground/package.json b/playground/package.json index e3879d8..3ca8a83 100644 --- a/playground/package.json +++ b/playground/package.json @@ -7,6 +7,9 @@ "build": "nuxi build", "generate": "nuxi generate" }, + "dependencies": { + "@nuxtjs/i18n": "^8.0.0-beta.12" + }, "devDependencies": { "nuxt": "latest", "sass": "^1.63.6" diff --git a/playground/pages/index.vue b/playground/pages/index.vue index 54d42b9..6b06970 100644 --- a/playground/pages/index.vue +++ b/playground/pages/index.vue @@ -1,4 +1,6 @@ From a707992657b1c8bcdeb40de036a74ea2a18f7afd Mon Sep 17 00:00:00 2001 From: userquin Date: Sun, 2 Jul 2023 14:20:19 +0200 Subject: [PATCH 03/11] chore: remove async --- src/runtime/plugins/vuetify-i18n.mts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/runtime/plugins/vuetify-i18n.mts b/src/runtime/plugins/vuetify-i18n.mts index 7ab54b5..90dc615 100644 --- a/src/runtime/plugins/vuetify-i18n.mts +++ b/src/runtime/plugins/vuetify-i18n.mts @@ -2,9 +2,8 @@ import { createAdapter } from './i18n' import { defineNuxtPlugin } from '#app' export default defineNuxtPlugin((nuxtApp) => { - nuxtApp.hook('vuetify:configuration', async (_isDev, vuetifyOptions) => { + nuxtApp.hook('vuetify:configuration', (_isDev, vuetifyOptions) => { vuetifyOptions.locale = vuetifyOptions.locale ?? {} - vuetifyOptions.locale.adapter = await createAdapter(nuxtApp) - // vuetifyOptions.locale.adapter = await import('./i18n').then(({ createAdapter }) => createAdapter(nuxtApp)) + vuetifyOptions.locale.adapter = createAdapter(nuxtApp) }) }) From b13d9ed14974e36f02bef9f05cf634cb86aee581 Mon Sep 17 00:00:00 2001 From: userquin Date: Sun, 2 Jul 2023 15:53:45 +0200 Subject: [PATCH 04/11] chore: refactor i18n conf and plugin --- playground/config/i18n.config.mts | 17 +++++++++-------- playground/config/i18n.ts | 15 ++++----------- playground/pages/index.vue | 8 +++++--- src/runtime/plugins/i18n.ts | 7 +++---- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/playground/config/i18n.config.mts b/playground/config/i18n.config.mts index 589bdb7..557fb1a 100644 --- a/playground/config/i18n.config.mts +++ b/playground/config/i18n.config.mts @@ -1,10 +1,11 @@ import { availableLocales } from './i18n' -export default defineI18nConfig(() => ({ - locale: 'en-US', - availableLocales: availableLocales.map(l => l.code), - fallbackLocale: 'en-US', - fallbackWarn: false, - missingWarn: false, - globalInjection: true, -})) +export default defineI18nConfig(() => { + return { + availableLocales: availableLocales.map(l => l.code), + fallbackLocale: 'en-US', + fallbackWarn: false, + // eslint-disable-next-line @typescript-eslint/comma-dangle + missingWarn: false + } +}) diff --git a/playground/config/i18n.ts b/playground/config/i18n.ts index f14e1e4..562c9c5 100644 --- a/playground/config/i18n.ts +++ b/playground/config/i18n.ts @@ -1,13 +1,6 @@ -import type { DateTimeFormats, NumberFormats, PluralizationRule } from '@intlify/core-base' import type { LocaleObject } from '#i18n' -interface LocaleObjectData extends LocaleObject { - numberFormats?: NumberFormats - dateTimeFormats?: DateTimeFormats - pluralRule?: PluralizationRule -} - -const countryLocaleVariants: Record = { +const countryLocaleVariants: Record = { en: [ // en.json contains en-US translations { code: 'en-US', name: 'English (US)' }, @@ -39,7 +32,7 @@ const countryLocaleVariants: Record = { ], } -const locales: LocaleObjectData[] = [ +const locales: LocaleObject[] = [ { code: 'en', file: 'en.json', @@ -57,7 +50,7 @@ function buildLocales() { const locales = countryLocaleVariants[data.code] if (locales) { locales.forEach((l) => { - const entry: LocaleObjectData = { + const entry: LocaleObject = { ...data, code: l.code, name: l.name, @@ -71,7 +64,7 @@ function buildLocales() { acc.push(data) } return acc - }, []) + }, []) return useLocales.sort((a, b) => a.code.localeCompare(b.code)) } diff --git a/playground/pages/index.vue b/playground/pages/index.vue index be63a96..aaa53aa 100644 --- a/playground/pages/index.vue +++ b/playground/pages/index.vue @@ -10,7 +10,7 @@ const value = reactive<{ name2: undefined, name3: undefined, }) -const { locales, t } = useI18n({ useScope: 'global' }) +const { locales, t } = useI18n() const { current } = useLocale() @@ -18,10 +18,12 @@ const { current } = useLocale()
Vuetify current: {{ current }}
I18N current: {{ $i18n.locale }}
+
$vuetify current: {{ $vuetify.locale.current }}
t without locale: {{ t('favourite') }}
-
t with I18N locale: {{ t('favourite', $i18n.locale) }}
-
t with Vuetify current locale: {{ t('favourite', current) }}
+
t with I18N locale: {{ t('favourite', { locale: $i18n.locale }) }}
+
t with Vuetify current locale: {{ t('favourite', { locale: current }) }}
$t {{ $t('favourite') }}
+
$vuetify.locale.t {{ $vuetify.locale.t('favourite') }}
current, (l) => { - i18n.setLocale(l).then(() => (nuxtApp.$vuetify.locale.current.value = l)) - }) + watch(() => current, l => (nuxtApp.$vuetify.locale.current.value = l)) watch(() => nuxtApp.$vuetify?.locale?.current.value, l => i18n.setLocale(l)) @@ -184,7 +182,7 @@ function createProvideFunction(data: { const { current: vLocale } = useLocale() - watch(current, l => i18n.setLocale(l).then(() => vLocale.value = l)) + watch(current, l => vLocale.value = l) watch(vLocale, l => i18n.setLocale(l)) @@ -193,6 +191,7 @@ function createProvideFunction(data: { current, fallback, messages, + // @ts-expect-error Type instantiation is excessively deep and possibly infinite.ts(2589) t: (key, ...params) => i18n.t(key, params), n: i18n.n, provide: createProvideFunction({ current, fallback, messages }), From c474c81734a570a40c0bdca23ebfc7a1b40ba8f1 Mon Sep 17 00:00:00 2001 From: userquin Date: Sun, 2 Jul 2023 16:27:39 +0200 Subject: [PATCH 05/11] chore: remove `vueI18n` from nuxt i18n entry --- playground/config/i18n.config.mts | 11 ----------- playground/config/i18n.ts | 15 ++++++++++++++- playground/locales/en-GB.json | 2 +- playground/locales/en.json | 2 +- playground/locales/es.json | 2 +- playground/nuxt.config.mts | 13 ++----------- playground/pages/index.vue | 20 ++++++++++---------- src/types.ts | 2 +- 8 files changed, 30 insertions(+), 37 deletions(-) delete mode 100644 playground/config/i18n.config.mts diff --git a/playground/config/i18n.config.mts b/playground/config/i18n.config.mts deleted file mode 100644 index 557fb1a..0000000 --- a/playground/config/i18n.config.mts +++ /dev/null @@ -1,11 +0,0 @@ -import { availableLocales } from './i18n' - -export default defineI18nConfig(() => { - return { - availableLocales: availableLocales.map(l => l.code), - fallbackLocale: 'en-US', - fallbackWarn: false, - // eslint-disable-next-line @typescript-eslint/comma-dangle - missingWarn: false - } -}) diff --git a/playground/config/i18n.ts b/playground/config/i18n.ts index 562c9c5..81029c4 100644 --- a/playground/config/i18n.ts +++ b/playground/config/i18n.ts @@ -1,3 +1,4 @@ +import type { NuxtI18nOptions } from '@nuxtjs/i18n/dist/module' import type { LocaleObject } from '#i18n' const countryLocaleVariants: Record = { @@ -69,4 +70,16 @@ function buildLocales() { return useLocales.sort((a, b) => a.code.localeCompare(b.code)) } -export const availableLocales = buildLocales() +const availableLocales = buildLocales() + +export const i18n: NuxtI18nOptions = { + locales: availableLocales, + lazy: true, + strategy: 'no_prefix', + detectBrowserLanguage: false, + langDir: 'locales', + defaultLocale: 'en-US', + customRoutes: undefined, + dynamicRouteParams: false, + // debug: true, +} diff --git a/playground/locales/en-GB.json b/playground/locales/en-GB.json index 686e85e..b4abdb1 100644 --- a/playground/locales/en-GB.json +++ b/playground/locales/en-GB.json @@ -1,3 +1,3 @@ { - "favourite": "Favourite" + "xxx": "Favourite" } diff --git a/playground/locales/en.json b/playground/locales/en.json index fac114c..a082249 100644 --- a/playground/locales/en.json +++ b/playground/locales/en.json @@ -1,3 +1,3 @@ { - "favourite": "Favorite" + "xxx": "Favorite" } diff --git a/playground/locales/es.json b/playground/locales/es.json index 768c48c..ef271b0 100644 --- a/playground/locales/es.json +++ b/playground/locales/es.json @@ -1,3 +1,3 @@ { - "favourite": "Favorito" + "xxx": "Favorito" } diff --git a/playground/nuxt.config.mts b/playground/nuxt.config.mts index be79c30..67fb66a 100644 --- a/playground/nuxt.config.mts +++ b/playground/nuxt.config.mts @@ -1,5 +1,5 @@ import { md3 } from 'vuetify/blueprints' -import { availableLocales } from './config/i18n' +import { i18n } from './config/i18n' export default defineNuxtConfig({ ssr: true, @@ -16,16 +16,7 @@ export default defineNuxtConfig({ injectAtEnd: true, }, modules: ['@nuxtjs/i18n', '../src/module'], - i18n: { - locales: availableLocales, - lazy: true, - strategy: 'no_prefix', - detectBrowserLanguage: false, - langDir: 'locales', - defaultLocale: 'en-US', - // debug: true, - vueI18n: './config/i18n.config.mts', - }, + i18n, vuetify: { moduleOptions: { styles: { configFile: '/settings.scss' }, diff --git a/playground/pages/index.vue b/playground/pages/index.vue index aaa53aa..d7b3a60 100644 --- a/playground/pages/index.vue +++ b/playground/pages/index.vue @@ -16,14 +16,14 @@ const { current } = useLocale() diff --git a/src/types.ts b/src/types.ts index 02153a5..801ac88 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,7 +2,7 @@ import type { VuetifyOptions } from 'vuetify' export type BooleanOrArrayString = boolean | string[] -export interface VOptions extends Partial> { +export interface VOptions extends Partial> { /** * Include the lab components? * From 3bd601838057724baf790bd9d874f19a31c27519 Mon Sep 17 00:00:00 2001 From: userquin Date: Sun, 2 Jul 2023 18:42:40 +0200 Subject: [PATCH 06/11] chore: include single and multiple locale files --- .npmrc | 3 +- package.json | 2 + playground/config/i18n.config.mts | 12 ++++++ playground/config/i18n.ts | 42 ++++++++++--------- playground/locales/en-US.json | 1 - playground/locales/{ => multiple}/en-GB.json | 0 .../locales/{en.json => multiple/en-US.json} | 0 playground/locales/multiple/en.json | 3 ++ playground/locales/{ => multiple}/es-419.json | 0 playground/locales/{ => multiple}/es-ES.json | 0 playground/locales/{ => multiple}/es.json | 0 playground/locales/single/en-GB.json | 3 ++ playground/locales/single/en-US.json | 3 ++ playground/locales/single/es-419.json | 3 ++ playground/locales/single/es-ES.json | 3 ++ playground/nuxt.config.mts | 15 ++++++- playground/pages/index.vue | 5 ++- 17 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 playground/config/i18n.config.mts delete mode 100644 playground/locales/en-US.json rename playground/locales/{ => multiple}/en-GB.json (100%) rename playground/locales/{en.json => multiple/en-US.json} (100%) create mode 100644 playground/locales/multiple/en.json rename playground/locales/{ => multiple}/es-419.json (100%) rename playground/locales/{ => multiple}/es-ES.json (100%) rename playground/locales/{ => multiple}/es.json (100%) create mode 100644 playground/locales/single/en-GB.json create mode 100644 playground/locales/single/en-US.json create mode 100644 playground/locales/single/es-419.json create mode 100644 playground/locales/single/es-ES.json diff --git a/.npmrc b/.npmrc index c483022..8fb0b90 100644 --- a/.npmrc +++ b/.npmrc @@ -1 +1,2 @@ -shamefully-hoist=true \ No newline at end of file +shamefully-hoist=true +shell-emulator=true diff --git a/package.json b/package.json index 66b34c3..2dd8fd3 100644 --- a/package.json +++ b/package.json @@ -37,8 +37,10 @@ ], "scripts": { "build": "pnpm dev:prepare && nuxt-module-build", + "dev:multiple-json": "MULTIPLE_LANG_FILES=true nuxi dev playground", "dev": "nuxi dev playground", "dev:build": "nuxi build playground", + "dev:prepare:multiple-json": "nuxt-module-build --stub && MULTIPLE_LANG_FILES=true nuxi prepare playground", "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground", "lint": "eslint .", "lint:fix": "nr lint --fix", diff --git a/playground/config/i18n.config.mts b/playground/config/i18n.config.mts new file mode 100644 index 0000000..f5e8968 --- /dev/null +++ b/playground/config/i18n.config.mts @@ -0,0 +1,12 @@ +import { availableLocales } from './i18n' + +export default defineI18nConfig(() => { + return { + legacy: false, + availableLocales: availableLocales.map(l => l.code), + fallbackLocale: 'en-US', + fallbackWarn: true, + // eslint-disable-next-line @typescript-eslint/comma-dangle + missingWarn: true + } +}) diff --git a/playground/config/i18n.ts b/playground/config/i18n.ts index 81029c4..770cfa6 100644 --- a/playground/config/i18n.ts +++ b/playground/config/i18n.ts @@ -1,6 +1,7 @@ -import type { NuxtI18nOptions } from '@nuxtjs/i18n/dist/module' import type { LocaleObject } from '#i18n' +const multipleJson = process.env.MULTIPLE_LANG_FILES === 'true' + const countryLocaleVariants: Record = { en: [ // en.json contains en-US translations @@ -51,35 +52,38 @@ function buildLocales() { const locales = countryLocaleVariants[data.code] if (locales) { locales.forEach((l) => { - const entry: LocaleObject = { - ...data, - code: l.code, - name: l.name, - files: [data.file!, `${l.code}.json`], + let entry: LocaleObject + if (multipleJson) { + entry = { + ...data, + code: l.code, + name: l.name, + files: [data.file!, `${l.code}.json`], + } + delete entry.file + } + else { + entry = { + ...data, + code: l.code, + name: l.name, + file: `${l.code}.json`, + } } - delete entry.file + acc.push(entry) }) } else { acc.push(data) } + return acc }, []) return useLocales.sort((a, b) => a.code.localeCompare(b.code)) } -const availableLocales = buildLocales() +export const availableLocales = buildLocales() -export const i18n: NuxtI18nOptions = { - locales: availableLocales, - lazy: true, - strategy: 'no_prefix', - detectBrowserLanguage: false, - langDir: 'locales', - defaultLocale: 'en-US', - customRoutes: undefined, - dynamicRouteParams: false, - // debug: true, -} +export const langDir = multipleJson ? 'locales/multiple' : 'locales/single' diff --git a/playground/locales/en-US.json b/playground/locales/en-US.json deleted file mode 100644 index 0967ef4..0000000 --- a/playground/locales/en-US.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/playground/locales/en-GB.json b/playground/locales/multiple/en-GB.json similarity index 100% rename from playground/locales/en-GB.json rename to playground/locales/multiple/en-GB.json diff --git a/playground/locales/en.json b/playground/locales/multiple/en-US.json similarity index 100% rename from playground/locales/en.json rename to playground/locales/multiple/en-US.json diff --git a/playground/locales/multiple/en.json b/playground/locales/multiple/en.json new file mode 100644 index 0000000..a082249 --- /dev/null +++ b/playground/locales/multiple/en.json @@ -0,0 +1,3 @@ +{ + "xxx": "Favorite" +} diff --git a/playground/locales/es-419.json b/playground/locales/multiple/es-419.json similarity index 100% rename from playground/locales/es-419.json rename to playground/locales/multiple/es-419.json diff --git a/playground/locales/es-ES.json b/playground/locales/multiple/es-ES.json similarity index 100% rename from playground/locales/es-ES.json rename to playground/locales/multiple/es-ES.json diff --git a/playground/locales/es.json b/playground/locales/multiple/es.json similarity index 100% rename from playground/locales/es.json rename to playground/locales/multiple/es.json diff --git a/playground/locales/single/en-GB.json b/playground/locales/single/en-GB.json new file mode 100644 index 0000000..b4abdb1 --- /dev/null +++ b/playground/locales/single/en-GB.json @@ -0,0 +1,3 @@ +{ + "xxx": "Favourite" +} diff --git a/playground/locales/single/en-US.json b/playground/locales/single/en-US.json new file mode 100644 index 0000000..a082249 --- /dev/null +++ b/playground/locales/single/en-US.json @@ -0,0 +1,3 @@ +{ + "xxx": "Favorite" +} diff --git a/playground/locales/single/es-419.json b/playground/locales/single/es-419.json new file mode 100644 index 0000000..86983c5 --- /dev/null +++ b/playground/locales/single/es-419.json @@ -0,0 +1,3 @@ +{ + "xxx": "Favorito (es-ES)" +} diff --git a/playground/locales/single/es-ES.json b/playground/locales/single/es-ES.json new file mode 100644 index 0000000..c238c39 --- /dev/null +++ b/playground/locales/single/es-ES.json @@ -0,0 +1,3 @@ +{ + "xxx": "Favorito (es-419)" +} diff --git a/playground/nuxt.config.mts b/playground/nuxt.config.mts index 67fb66a..c67e2ec 100644 --- a/playground/nuxt.config.mts +++ b/playground/nuxt.config.mts @@ -1,5 +1,5 @@ import { md3 } from 'vuetify/blueprints' -import { i18n } from './config/i18n' +import { availableLocales, langDir } from './config/i18n' export default defineNuxtConfig({ ssr: true, @@ -16,7 +16,18 @@ export default defineNuxtConfig({ injectAtEnd: true, }, modules: ['@nuxtjs/i18n', '../src/module'], - i18n, + i18n: { + locales: availableLocales, + lazy: true, + strategy: 'no_prefix', + detectBrowserLanguage: false, + langDir, + defaultLocale: 'en-US', + customRoutes: undefined, + dynamicRouteParams: false, + // debug: true, + vueI18n: './config/i18n.config.mts', + }, vuetify: { moduleOptions: { styles: { configFile: '/settings.scss' }, diff --git a/playground/pages/index.vue b/playground/pages/index.vue index d7b3a60..fdb3248 100644 --- a/playground/pages/index.vue +++ b/playground/pages/index.vue @@ -10,8 +10,11 @@ const value = reactive<{ name2: undefined, name3: undefined, }) -const { locales, t } = useI18n() +const { locales, t, availableLocales } = useI18n() const { current } = useLocale() +watch(current, () => { + console.log('current', t('xxx', { locale: current.value })) +})