This is a very simple library common to every StackSpot Web project. This is responsible for implementing internationalization and currently supports english and portuguese.
- Get the current language:
getLanguage
,useLanguage
. - Change the current language:
setLanguage
. - Get the translations from a dictionary according to the current language:
translate
,useTranslate
. - Interpolate a string with variables:
interpolate
.
import { useTranslate, interpolate, Dictionary } from '@stack-spot/portal-translate'
const MyComponent = ({ username }: { username: string }) => {
const t = useTranslate(dictionary)
const language = useLanguage()
return (
<>
<h1>{t.title}</h1>
<p>{interpolate(t.hello, username, new Date().toLocaleString(language))}</p>
</>
)
}
const dictionary = {
en: {
title: 'Main page',
hello: 'Hello $0, today it is $1.',
},
pt: {
title: 'Página principal',
hello: 'Olá $0, hoje é $1.',
},
} satisfies Dictionary