-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Quiddlee/add_multilang_functionality
Add multilang functionality
- Loading branch information
Showing
5 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const ru = { | ||
header: { h1: { h2: 'русский' } }, | ||
button: 'Нажми', | ||
}; | ||
|
||
export default ru; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |