B2B-88: add starter kit structure and elements
This commit is contained in:
41
packages/i18n/src/create-i18n-settings.ts
Normal file
41
packages/i18n/src/create-i18n-settings.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { InitOptions } from 'i18next';
|
||||
|
||||
/**
|
||||
* Get i18n settings for i18next.
|
||||
* @param languages
|
||||
* @param language
|
||||
* @param namespaces
|
||||
*/
|
||||
export function createI18nSettings({
|
||||
languages,
|
||||
language,
|
||||
namespaces,
|
||||
}: {
|
||||
languages: string[];
|
||||
language: string;
|
||||
namespaces?: string | string[];
|
||||
}): InitOptions {
|
||||
const lng = language;
|
||||
const ns = namespaces;
|
||||
|
||||
return {
|
||||
supportedLngs: languages,
|
||||
fallbackLng: languages[0],
|
||||
detection: undefined,
|
||||
lng,
|
||||
preload: false as const,
|
||||
lowerCaseLng: true as const,
|
||||
fallbackNS: ns,
|
||||
missingInterpolationHandler: (text, value, options) => {
|
||||
console.debug(
|
||||
`Missing interpolation value for key: ${text}`,
|
||||
value,
|
||||
options,
|
||||
);
|
||||
},
|
||||
ns,
|
||||
react: {
|
||||
useSuspense: true,
|
||||
},
|
||||
};
|
||||
}
|
||||
47
packages/i18n/src/i18n-provider.tsx
Normal file
47
packages/i18n/src/i18n-provider.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
'use client';
|
||||
|
||||
import type { InitOptions, i18n } from 'i18next';
|
||||
|
||||
import { initializeI18nClient } from './i18n.client';
|
||||
|
||||
let i18nInstance: i18n;
|
||||
|
||||
type Resolver = (
|
||||
lang: string,
|
||||
namespace: string,
|
||||
) => Promise<Record<string, string>>;
|
||||
|
||||
export function I18nProvider({
|
||||
settings,
|
||||
children,
|
||||
resolver,
|
||||
}: React.PropsWithChildren<{
|
||||
settings: InitOptions;
|
||||
resolver: Resolver;
|
||||
}>) {
|
||||
useI18nClient(settings, resolver);
|
||||
|
||||
return children;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name useI18nClient
|
||||
* @description A hook that initializes the i18n client.
|
||||
* @param settings
|
||||
* @param resolver
|
||||
*/
|
||||
function useI18nClient(settings: InitOptions, resolver: Resolver) {
|
||||
if (
|
||||
!i18nInstance ||
|
||||
i18nInstance.language !== settings.lng ||
|
||||
i18nInstance.options.ns?.length !== settings.ns?.length
|
||||
) {
|
||||
throw loadI18nInstance(settings, resolver);
|
||||
}
|
||||
|
||||
return i18nInstance;
|
||||
}
|
||||
|
||||
async function loadI18nInstance(settings: InitOptions, resolver: Resolver) {
|
||||
i18nInstance = await initializeI18nClient(settings, resolver);
|
||||
}
|
||||
81
packages/i18n/src/i18n.client.ts
Normal file
81
packages/i18n/src/i18n.client.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import i18next, { type InitOptions, i18n } from 'i18next';
|
||||
import LanguageDetector from 'i18next-browser-languagedetector';
|
||||
import resourcesToBackend from 'i18next-resources-to-backend';
|
||||
import { initReactI18next } from 'react-i18next';
|
||||
|
||||
// Keep track of the number of iterations
|
||||
let iteration = 0;
|
||||
|
||||
// Maximum number of iterations
|
||||
const MAX_ITERATIONS = 20;
|
||||
|
||||
/**
|
||||
* Initialize the i18n instance on the client.
|
||||
* @param settings - the i18n settings
|
||||
* @param resolver - a function that resolves the i18n resources
|
||||
*/
|
||||
export async function initializeI18nClient(
|
||||
settings: InitOptions,
|
||||
resolver: (lang: string, namespace: string) => Promise<object>,
|
||||
): Promise<i18n> {
|
||||
const loadedLanguages: string[] = [];
|
||||
const loadedNamespaces: string[] = [];
|
||||
|
||||
await i18next
|
||||
.use(
|
||||
resourcesToBackend(async (language, namespace, callback) => {
|
||||
const data = await resolver(language, namespace);
|
||||
|
||||
if (!loadedLanguages.includes(language)) {
|
||||
loadedLanguages.push(language);
|
||||
}
|
||||
|
||||
if (!loadedNamespaces.includes(namespace)) {
|
||||
loadedNamespaces.push(namespace);
|
||||
}
|
||||
|
||||
return callback(null, data);
|
||||
}),
|
||||
)
|
||||
.use(LanguageDetector)
|
||||
.use(initReactI18next)
|
||||
.init(
|
||||
{
|
||||
...settings,
|
||||
detection: {
|
||||
order: ['htmlTag', 'cookie', 'navigator'],
|
||||
caches: ['cookie'],
|
||||
lookupCookie: 'lang',
|
||||
},
|
||||
interpolation: {
|
||||
escapeValue: false,
|
||||
},
|
||||
},
|
||||
(err) => {
|
||||
if (err) {
|
||||
console.error('Error initializing i18n client', err);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// to avoid infinite loops, we return the i18next instance after a certain number of iterations
|
||||
// even if the languages and namespaces are not loaded
|
||||
if (iteration >= MAX_ITERATIONS) {
|
||||
console.debug(`Max iterations reached: ${MAX_ITERATIONS}`);
|
||||
|
||||
return i18next;
|
||||
}
|
||||
|
||||
// keep component from rendering if no languages or namespaces are loaded
|
||||
if (loadedLanguages.length === 0 || loadedNamespaces.length === 0) {
|
||||
iteration++;
|
||||
|
||||
console.debug(
|
||||
`Keeping component from rendering if no languages or namespaces are loaded. Iteration: ${iteration}. Will stop after ${MAX_ITERATIONS} iterations.`,
|
||||
);
|
||||
|
||||
throw new Error('No languages or namespaces loaded');
|
||||
}
|
||||
|
||||
return i18next;
|
||||
}
|
||||
151
packages/i18n/src/i18n.server.ts
Normal file
151
packages/i18n/src/i18n.server.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
import { type InitOptions, createInstance } from 'i18next';
|
||||
import resourcesToBackend from 'i18next-resources-to-backend';
|
||||
import { initReactI18next } from 'react-i18next/initReactI18next';
|
||||
|
||||
/**
|
||||
* Initialize the i18n instance on the server.
|
||||
* This is useful for RSC and SSR.
|
||||
* @param settings - the i18n settings
|
||||
* @param resolver - a function that resolves the i18n resources
|
||||
*/
|
||||
export async function initializeServerI18n(
|
||||
settings: InitOptions,
|
||||
resolver: (language: string, namespace: string) => Promise<object>,
|
||||
) {
|
||||
const i18nInstance = createInstance();
|
||||
const loadedNamespaces = new Set<string>();
|
||||
|
||||
await new Promise((resolve) => {
|
||||
void i18nInstance
|
||||
.use(
|
||||
resourcesToBackend(async (language, namespace, callback) => {
|
||||
try {
|
||||
const data = await resolver(language, namespace);
|
||||
loadedNamespaces.add(namespace);
|
||||
|
||||
return callback(null, data);
|
||||
} catch (error) {
|
||||
console.log(
|
||||
`Error loading i18n file: locales/${language}/${namespace}.json`,
|
||||
error,
|
||||
);
|
||||
|
||||
return callback(null, {});
|
||||
}
|
||||
}),
|
||||
)
|
||||
.use({
|
||||
type: '3rdParty',
|
||||
init: async (i18next: typeof i18nInstance) => {
|
||||
let iterations = 0;
|
||||
const maxIterations = 100;
|
||||
|
||||
// do not bind this to the i18next instance until it's initialized
|
||||
while (i18next.isInitializing) {
|
||||
iterations++;
|
||||
|
||||
if (iterations > maxIterations) {
|
||||
console.error(
|
||||
`i18next is not initialized after ${maxIterations} iterations`,
|
||||
);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 1));
|
||||
}
|
||||
|
||||
initReactI18next.init(i18next);
|
||||
resolve(i18next);
|
||||
},
|
||||
})
|
||||
.init(settings);
|
||||
});
|
||||
|
||||
const namespaces = settings.ns as string[];
|
||||
|
||||
// If all namespaces are already loaded, return the i18n instance
|
||||
if (loadedNamespaces.size === namespaces.length) {
|
||||
return i18nInstance;
|
||||
}
|
||||
|
||||
// Otherwise, wait for all namespaces to be loaded
|
||||
|
||||
const maxWaitTime = 0.1; // 100 milliseconds
|
||||
const checkIntervalMs = 5; // 5 milliseconds
|
||||
|
||||
async function waitForNamespaces() {
|
||||
const startTime = Date.now();
|
||||
|
||||
while (Date.now() - startTime < maxWaitTime) {
|
||||
const allNamespacesLoaded = namespaces.every((ns) =>
|
||||
loadedNamespaces.has(ns),
|
||||
);
|
||||
|
||||
if (allNamespacesLoaded) {
|
||||
return true;
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, checkIntervalMs));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const success = await waitForNamespaces();
|
||||
|
||||
if (!success) {
|
||||
console.warn(
|
||||
`Not all namespaces were loaded after ${maxWaitTime}ms. Initialization may be incomplete.`,
|
||||
);
|
||||
}
|
||||
|
||||
return i18nInstance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the accept-language header value and return the languages that are included in the accepted languages.
|
||||
* @param languageHeaderValue
|
||||
* @param acceptedLanguages
|
||||
*/
|
||||
export function parseAcceptLanguageHeader(
|
||||
languageHeaderValue: string | null | undefined,
|
||||
acceptedLanguages: string[],
|
||||
): string[] {
|
||||
// Return an empty array if the header value is not provided
|
||||
if (!languageHeaderValue) return [];
|
||||
|
||||
const ignoreWildcard = true;
|
||||
|
||||
// Split the header value by comma and map each language to its quality value
|
||||
return languageHeaderValue
|
||||
.split(',')
|
||||
.map((lang): [number, string] => {
|
||||
const [locale, q = 'q=1'] = lang.split(';');
|
||||
|
||||
if (!locale) return [0, ''];
|
||||
|
||||
const trimmedLocale = locale.trim();
|
||||
const numQ = Number(q.replace(/q ?=/, ''));
|
||||
|
||||
return [isNaN(numQ) ? 0 : numQ, trimmedLocale];
|
||||
})
|
||||
.sort(([q1], [q2]) => q2 - q1) // Sort by quality value in descending order
|
||||
.flatMap(([_, locale]) => {
|
||||
// Ignore wildcard '*' if 'ignoreWildcard' is true
|
||||
if (locale === '*' && ignoreWildcard) return [];
|
||||
|
||||
const languageSegment = locale.split('-')[0];
|
||||
|
||||
if (!languageSegment) return [];
|
||||
|
||||
// Return the locale if it's included in the accepted languages
|
||||
try {
|
||||
return acceptedLanguages.includes(languageSegment)
|
||||
? [languageSegment]
|
||||
: [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
}
|
||||
1
packages/i18n/src/index.ts
Normal file
1
packages/i18n/src/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './create-i18n-settings';
|
||||
Reference in New Issue
Block a user