yarn
yarn install glotstack
import * as React from 'react'
import { GlotstackProvider } from 'glotstack'
const importMethod = (locale: string) => {
// fetch() from static directories to limit bundle sizes you will need to save your translations to a static path
return fetch(`/public/translations/${locale}.json`)
}
// // OR implement inline requires for built-in translations -- works with no dynamic import support
const translations: Record<string, () => Promise<Translations>> = {
'en-US': () => require('@/translations/en-US.json'),
'pl-PL': () => require('@/translations/pl-PL.json'),
'fr-FR': () => require('@/translations/fr-FR.json'),
};
const importMethod2 = (locale: string) => {
return translations[locale]();
}
const Application = () => {
const locale = 'en-US' // replace with your locale source
return (
<GlotstackProvider initialLocale={locale} importMethod={importMethod}>
{/*<YourComponent />*/}
</GlotstackProvider>
)
}
fetch()
pulls files from staticly hosted files that you host anywhere like a CDN - this also helps keep
bundle sizes small if you have a limited bundle size, like on Cloudflare Workers. fetch()
is almost always better
unless your environment cannot support dynamic import syntax, then the require()
method is your best bet for a
little more boilerplate.
For smaller applications or react-native, we recommend you use require()
to build the assets into
your hosted bundles.
useGlotstack()
inside your components
import * as React from 'react'
import {useGlotstack} from 'glotstack'
function MyComponent() => {
const {t} = useGlotstack()
return (
<div>
{t('Quickstart.example', {assigns: {userName: 'JD'}})}
{/* Icon can be a function reference or a node */}
{t('Quickstart.example2', {assigns: {Icon: <Icon/>}})}
</div>
)
}
en-US.json
. We recommend putting in a location like ./translations/en-US.json
. If you are worried about bundle sizes an reduce bundle sizes by splitting using public files like ./public/translations/en-US.json
.glotstack.json
in your projectget-translations
yarn glotstack get-translations
yarn glotstack live-translate
export GLOTSTACK_API_KEY="MbXsfV7sa5A8l9ENWWHkPqd1lRSmAegpPjAW8kpPaMjIcvePe3vlBE7O/VQokoA1UDZl3dA/gg7ytpHneB6uNA=="
yarn glotstack get-translations --source-path "./translations" --project-id "first-project" pl-PL fr-FR