diff --git a/specs/fixtures/inline_options/locale-detector.ts b/specs/fixtures/inline_options/locale-detector.ts new file mode 100644 index 000000000..4cce000e8 --- /dev/null +++ b/specs/fixtures/inline_options/locale-detector.ts @@ -0,0 +1,23 @@ +// Detect based on query, cookie, header +export default defineI18nLocaleDetector((event, config) => { + // try to get locale from query + const query = tryQueryLocale(event, { lang: '' }) // disable locale default value with `lang` option + if (query) { + return query.toString() + } + + // try to get locale from cookie + const cookie = tryCookieLocale(event, { lang: '', name: 'i18n_locale' }) // disable locale default value with `lang` option + if (cookie) { + return cookie.toString() + } + + // try to get locale from header (`accept-header`) + const header = tryHeaderLocale(event, { lang: '' }) // disable locale default value with `lang` option + if (header) { + return header.toString() + } + + // If the locale cannot be resolved up to this point, it is resolved with the value `defaultLocale` of the locale config passed to the function + return config.defaultLocale +}) diff --git a/specs/fixtures/inline_options/nuxt.config.ts b/specs/fixtures/inline_options/nuxt.config.ts index 9712bcec9..5b955a318 100644 --- a/specs/fixtures/inline_options/nuxt.config.ts +++ b/specs/fixtures/inline_options/nuxt.config.ts @@ -19,7 +19,10 @@ export default defineNuxtConfig({ file: 'locale-file-en.json', name: 'English' } - ] + ], + experimental: { + localeDetector: './locale-detector.ts' + } } ] ], diff --git a/specs/fixtures/inline_options/server/api/translate.ts b/specs/fixtures/inline_options/server/api/translate.ts new file mode 100644 index 000000000..1eae6c186 --- /dev/null +++ b/specs/fixtures/inline_options/server/api/translate.ts @@ -0,0 +1,7 @@ +import { useTranslation } from '@intlify/h3' + +export default defineEventHandler(async event => { + const t = await useTranslation(event) + + return t('home') +}) diff --git a/specs/inline_options.spec.ts b/specs/inline_options.spec.ts index 4c7717d69..2cbb66080 100644 --- a/specs/inline_options.spec.ts +++ b/specs/inline_options.spec.ts @@ -1,6 +1,6 @@ import { test, expect, describe } from 'vitest' import { fileURLToPath } from 'node:url' -import { setup } from './utils' +import { setup, url, $fetch } from './utils' import { renderPage } from './helper' await setup({ @@ -34,4 +34,9 @@ describe('inline options are handled correctly', async () => { ) ).toBe(false) }) + + test('(#2721) server integration from inline configuration', async () => { + const res = await $fetch(url('/api/translate')) + expect(res).toEqual('Homepage') + }) }) diff --git a/src/nitro.ts b/src/nitro.ts index 026a25395..cf58ce105 100644 --- a/src/nitro.ts +++ b/src/nitro.ts @@ -7,7 +7,7 @@ import { addServerPlugin, createResolver, resolvePath, useLogger } from '@nuxt/k import yamlPlugin from '@rollup/plugin-yaml' import json5Plugin from '@miyaneee/rollup-plugin-json5' import { getFeatureFlags } from './bundler' -import { isExists } from './utils' +import { getLayerI18n, isExists } from './utils' import { H3_PKG, UTILS_H3_PKG, @@ -130,13 +130,16 @@ export { localeDetector } } async function resolveLocaleDetectorPath(nuxt: Nuxt) { - const serverI18nLayer = nuxt.options._layers.find( - l => l.config.i18n?.experimental?.localeDetector != null && l.config.i18n?.experimental?.localeDetector !== '' - ) + const serverI18nLayer = nuxt.options._layers.find(l => { + const layerI18n = getLayerI18n(l) + return layerI18n?.experimental?.localeDetector != null && layerI18n?.experimental?.localeDetector !== '' + }) + let enableServerIntegration = serverI18nLayer != null if (serverI18nLayer != null) { - const pathTo = resolve(serverI18nLayer.config.rootDir, serverI18nLayer.config.i18n!.experimental!.localeDetector!) + const serverI18nLayerConfig = getLayerI18n(serverI18nLayer) + const pathTo = resolve(serverI18nLayer.config.rootDir, serverI18nLayerConfig!.experimental!.localeDetector!) const localeDetectorPath = await resolvePath(pathTo, { cwd: nuxt.options.rootDir, extensions: EXECUTABLE_EXTENSIONS