Typed Locale

Dictionary

Estimated reading time: 1 minutes

For multi-language applications where you need to switch between languages, use the createTranslatorFromDictionary function:

import {createTranslatorFromDictionary} from 'typed-locale'; import {en, fr} from './translations'; const dictionary = {en, fr}; const translator = createTranslatorFromDictionary({ dictionary, locale: 'en', defaultLocale: 'en', defaultVariables: { appName: 'Acme App', }, });

defaultVariables are merged into every phrase and can still be overridden at the phrase call site:

const welcome = translator(t => t.welcome); console.log(welcome); // Output: 'Welcome to Acme App' const customWelcome = translator(t => t.welcome({appName: 'Custom App'})); console.log(customWelcome); // Output: 'Welcome to Custom App'

Switching Languages

When using createTranslatorFromDictionary, you can easily switch between languages by creating a new translator with a different locale:

const frTranslator = createTranslatorFromDictionary({ dictionary, locale: 'fr', defaultLocale: 'en', }); const frGreeting = frTranslator(t => t.hello); console.log(frGreeting); // Output: 'Bonjour'