Tutorial - Internationalization of React apps - LinguiJS documentation
https://github.com/lukeed/rosetta
“Analicemos la forma simple”
// allLabels.json
{
"es":{
"buy": "Comprar",
"accept": "Aceptar",
...
},
"en-US":{
"buy": "Buy",
"accept": "Accept",
...
},
...
}
Para implementar esta solución mediante JavaScript se realizaría de la siguiente manera:
import allLabels from "locales/labels.json";
function getServerSideProps(){ ... }
function MyComponent ({locale}){
const labels = allLabels[locale];
return (
<Button>{labels.comprar}</Button>
)
}
Dividiendo por componentes
// locales/es/checkout.json
{
"buy": "Comprar",
"addToCart": "Agregar al carro",
"contactUs": "¿Interesado/a?"
}
// locales/es/about.json
{
"about": "Nosotros",
"phone": "Teléfono",
"contactUs": "Conócenos"
}
import useTranslations from "useTranslations"
function getServerSideProps(){}
function MyComponent(){
const labels = useTranslations()
return (
<Button>{labels("comprar")}</Button>
)
}
function useTranslations(){
const ctx = useContext(LocaleContext);
const locale = ctx.getCurrentLocale();
const labels = ctx.labels.get(locale);
return labels;
}