Skip to content
fluenti

@fluenti/nuxt

Configure in nuxt.config.ts under the fluenti key:

export default defineNuxtConfig({
modules: ['@fluenti/nuxt'],
fluenti: {
locales: ['en', 'ja', 'zh'],
defaultLocale: 'en',
strategy: 'prefix_except_default',
},
})
PropertyTypeDefaultDescription
localesstring[]requiredSupported locale codes
defaultLocalestringrequiredDefault locale code
strategyStrategy'prefix_except_default'URL routing strategy
sourceLocalestring?Source locale for extraction
catalogDirstring?Directory for compiled catalogs
detectBrowserLanguageDetectBrowserLanguageOptions?Browser language detection
detectOrderArray<string>['path', 'cookie', 'header']Ordered list of locale detectors
componentPrefixstring?''Prefix for global components ('I18n'I18nNuxtLinkLocale)
autoVitePluginboolean?trueAuto-register @fluenti/vite-plugin in Vite config
isrISROptions?ISR route rules configuration
compatboolean?Enable @fluenti/vue-i18n-compat bridge mode
type Strategy = 'prefix' | 'prefix_except_default' | 'prefix_and_default' | 'no_prefix'
StrategyDefault localeOther locales
prefix_except_default/about/ja/about
prefix/en/about/ja/about
prefix_and_default/about + /en/about/ja/about
no_prefix/about/about

All composables are auto-imported — no import needed. This includes useI18n from @fluenti/vue:

<script setup>
// All auto-imported, zero imports needed
const { t, locale, setLocale, d, n } = useI18n()
const localePath = useLocalePath()
const switchLocalePath = useSwitchLocalePath()
const head = useLocaleHead({ addSeoAttributes: true })
</script>

Returns a function that generates locale-prefixed paths.

<script setup>
const localePath = useLocalePath()
</script>
<template>
<NuxtLink :to="localePath('/about')">About</NuxtLink>
<NuxtLink :to="localePath('/about', 'ja')">About (JA)</NuxtLink>
</template>

Signature:

function useLocalePath(): (path: string, locale?: string) => string

Zero-argument — reads locale from @fluenti/vue and config from Nuxt runtimeConfig automatically. When locale is omitted, the current locale is used.

Returns a function that gets the current page path in a different locale.

<script setup>
const switchLocalePath = useSwitchLocalePath()
</script>
<template>
<!-- If current path is /ja/about -->
<NuxtLink :to="switchLocalePath('en')">EN</NuxtLink> <!-- → /about -->
<NuxtLink :to="switchLocalePath('zh')">ZH</NuxtLink> <!-- → /zh/about -->
</template>

Signature:

function useSwitchLocalePath(): (locale: string) => string

Returns a computed ref with SEO-friendly <head> metadata.

<script setup>
const head = useLocaleHead({
addSeoAttributes: true,
baseUrl: 'https://example.com',
})
useHead(head.value)
</script>

Signature:

function useLocaleHead(options?: LocaleHeadOptions): ComputedRef<LocaleHeadMeta>

Options:

PropertyTypeDefaultDescription
addSeoAttributesboolean?falseGenerate hreflang links and og:locale meta
baseUrlstring?''Base URL for absolute hreflang hrefs

Return type (LocaleHeadMeta):

PropertyTypeDescription
htmlAttrs{ lang: string }HTML element attributes
linkArray<{ rel, hreflang, href }>Alternate hreflang links
metaArray<{ property, content }>Open Graph locale meta

A locale-aware link component. Automatically prefixes the to path with the current locale.

<template>
<NuxtLinkLocale to="/about">About</NuxtLinkLocale>
<!-- When locale is 'ja': renders <a href="/ja/about">About</a> -->
<!-- When locale is 'en' (default): renders <a href="/about">About</a> -->
</template>

The module automatically registers @fluenti/vite-plugin with framework: 'vue' so you don’t need to configure it manually. This means v-t directive transforms, <Trans> / <Plural> compile-time optimization, and code splitting all work out of the box.

To disable this (e.g. if you configure the plugin yourself):

