forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support server-side i18n integration (nuxt-modules#2558)
- Loading branch information
Showing
47 changed files
with
4,021 additions
and
4,176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Server-side Translations | ||
|
||
The locale messages defined in nuxt i18n module options are integrated, so all you need to do is configure the locale detector. | ||
|
||
You can do the translation on the server side and return it as a response. | ||
|
||
--- | ||
|
||
::alert{type="warning"} | ||
**This feature is experimental,** that is supported from v8 RC8. | ||
:: | ||
|
||
## Define locale detector | ||
|
||
For server-side translation, you need to define locale detector. | ||
|
||
Nuxt i18n export the `defineI18nLocaleDetector` composable function to define it. | ||
You can use it to define the locale detector. | ||
|
||
The following is an example of defining a detector that detects locale using query, cookie, and header: | ||
```ts {}[localeDetector.ts] | ||
// 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 | ||
}) | ||
``` | ||
|
||
The locale detector function is used to detect the locale on server-side. It's called per request on the server. | ||
|
||
when you will define the locale detector, you need to pass the path to the locale detector to `experimental.localeDetector` option. | ||
|
||
The following is an example of locale detector configuration defined directly under Nuxt application: | ||
|
||
```ts {}[nuxt.config.ts] | ||
export default defineNuxtConfig({ | ||
// ... | ||
|
||
i18n: { | ||
experimental: { | ||
localeDetector: './localeDetector.ts' | ||
}, | ||
// ... | ||
}, | ||
|
||
// ... | ||
}) | ||
``` | ||
|
||
For details on the locale detector function defined by `defineI18nLocaleDetector`, see [here](../api/composables#definei18nlocaledetector). | ||
|
||
|
||
## `useTranslation` on eventHandler | ||
|
||
To translate on the server side, you need to call `useTranslation`. | ||
|
||
Example: | ||
```ts | ||
// you need to define `async` event handler | ||
export default defineEventHandler(async event => { | ||
// call `useTranslation`, so it retrun the translation function | ||
const t = await useTranslation(event) | ||
return { | ||
// call translation function with key of locale messages, | ||
// and translation function has some overload | ||
hello: t('hello') | ||
} | ||
}) | ||
``` | ||
|
||
::alert{type="info"} | ||
For the key of translation function, you can specify the locale messages set in nuxt i18n options in nuxt.config, or the locale loaded in i18n.config messages loaded in i18n.config. | ||
:: |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Detect based on query, cookie, header | ||
export default defineI18nLocaleDetector((event, config) => { | ||
const query = tryQueryLocale(event, { lang: '' }) | ||
if (query) { | ||
return query.toString() | ||
} | ||
|
||
const cookie = tryCookieLocale(event, { lang: '', name: 'i18n_locale' }) | ||
if (cookie) { | ||
return cookie.toString() | ||
} | ||
|
||
const header = tryHeaderLocale(event, { lang: '' }) | ||
if (header) { | ||
return header.toString() | ||
} | ||
|
||
return config.defaultLocale | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export default defineI18nLocale(async function (locale) { | ||
console.log('Loading locale', locale) | ||
return $fetch(`/api/${locale}`) | ||
console.log('Loading locale ...', locale) | ||
return $fetch(`/api/locales/${locale}`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default async function (locale) { | ||
return $fetch(`/api/${locale}`) | ||
return $fetch(`/api/locales/${locale}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script setup lang="ts"> | ||
const { locale } = useI18n() | ||
const { data } = await useFetch(`/api/server?locale=${locale.value}`) | ||
console.log('client locale', locale.value) | ||
console.log('fetch data from server-side', data.value) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<h1>fetch data from server-side</h1> | ||
<div> | ||
<code id="server-data">{{ data }}</code> | ||
</div> | ||
</div> | ||
</template> |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default defineEventHandler(async event => { | ||
const t = await useTranslation(event) | ||
return { | ||
hello: t('hello') | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../.nuxt/tsconfig.server.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.