Source code is hosted on GitHubarrow-up-right
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
Copy yarn add @corets/translator
Copy npm install --save @corets/translator Seamless React integration is shipped in a separate package:
useTranslator chevron-right Static 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:
Copy import { createTranslator } from " @corets/translator "
const customInterpolator = ( text : string , match : string , replacement : any ) => {
return text . replace ( ` [[ ${ match } ]] ` , replacement )
}
const translator = createTranslator ( {}, { interpolator : customInterpolator } ) You can access the default interpolation function anytime:
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:
Accessor chevron-right Translator.config()
Configure translator:
Translator.getLanguage()
Get current language:
Translator.setLanguage()
Change current language:
Translator.getLanguages()
Get all registered languages:
Translator.getFallbackLanguage()
Get fallback 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:
Translator.addTranslationsForLanguage()
Add translations, for a specific language, using a merge:
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:
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:
Create a standalone translation function:
Create a translation function for a specific scope:
Override scope with ~:
Translator.listen()
Listen to any kind of changes, like language change, translations change, etc.: