Skip to content

Commit

Permalink
feat: allow generating types based on defaultLocale
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Oct 6, 2024
1 parent bc52515 commit ca91f96
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
39 changes: 31 additions & 8 deletions docs/content/docs/5.v9/3.options/10.misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,47 @@ description: Miscellaneous options.

## `experimental`

- type: `object`
- default: `{ localeDetector: '', switchLocalePathLinkSSR: false, autoImportTranslationFunctions: false }`
Experimental configuration property is an object with the following properties:

Supported properties:
### `experimental.localeDetector`
- type: `string`
- default: `''`
- Specify the locale detector to be called per request on the server side. You need to specify the filepath where the locale detector is defined.

- `localeDetector` (default: `''`) - Specify the locale detector to be called per request on the server side. You need to specify the filepath where the locale detector is defined.
::callout{icon="i-heroicons-exclamation-triangle" color="amber"}
About how to define the locale detector, see the [`defineI18nLocaleDetector` API](/docs/api#definei18nlocaledetector)
::
- `switchLocalePathLinkSSR` (default: `false`) - Changes the way dynamic route parameters are tracked and updated internally, improving language switcher SSR when using the [`SwitchLocalePathLink`](/docs/api/components#switchlocalepathlink) component.
- `autoImportTranslationFunctions` (default: `false`) - Automatically imports/initializes `$t`, `$rt`, `$d`, `$n`, `$tm` and `$te` functions in `<script setup>` when used.

### `experimental.switchLocalePathLinkSSR`
- type: `boolean`
- default: `false`
- Changes the way dynamic route parameters are tracked and updated internally, improving language switcher SSR when using the [`SwitchLocalePathLink`](/docs/api/components#switchlocalepathlink) component.

### `experimental.autoImportTranslationFunctions`
- type: `boolean`
- default: `false`
- Automatically imports/initializes `$t`, `$rt`, `$d`, `$n`, `$tm` and `$te` functions in `<script setup>` when used.

::callout{icon="i-heroicons-exclamation-triangle" color="amber"}
This feature relies on [Nuxt's Auto-imports](https://nuxt.com/docs/guide/concepts/auto-imports) and will not work if this has been disabled.
- `typedPages` (default: `true`) - Generates route types used in composables and configuration, this feature is enabled by default when Nuxt's `experimental.typedRoutes` is enabled.
::

### `experimental.typedPages`
- type: `boolean`
- default: `true`
- Generates route types used in composables and configuration, this feature is enabled by default when Nuxt's `experimental.typedRoutes` is enabled.

::callout{icon="i-heroicons-exclamation-triangle" color="amber"}
This feature relies on [Nuxt's `experimental.typedRoutes`](https://nuxt.com/docs/guide/going-further/experimental-features#typedpages) and will not work if this is not enabled.
::
- `typedOptionsAndMessages` (default: `false`) - Generate `vue-i18n` and message types used in translation functions and `vue-i18n` configuration.

### `experimental.typedOptionsAndMessages`
- type: `false | 'default' | 'all'`
- `false` - disables type generation
- `'default'` - generate types based on configured `defaultLocale`
- `'all'` - generate types based on all configured locales
- default: `false`
- Generate `vue-i18n` and message types used in translation functions and `vue-i18n` configuration. Can be configured to use the `defaultLocale` (better performance) or all locales for type generation.


## `customBlocks`
Expand Down
16 changes: 13 additions & 3 deletions src/runtime/server/api/merged-options.get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { deepCopy } from '@intlify/shared'
// @ts-ignore
import { defineEventHandler } from '#imports'
import { vueI18nConfigs, localeLoaders } from '#internal/i18n/options.mjs'
import { vueI18nConfigs, localeLoaders, nuxtI18nOptions, normalizedLocales } from '#internal/i18n/options.mjs'

import type { Locale, LocaleMessages } from 'vue-i18n'
import { loadLocale, loadVueI18nOptions } from '../../messages'
Expand All @@ -13,8 +13,16 @@ export default defineEventHandler(async () => {
const datetimeFormats = {}
const numberFormats = {}

const targetLocales: string[] = []
if (nuxtI18nOptions.experimental.typedOptionsAndMessages === 'default' && nuxtI18nOptions.defaultLocale != null) {
targetLocales.push(nuxtI18nOptions.defaultLocale)
} else if (nuxtI18nOptions.experimental.typedOptionsAndMessages === 'all') {
targetLocales.push(...normalizedLocales.map(x => x.code))
}

const vueI18nConfig = await loadVueI18nOptions(vueI18nConfigs, nuxtMock)
for (const locale in vueI18nConfig.messages) {
if (!targetLocales.includes(locale)) continue
deepCopy(vueI18nConfig.messages[locale] || {}, messages)
deepCopy(vueI18nConfig.numberFormats?.[locale] || {}, numberFormats)
deepCopy(vueI18nConfig.datetimeFormats?.[locale] || {}, datetimeFormats)
Expand All @@ -25,12 +33,14 @@ export default defineEventHandler(async () => {
// @ts-ignore
globalThis.defineI18nLocale = val => val

for (const code in localeLoaders) {
for (const locale in localeLoaders) {
if (!targetLocales.includes(locale)) continue

const setter = (_: Locale, message: LocaleMessages<DefineLocaleMessage, Locale>) => {
deepCopy(message, messages)
}

await loadLocale(code, localeLoaders, setter)
await loadLocale(locale, localeLoaders, setter)
}

// @ts-ignore
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ export interface ExperimentalFeatures {
* Generates types for vue-i18n and messages
*
* @defaultValue `false`
*
* @remark `'default'` to generate types based on `defaultLocale`
* @remark `'all'` to generate types based on all locales
*/
typedOptionsAndMessages?: boolean
typedOptionsAndMessages?: false | 'default' | 'all'
}

export interface BundleOptions
Expand Down

0 comments on commit ca91f96

Please sign in to comment.