export default defineNuxtConfig({
fluenti: {
autoVitePlugin: false,
},
})

Low-level functions for programmatic use.

localePath(path, locale, defaultLocale, strategy)

Section titled “localePath(path, locale, defaultLocale, strategy)”

Add a locale prefix to a path based on the routing strategy.

localePath('/about', 'ja', 'en', 'prefix_except_default') // → '/ja/about'
localePath('/about', 'en', 'en', 'prefix_except_default') // → '/about'
localePath('/', 'ja', 'en', 'prefix') // → '/ja'

switchLocalePath(currentPath, newLocale, locales, defaultLocale, strategy)

Section titled “switchLocalePath(currentPath, newLocale, locales, defaultLocale, strategy)”

Get the current path in a different locale, stripping and re-applying the locale prefix.

switchLocalePath('/ja/about', 'en', ['en', 'ja'], 'en', 'prefix_except_default')
// → '/about'
switchLocalePath('/about', 'ja', ['en', 'ja'], 'en', 'prefix_except_default')
// → '/ja/about'

Extract the locale code from a path prefix.

extractLocaleFromPath('/ja/about', ['en', 'ja'])
// → { locale: 'ja', pathWithoutLocale: '/about' }
extractLocaleFromPath('/about', ['en', 'ja'])
// → { locale: null, pathWithoutLocale: '/about' }

Extend a Nuxt pages array with locale-prefixed route variants. Mutates the array in place (Nuxt convention).

const pages = [
{ path: '/', name: 'index' },
{ path: '/about', name: 'about' },
]
extendPages(pages, {
locales: ['en', 'ja'],
defaultLocale: 'en',
strategy: 'prefix_except_default',
})
// pages now also contains:
// { path: '/ja', name: 'index___ja' }
// { path: '/ja/about', name: 'about___ja' }

The module detects the user’s locale by running an ordered chain of detectors. The first detector to call ctx.setLocale() wins; subsequent detectors are skipped.

Default detection order: pathcookieheader.

Customize the detection order or add custom detectors:

export default defineNuxtConfig({
modules: ['@fluenti/nuxt'],
fluenti: {
locales: ['en', 'ja', 'zh'],
defaultLocale: 'en',
detectOrder: ['query', 'cookie', 'path', 'header'],
},
})
NameSourceDescription
pathURL path prefixExtracts locale from /ja/about'ja'. Skipped when strategy is 'no_prefix'.
cookieCookie valueReads the locale cookie. Requires detectBrowserLanguage.useCookie: true.
headerAccept-Language headerNegotiates against available locales. SSR only.
query?locale= query paramReads locale from the URL query string.
interface DetectBrowserLanguageOptions {
/** Use a cookie to persist the detected locale */
useCookie?: boolean
/** Cookie key name (default: 'fluenti_locale') */
cookieKey?: string
/** Fallback locale when detection fails */
fallbackLocale?: string
}

A Nuxt runtime hook fired after the detector chain completes (when no detector matched). Use this to inject locale from any custom source — database, JWT, session, external API, etc.

plugins/db-locale.ts
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('fluenti:detect-locale', async (ctx) => {
// ctx.detectedLocale is null if no detector matched yet
if (ctx.detectedLocale) return
// Read from any source: database, JWT, session...
const user = nuxtApp.ssrContext?.event?.context.user
if (user?.preferredLocale) {
ctx.setLocale(user.preferredLocale)
}
})
})

The context object passed to detectors and the fluenti:detect-locale hook:

PropertyTypeDescription
pathstringRequest path (e.g. '/ja/about')
localesstring[]Available locale codes
defaultLocalestringDefault locale code
strategyStrategyCurrent routing strategy
detectBrowserLanguageDetectBrowserLanguageOptions?Browser language detection config
detectedLocalestring | nullLocale detected so far (null if none)
setLocale(locale: string) => voidClaim a locale. Only accepts values in locales.
isServerbooleanWhether running on the server
type LocaleDetectorFn = (ctx: LocaleDetectContext) => void | Promise<void>