Refactor i18n handling for language cookie and headers
The commit encompasses the aspect of refactoring the i18n handling for language cookies and headers. It also includes the deletion of get-language-cookie file and its transformation into a function inside i18n.server file. Extra functionalities were added to the i18n.server like enhancing the i18n server instance creation to consider the 'accept-language' header and default to environment provided values when necessary. The changes were also adjusted accordingly on the packages/i18n/package.json where deletion of "./cookie" was realized.
This commit is contained in:
@@ -1,10 +1,27 @@
|
||||
import getLanguageCookie from '@kit/i18n/cookie';
|
||||
import { initializeServerI18n } from '@kit/i18n/server';
|
||||
import { cookies, headers } from 'next/headers';
|
||||
|
||||
import {
|
||||
getLanguageCookie,
|
||||
initializeServerI18n,
|
||||
parseAcceptLanguageHeader,
|
||||
} from '@kit/i18n/server';
|
||||
|
||||
import { i18nResolver } from './i18n.resolver';
|
||||
|
||||
/**
|
||||
* @name createI18nServerInstance
|
||||
* @description Creates an instance of the i18n server.
|
||||
* It uses the language from the cookie if it exists, otherwise it uses the language from the accept-language header.
|
||||
* If neither is available, it will default to the provided environment variable.
|
||||
*
|
||||
* Initialize the i18n instance for every RSC server request (eg. each page/layout)
|
||||
*/
|
||||
export function createI18nServerInstance() {
|
||||
const cookie = getLanguageCookie();
|
||||
const acceptLanguage = headers().get('accept-language');
|
||||
const cookie = getLanguageCookie(cookies());
|
||||
|
||||
return initializeServerI18n(cookie, i18nResolver);
|
||||
const language =
|
||||
cookie ?? parseAcceptLanguageHeader(acceptLanguage)[0] ?? undefined;
|
||||
|
||||
return initializeServerI18n(language, i18nResolver);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user