Skip to content

Commit

Permalink
fix: unable to configure server integration using inline options (#3020)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Jul 26, 2024
1 parent e72a5de commit 856ba4f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
23 changes: 23 additions & 0 deletions specs/fixtures/inline_options/locale-detector.ts
Original file line number Diff line number Diff line change
@@ -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
})
5 changes: 4 additions & 1 deletion specs/fixtures/inline_options/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export default defineNuxtConfig({
file: 'locale-file-en.json',
name: 'English'
}
]
],
experimental: {
localeDetector: './locale-detector.ts'
}
}
]
],
Expand Down
7 changes: 7 additions & 0 deletions specs/fixtures/inline_options/server/api/translate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useTranslation } from '@intlify/h3'

export default defineEventHandler(async event => {
const t = await useTranslation(event)

return t('home')
})
7 changes: 6 additions & 1 deletion specs/inline_options.spec.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -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')
})
})
13 changes: 8 additions & 5 deletions src/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -131,13 +131,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
Expand Down

0 comments on commit 856ba4f

Please sign in to comment.