Files
myeasycms-v2/packages/i18n/src/i18n.settings.ts
giancarlo 2782b26dc2 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.
2024-04-04 09:22:43 +08:00

47 lines
971 B
TypeScript

import { InitOptions } from 'i18next';
const fallbackLng = 'en';
export const languages: string[] = [fallbackLng];
export const I18N_COOKIE_NAME = 'lang';
/**
* The default array of Internationalization (i18n) namespaces.
* These namespaces are commonly used in the application for translation purposes.
*
* Add your own namespaces here
**/
export const defaultI18nNamespaces = [
'common',
'auth',
'account',
'teams',
'billing',
'marketing',
'admin',
];
export function getI18nSettings(
language: string | undefined,
ns: string | string[] = defaultI18nNamespaces,
): InitOptions {
let lng = language ?? fallbackLng;
if (!languages.includes(lng)) {
console.warn(
`Language "${lng}" is not supported. Falling back to "${fallbackLng}"`,
);
lng = fallbackLng;
}
return {
supportedLngs: languages,
fallbackLng,
lng,
fallbackNS: defaultI18nNamespaces,
defaultNS: defaultI18nNamespaces,
ns,
};
}