Files
ChatApp/packages/shared/src/i18n/index.ts
T
byGalax b61f929cf7 fix(shared): drop .js extensions from relative imports for Metro
packages/shared/src/index.ts and all sub-modules used .js extensions on
relative imports (e.g. './admin/index.js') pointing at .ts source files.
TypeScript with moduleResolution: "Bundler" doesn't need them, and
Metro's eager exporter (used for preview / production builds) reads
them literally and fails — only the dev-server Metro fell back to .ts.

Workspace typecheck remains 8/8 green; Vite and TS Bundler resolution
already accept both styles, so desktop is unaffected.
2026-05-15 01:52:14 +02:00

57 lines
1.7 KiB
TypeScript

import i18next, { type i18n as I18nInstance, type Resource } from 'i18next';
import { initReactI18next } from 'react-i18next';
import { resources } from './resources';
import { DEFAULT_LOCALE, type SupportedLocale } from './types';
export * from './detect';
export * from './error-map';
export * from './types';
export interface InitI18nOptions {
initialLocale: SupportedLocale;
// Called whenever the active language changes.
onLanguageChanged?: (locale: SupportedLocale) => void;
}
// Idempotent init — safe to call from multiple entry points.
// Returns the configured i18next instance.
export function initI18n(options: InitI18nOptions): I18nInstance {
if (!i18next.isInitialized) {
void i18next.use(initReactI18next).init({
resources: resources as unknown as Resource,
lng: options.initialLocale,
fallbackLng: DEFAULT_LOCALE,
defaultNS: 'common',
ns: ['common', 'auth', 'errors', 'app'],
interpolation: { escapeValue: false },
returnNull: false,
});
} else if (i18next.language !== options.initialLocale) {
void i18next.changeLanguage(options.initialLocale);
}
if (options.onLanguageChanged) {
i18next.off('languageChanged');
i18next.on('languageChanged', (lng: string) => {
if (isSupportedLocale(lng)) {
options.onLanguageChanged?.(lng);
}
});
}
return i18next;
}
export function changeLocale(locale: SupportedLocale): Promise<unknown> {
return i18next.changeLanguage(locale);
}
export { i18next };
// Local type guard (detect.ts exports this too; re-declared here to keep this
// file free of cyclic references).
function isSupportedLocale(value: string): value is SupportedLocale {
return value === 'en' || value === 'de';
}