Translator
Customisable translator for backend and frontend usage with a statically typed translations facade.
Source code is hosted on GitHub
Works for server side and client side translations handling
Features static translations access, without using string based keys
Extremely easy to use and customise
Not bloated with unnecessary features
Seamless React integration is shipped in a separate package:
useTranslatorStatic translation access
Translations can be accessed in a static manner thanks to the createTranslatorAccessor()
helper. Check out @corets/use-translator docs for an example usage in React.
Custom interpolations
Provide a custom interpolation function used for replacement of placeholders with values:
You can access the default interpolation function anytime:
Custom formatting
Provide a custom formatter function used to format replacements before interpolation:
Custom placeholders
Provide a custom placeholder function used to generate placeholders for missing translation keys:
You can access the default placeholder function anytime:
createTranslator()
Create a new Translator
instance:
Create a translator instance without the factory function:
createTranslatorAccessor()
Create a statically typed facade for your translations. This allows you to access translations in a statically typed manner, no need to use string based translation keys anymore. Trying to access missing translations will lead to a compilation error! 💥
This functionality is powered by the accessor library:
AccessorTranslator.config()
Configure translator:
Translator.getLanguage()
Get current language:
Translator.setLanguage()
Change current language:
Translator.getLanguages()
Get all registered languages:
Translator.getFallbackLanguage()
Get fallback language:
A fallback language is used whenever a translation key is missing for current language.
Translator.setFallbackLanguage()
Change fallback language:
Translator.getTranslations()
Get all registered translations:
Returned object looks something like this:
Translator.getTranslationsForLanguage()
Get all translations for a specific language:
The returned object looks something like this:
Translator.setTranslations()
Replace all translations, for all languages, with the new ones:
Translator.setTranslationsForLanguage()
Replace all translations for a specific language:
Translator.addTranslations()
Update all translations, in all languages, using a merge:
Contrary to the Translator.setTranslations() method, this one will not replace all translations but do a merge instead.
Translator.addTranslationsForLanguage()
Add translations, for a specific language, using a merge:
Contrary to the Translator.setTranslationsForLanguage() method, this one will not replace all translations but do a merge instead.
Translator.get()
Retrieve a translation:
Retrieve a translation with a nested key:
Retrieve translation for another language:
Retrieve translation with another fallback language:
Retrieve translation without interpolating it:
Retrieve a translation and interpolate some values, using array syntax:
Retrieve a translation and interpolate some values, using object syntax:
Retrieve translation with a custom formatter, interpolator and placeholder:
Check out documentation for formatter, interpolator, and placeholder.
Translator.has()
Check if translation exists:
Check if translation exists for a specific language:
Check if translation exists in the current language or in a specific fallback language:
Translator.t()
Create a standalone translation function:
Create a translation function for a specific scope:
Override scope with ~
:
This method supports the same options object as Translator.get().
Translator.listen()
Listen to any kind of changes, like language change, translations change, etc.:
Last updated