0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

fix(experience): update text content in the preview on custom phrases updated (#5192)

This commit is contained in:
Xiao Yijun 2024-01-03 11:20:08 +08:00 committed by GitHub
parent 0fa2017dc8
commit 2b273b4a6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 13 deletions

View file

@ -13,18 +13,17 @@ const usePreview = () => {
// Fetch the preview config
useEffect(() => {
// Init i18n
const i18nInit = initI18n();
// Listen to the message from the ancestor window
const previewMessageHandler = async (event: MessageEvent) => {
// #event.data should be guarded at the provider's side
if (event.data.sender === 'ac_preview') {
// Wait for i18n to be initialized
await i18nInit;
// eslint-disable-next-line no-restricted-syntax
setPreviewConfig(event.data.config as PreviewConfig);
const previewConfig = event.data.config as PreviewConfig;
// Wait for i18n to be initialized
await initI18n(previewConfig.language);
setPreviewConfig(previewConfig);
}
};

View file

@ -4,8 +4,11 @@ import { initReactI18next } from 'react-i18next';
import { getI18nResource } from '@/i18n/utils';
const initI18n = async () => {
const { resources, lng } = await getI18nResource();
// Call once globally
i18next.use(initReactI18next);
const initI18n = async (initialLanguage?: string) => {
const { resources, lng } = await getI18nResource(initialLanguage);
const options: InitOptions = {
resources,
@ -15,12 +18,14 @@ const initI18n = async () => {
},
};
i18next.use(initReactI18next);
const i18n = i18next.init(options);
// @ts-expect-error - i18next doesn't have a type definition for this. Must called after i18next is initialized
i18next.services.formatter.add('zhOrSpaces', (value: string, lng) => {
/**
* Note
* - Must call after i18next is initialized
* - Don't worry to call this multiple times, i18next will replace the formatter if it's already added.
*/
i18next.services.formatter?.add('zhOrSpaces', (value: string, lng) => {
if (lng !== 'zh-CN') {
return value;
}