mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-10 18:03:47 +00:00
5fafa08723
- remove legacy migrations - remove unused server code - remove unused UI code
65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { createContext, PropsWithChildren, useCallback, useContext } from 'react';
|
|
|
|
import useSettings from '../common/hooks-query/useSettings';
|
|
|
|
import { langDe } from './languages/de';
|
|
import { langEn } from './languages/en';
|
|
import { langEs } from './languages/es';
|
|
import { langFr } from './languages/fr';
|
|
import { langHu } from './languages/hu';
|
|
import { langIt } from './languages/it';
|
|
import { langNo } from './languages/no';
|
|
import { langPl } from './languages/pl';
|
|
import { langPt } from './languages/pt';
|
|
import { langSv } from './languages/sv';
|
|
import { langZhCn } from './languages/zh';
|
|
|
|
const translationsList = {
|
|
en: langEn,
|
|
es: langEs,
|
|
fr: langFr,
|
|
hu: langHu,
|
|
it: langIt,
|
|
de: langDe,
|
|
no: langNo,
|
|
pt: langPt,
|
|
sv: langSv,
|
|
pl: langPl,
|
|
zh: langZhCn,
|
|
};
|
|
|
|
interface TranslationContextValue {
|
|
getLocalizedString: (key: keyof typeof langEn, lang?: string) => string;
|
|
}
|
|
|
|
const TranslationContext = createContext<TranslationContextValue>({
|
|
getLocalizedString: () => '',
|
|
});
|
|
|
|
export const TranslationProvider = ({ children }: PropsWithChildren) => {
|
|
const { data } = useSettings();
|
|
|
|
const getLocalizedString = useCallback(
|
|
(key: keyof typeof langEn, lang = data?.language || 'en'): string => {
|
|
if (lang in translationsList) {
|
|
if (key in translationsList[lang as keyof typeof translationsList]) {
|
|
return translationsList[lang as keyof typeof translationsList][key];
|
|
}
|
|
}
|
|
return langEn[key];
|
|
},
|
|
[data?.language],
|
|
);
|
|
|
|
const contextValue = {
|
|
getLocalizedString,
|
|
};
|
|
|
|
return <TranslationContext.Provider value={contextValue}>{children}</TranslationContext.Provider>;
|
|
};
|
|
|
|
export const useTranslation = () => {
|
|
const { getLocalizedString } = useContext(TranslationContext);
|
|
return { getLocalizedString };
|
|
};
|