Skip to content

Commit

Permalink
load i18n messages
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Oct 30, 2024
1 parent 70c8319 commit e95580c
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 260 deletions.
5 changes: 3 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { IntlProvider } from 'react-intl';
import { ErrorBoundary } from '@components/ErrorBoundary';
import { ROUTES } from '@common/constants/routes.constants';
import { OKAPI_CONFIG } from '@common/constants/api.constants';
import { BASE_LOCALE, i18nMessages } from '@common/i18n/messages';
import { localStorageService } from '@common/services/storage';
import { useLoadI18nMessages } from '@common/hooks/useLoadI18nMessages';
import { Root, Search, Load, EditWrapper, ExternalResourceEdit } from '@views';
import state from '@state';
import { ServicesProvider } from './providers';
Expand Down Expand Up @@ -59,13 +59,14 @@ const createRouter = (basename: string) => createBrowserRouter(routes, { basenam
const Container: FC<IContainer> = ({ routePrefix = '', config }) => {
const locale = useRecoilValue(state.config.locale);
const setCustomEvents = useSetRecoilState(state.config.customEvents);
const { i18nMessages, baseLocaleMessages } = useLoadI18nMessages();

useEffect(() => {
setCustomEvents(config?.customEvents as Record<string, string>);
}, [config]);

return (
<IntlProvider messages={i18nMessages[locale] || BASE_LOCALE} locale={locale} defaultLocale="en-US">
<IntlProvider messages={i18nMessages[locale] || baseLocaleMessages} locale={locale} defaultLocale="en-US">
<ErrorBoundary>
<ServicesProvider>
<RouterProvider router={createRouter(routePrefix)} />
Expand Down
20 changes: 20 additions & 0 deletions src/common/helpers/locales.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const loadI18nMessages = async (localesList: Record<string, string>) => {
const supportedLocales = Object.values(localesList);
const messages = {} as I18nMessages;

for await (const locale of supportedLocales) {
try {
const result = await fetch(`/translations/ui-linked-data/${locale}.json`);

if (result.ok) {
const parsedMessages = await result.json();

messages[locale] = parsedMessages;
}
} catch (error) {
console.error('Error occured while loading i18n messages', error);
}
}

return messages;
};
25 changes: 25 additions & 0 deletions src/common/hooks/useLoadI18nMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useEffect } from 'react';
import { useRecoilState } from 'recoil';
import { loadI18nMessages } from '@common/helpers/locales.helper';
import { LOCALES } from '@common/i18n/locales';
import baseLocaleMessages from '@translations/ui-linked-data/en.json';
import state from '@state';

export const useLoadI18nMessages = () => {
const [i18nMessages, setI18nMessages] = useRecoilState(state.config.i18nMessages);

useEffect(() => {
async function onLoad() {
try {
const messages = await loadI18nMessages(LOCALES);
setI18nMessages(messages);
} catch (error) {
console.error('Messages loading failed', error);
}
}

onLoad();
}, []);

return { i18nMessages, baseLocaleMessages };
};
4 changes: 1 addition & 3 deletions src/common/i18n/locales.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export const LOCALES = {
BASE_ENGLISH: 'en',
ENGLISH: 'en-US',
JAPANESE: 'ja-JA',
FRENCH: 'fr-FR',
GERMAN: 'de-DE',
};

export const LOCALE_DISPLAY_NAMES: Record<string, string> = {
Expand Down
254 changes: 0 additions & 254 deletions src/common/i18n/messages.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/state/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ const locale = atom<(typeof LOCALES)[keyof typeof LOCALES]>({
default: localStorageService.deserialize(OKAPI_CONFIG)?.locale || LOCALES.ENGLISH,
});

const i18nMessages = atom<I18nMessages>({
key: 'config.i18nMessages',
default: {},
});

const collectRecordDataForPreview = atom<boolean>({
key: 'config.collectRecordDataForPreview',
default: false,
Expand All @@ -61,6 +66,7 @@ export default {
initialSchemaKey,
selectedEntries,
locale,
i18nMessages,
collectRecordDataForPreview,
customEvents,
clonePrototypes,
Expand Down
1 change: 1 addition & 0 deletions src/types/config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type I18nMessages = Record<string, Record<string, string>>;
Loading

0 comments on commit e95580c

Please sign in to comment.