1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 21:14:18 +00:00
pds-2021-g2-nest/nest_frontend/components/providers/GlobalLanguage.js

50 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-05-23 13:55:18 +00:00
import React, { useCallback } from "react"
2021-05-18 00:04:06 +00:00
import useLocalStorageState from "../../hooks/useLocalStorageState"
import ContextLanguage from "../../contexts/ContextLanguage"
import LocalizationStrings from "../../LocalizationStrings"
2021-05-23 13:55:18 +00:00
import Style from "./GlobalLanguage.module.css"
2021-05-18 00:04:06 +00:00
/**
* 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")
2021-05-23 13:55:18 +00:00
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)
2021-05-23 14:04:31 +00:00
return <span className={Style.MissingLocalization} title={name}>{defaultStrings[name]}</span>
2021-05-23 13:55:18 +00:00
}
else {
console.warn("Missing string ", name)
return <i className={Style.MissingString} title={name}>MISSING STRING</i>
2021-05-23 13:55:18 +00:00
}
},
[lang]
)
const strings = new Proxy({}, {get: getString})
2021-05-18 00:04:06 +00:00
return (
<ContextLanguage.Provider value={{ lang, setLang, strings }}>
{children}
</ContextLanguage.Provider>
)
}