Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multilang functionality #10

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/locales/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const en = {
header: { h1: { h2: 'english' } },
button: 'Press me',
};

export default en;
6 changes: 6 additions & 0 deletions src/locales/ru.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const ru = {
header: { h1: { h2: 'русский' } },
button: 'НаТми',
};

export default ru;
7 changes: 6 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import React from 'react';
import ReactDOM from 'react-dom/client';

import App from '@/App';

import LanguageProvider from './shared/Context/LanguageContext';

import '@/styles/index.css';

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
<LanguageProvider>
<App />
</LanguageProvider>
</React.StrictMode>,
);
36 changes: 36 additions & 0 deletions src/shared/Context/LanguageContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createContext, ReactNode, useCallback, useMemo, useState } from 'react';

import enTranslation from '@/locales/en';
import ruTranslation from '@/locales/ru';

type Translation = typeof enTranslation | typeof ruTranslation;

type LanguageContextType = {
language: string;
changeLanguage: () => void;
translation: Translation;
};

const TranslationFiles: { [key: string]: Translation } = {
en: enTranslation,
ru: ruTranslation,
};

export const LanguageContext = createContext<LanguageContextType>({} as LanguageContextType);

export default function LanguageProvider({ children }: { children: ReactNode }) {
const [language, setLanguage] = useState('en');

const changeLanguage = useCallback(() => {
const newLang = language === 'en' ? 'ru' : 'en';
setLanguage(newLang);
}, [language]);

const translation = TranslationFiles[language];
const contextValue = useMemo(
() => ({ language, changeLanguage, translation }),
[language, changeLanguage, translation],
);

return <LanguageContext.Provider value={contextValue}>{children}</LanguageContext.Provider>;
}
9 changes: 9 additions & 0 deletions src/shared/Context/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useContext } from 'react';

import { LanguageContext } from './LanguageContext';

const useLanguage = () => {
return useContext(LanguageContext);
};

export default useLanguage;