Files
myeasycms-v2/apps/web/lib/i18n/i18n.server.ts
giancarlo c121a3bdad Update "Password Reset" to "Update Password"
The code changes correspond to the rebranding of "Password Reset" to "Update Password". The names of components, functions, and routes have been updated to reflect this change. Also, a minor code optimization has been made in the i18n.server file for the creation of i18n server instances.
2024-04-19 17:27:11 +08:00

49 lines
1.3 KiB
TypeScript

import { cache } from 'react';
import { cookies, headers } from 'next/headers';
import {
initializeServerI18n,
parseAcceptLanguageHeader,
} from '@kit/i18n/server';
import {
I18N_COOKIE_NAME,
getI18nSettings,
languages,
} from '~/lib/i18n/i18n.settings';
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)
*/
function createInstance() {
const acceptLanguage = headers().get('accept-language');
const cookie = cookies().get(I18N_COOKIE_NAME)?.value;
let language =
cookie ??
parseAcceptLanguageHeader(acceptLanguage, languages)[0] ??
languages[0];
if (!languages.includes(language ?? '')) {
console.warn(
`Language "${language}" is not supported. Falling back to "${languages[0]}"`,
);
language = languages[0];
}
const settings = getI18nSettings(language);
return initializeServerI18n(settings, i18nResolver);
}
export const createI18nServerInstance = cache(createInstance);