Create Translator
Estimated reading time: 2 minutes
There are two main ways to create a translator in typed-locale:
- Using
createTranslatorfor a single language - Using
createTranslatorFromDictionaryfor multiple languages
Using createTranslator
For single-language applications or when you want to create a translator for a specific language, use the createTranslator function:
You can also provide default variables when values come from application context:
Using the Translator
Once you have created a translator, you can use it to access your translations:
The translator function takes a callback that receives the translation object and returns the desired translation key. This approach provides excellent type safety and autocompletion in your IDE.
Type Safety
One of the key benefits of using typed-locale's translator is the type safety it provides:
- It ensures that you only access valid translation keys.
- It enforces the correct usage of variables in translations.
- It provides autocompletion for translation keys in your IDE.
For example, trying to access a non-existent key or providing incorrect variables will result in TypeScript errors.