0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00
logto/packages/experience-legacy/src/i18n/utils.ts

82 lines
2.4 KiB
TypeScript
Raw Normal View History

refactor(experience): experience api migration (#6407) * refactor(experience): migrate the password register and sign-in migrate the password register and sign-in flow * fix(experience): update some namings update some namings * refactor(experience): refactor the verification code flow (migration-2) (#6408) * refactor(experience): refactor the verificaiton code flow refactor the verification code flow * refactor(experience): migrate the social and sso flow (migration-3) (#6406) * refactor(experience): migrate the social and sso flow migrate the social and sso flow * refactor(experience): migrate profile fulfillment flow (migration-4) (#6414) * refactor(experience): migrate profile fulfillment flow migrate the profile fulfillment flow * refactor(experience): remove unused hook remove unused hook * fix(experience): fix password policy checker fix password policy checker error display * fix(experience): fix the api name fix the api name * refactor(experience): migrate mfa flow (migration-5) (#6417) * refactor(experience): migrate mfa binding flow migrate mfa binding flow * test(experience): update unit tests (migration-6) (#6420) * test(experience): update unit tests update unit tests * chore(experience): remove legacy APIs remove legacy APIs * refactor(experience): revert api prefix revert api prefix * fix(experience): update the sso connectors endpoint update the sso connectors endpoint * chore: add changeset add changeset * fix(experience): comments fix comments fix * refactor(experience): refactor the code verificatin api refactor the code verification api * refactor(experience): code refactor refactor some implementation logic * feat(experience, core): add experience legacy package (#6527) add experience legacy package
2024-09-09 10:08:52 +08:00
import type { LocalePhrase } from '@logto/phrases-experience';
import resource from '@logto/phrases-experience';
import type { LanguageInfo } from '@logto/schemas';
import { isObject } from '@silverhand/essentials';
import type { Resource } from 'i18next';
import i18next from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { getPhrases as getPhrasesApi } from '@/apis/settings';
const getPhrases = async (language?: string) => {
// Directly use the server-side phrases if it's already fetched
if (isObject(logtoSsr) && (!language || logtoSsr.phrases.lng === language)) {
return { phrases: logtoSsr.phrases.data, lng: logtoSsr.phrases.lng };
}
const detectedLanguage = detectLanguage();
const response = await getPhrasesApi({
localLanguage: Array.isArray(detectedLanguage) ? detectedLanguage.join(' ') : detectedLanguage,
language,
});
const remotePhrases = await response.json<LocalePhrase>();
const lng = response.headers.get('Content-Language');
if (!lng) {
throw new Error('lng not found');
}
return { phrases: remotePhrases, lng };
};
export const getI18nResource = async (
language?: string
): Promise<{ resources: Resource; lng: string }> => {
try {
const { phrases, lng } = await getPhrases(language);
return {
resources: { [lng]: phrases },
lng,
};
} catch {
// Fallback to build in en
return {
resources: { en: resource.en },
lng: 'en',
};
}
};
const storageKey = 'i18nextLogtoUiLng';
export const detectLanguage = (languageSettings?: LanguageInfo) => {
if (languageSettings?.autoDetect === false) {
return languageSettings.fallbackLanguage;
}
const languageDetector = new LanguageDetector();
languageDetector.init(
// Pass in a empty i18n languageUtils server instance to bypass the [languageDetector detection](https://github.com/i18next/i18next-browser-languageDetector/blob/master/src/index.js#L70)
{ languageUtils: {} },
{
lookupLocalStorage: storageKey,
lookupSessionStorage: storageKey,
}
);
return languageDetector.detect();
};
// Must be called after i18n's initialization
export const changeLanguage = async (language: string) => {
const { resources, lng } = await getI18nResource(language);
for (const [namespace, resource] of Object.entries(resources[lng] ?? {})) {
i18next.addResourceBundle(lng, namespace, resource);
}
await i18next.changeLanguage(lng);
};