diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2021-09-02 20:33:23 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2021-09-02 20:33:23 -0700 |
commit | e07d5ff079527da0c07c4f8ede0aaef272517393 (patch) | |
tree | 1941791e4b8ab4b98c3eeefb5e0d00b34ff1bec0 /modern/src/common/localization.js | |
parent | 2b34c3d6c00c29b1590403e5d62e18b91145abcc (diff) | |
parent | 363198118bba28ad3a37e200a7db2ca02bf2eae7 (diff) | |
download | trackermap-web-e07d5ff079527da0c07c4f8ede0aaef272517393.tar.gz trackermap-web-e07d5ff079527da0c07c4f8ede0aaef272517393.tar.bz2 trackermap-web-e07d5ff079527da0c07c4f8ede0aaef272517393.zip |
Merge into oyhan-master
Diffstat (limited to 'modern/src/common/localization.js')
-rw-r--r-- | modern/src/common/localization.js | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/modern/src/common/localization.js b/modern/src/common/localization.js index 9e2123a9..90a5739a 100644 --- a/modern/src/common/localization.js +++ b/modern/src/common/localization.js @@ -1,3 +1,5 @@ +import React, { useState, createContext, useContext } from "react"; + import af from '../../../web/l10n/af.json'; import ar from '../../../web/l10n/ar.json'; import az from '../../../web/l10n/az.json'; @@ -110,8 +112,9 @@ const supportedLanguages = { zh_TW: { data: zh_TW, name: '中文 (Taiwan)' }, }; -const languages = window.navigator.languages !== undefined ? window.navigator.languages.slice() : []; -let language = window.navigator.userLanguage || window.navigator.language; +const languageList = Object.entries(supportedLanguages).map((values) => ({ code: values[0], name: values[1].name })); +const languages = localStorage.getItem('language') ? [localStorage.getItem('language')] : (window.navigator.languages !== undefined ? window.navigator.languages.slice() : []); +let language = localStorage.getItem('language') || window.navigator.userLanguage || window.navigator.language; languages.push(language); languages.push(language.substring(0, 2)); languages.push('en'); @@ -128,8 +131,40 @@ for (let i = 0; i < languages.length; i++) { } } -const selectedLanguage = supportedLanguages[language]; +let selectedLanguage = supportedLanguages[language]; + +export const findStringKeys = (predicate) => { + return Object.keys(selectedLanguage.data).filter(predicate); +} + +export default key => { + return selectedLanguage.data[key]; +}; + +const setSelectedLanguage = (language) => { + selectedLanguage = supportedLanguages[language]; + localStorage.setItem('language', language); +} + +const defaultLanguage = language; + +const LocalizationContext = createContext({ + language +}); -export const findStringKeys = (predicate) => Object.keys(selectedLanguage.data).filter(predicate); +export function LocalizationProvider(props) { + const [language, setLanguage] = useState(defaultLanguage); + + const handleLanguageChange = (nextLanguage) => { + setSelectedLanguage(nextLanguage); + setLanguage(nextLanguage); + }; + + return <LocalizationContext.Provider value={{ language, setLanguage: handleLanguageChange, languageList }}> + {props.children} + </LocalizationContext.Provider> +} -export default (key) => selectedLanguage.data[key]; +export const useLocalization = () => { + return useContext(LocalizationContext); +}
\ No newline at end of file |