1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-29 07:54:19 +00:00
pds-2021-g2-nest/nest_frontend/components/providers/GlobalLanguage.js
2021-05-23 16:20:53 +02:00

49 lines
1.5 KiB
JavaScript

import React, { useCallback } from "react"
import useLocalStorageState from "../../hooks/useLocalStorageState"
import ContextLanguage from "../../contexts/ContextLanguage"
import LocalizationStrings from "../../LocalizationStrings"
import Style from "./GlobalLanguage.module.css"
/**
* Provides {@link ContextLanguage} to all contained elements.
*
* Defaults to using Italian.
*
* @param children
* @returns {JSX.Element}
* @constructor
*/
export default function GlobalLanguage({ children }) {
const [lang, setLang] = useLocalStorageState("language", "it")
const getString = useCallback(
(target, name) => {
const languageStrings = LocalizationStrings[lang]
const defaultStrings = LocalizationStrings["it"]
if(languageStrings.hasOwnProperty(name)) {
return languageStrings[name]
}
else if(defaultStrings.hasOwnProperty(name)) {
console.warn("Missing ", lang, " localization for string ", name)
return <span className={Style.MissingLocalization} title={name}>{defaultStrings[name]}</span>
}
else {
console.warn("Missing string ", name)
return <i className={Style.MissingString} title={name}>MISSING STRING</i>
}
},
[lang],
)
const strings = new Proxy({}, { get: getString })
return (
<ContextLanguage.Provider value={{ lang, setLang, strings }}>
{children}
</ContextLanguage.Provider>
)
}