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
yarn npm
Copy yarn add @corets/translator
Copy npm install --save @corets/translator
Seamless React integration is shipped in a separate package:
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:
Copy import { translatorInterpolator } from "@corets/translator"
Custom formatting
Provide a custom formatter function used to format replacements before interpolation:
Copy import { createTranslator, translatorFormatter } from "@corets/translator"
import dayjs from "dayjs"
const customFormatter = (language: string, replacement: any, replacements: object) => {
if (replacement instanceof Date) {
const format = replacements?.format ?? "DD.MM.YYYY"
return dayjs(replacement).format(format)
}
return defaultTranslatorFormatter(language, replacement, replacements)
}
const translator = createTranslator({}, { formatter: customFormatter })
Custom placeholders
Provide a custom placeholder function used to generate placeholders for missing translation keys:
Copy import { createTranslator } from "@corets/translator"
const customPlaceholder = (language: string, key: string, replacements: object) => `${language}.${key}`
const translator = createTranslator({}, { placeholder: customPlaceholder })
You can access the default placeholder function anytime:
Copy import { translatorPlaceholder } from "@corets/translator"
createTranslator()
Create a new Translator
instance:
Copy import { createTranslator } from "@corets/translator"
const translations = {
en: { title: "Welcome" },
de: { title: "Willkommen" }
}
const translator = createTranslator(translations, { language: "en" })
Create a translator instance without the factory function:
Copy import { Translator } from "@corets/translator"
const translator = new Translator({}, { language: "en" })
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! 💥
Copy import { createTranslator, createTranslatorAccessor } from "@corets/translator"
const translations = {
en: { some: { nested: "value", another: "Hello {greeting}"} },
de: { some: { nested: "other"} },
}
const translator = createTranslator(translations, { language: "en" })
const locales = createTranslatorAccessor(translator, translations.en)
locales.some.nested.get()
locales.some.another.get({ replace: { greeting: "World" } })
locales.some.nested.get({ language: "de" })
This functionality is powered by the accessor library:
Translator.config()
Configure translator:
Copy import { createTranslator } from "@corets/translator"
const translator = createTranslator({ ... }, { language: "en" })
translator.config({
language: "en",
fallbackLanguage: "de",
interpolate: true,
formatter,
interpolator,
placeholder,
})
Translator.getLanguage()
Get current language:
Copy translator.getLanguage()
Translator.setLanguage()
Change current language:
Copy translator.setLanguage("en")
Translator.getLanguages()
Get all registered languages:
Copy translator.getLanguages()
Translator.getFallbackLanguage()
Get fallback language:
Copy transaltor.getFallbackLanguage()
A fallback language is used whenever a translation key is missing for current language.
Translator.setFallbackLanguage()
Change fallback language:
Copy translator.setFallbackLanguage("de")
Translator.getTranslations()
Get all registered translations:
Copy translator.getTranslations()
Returned object looks something like this:
Copy {
en: { key: "value" },
de: { key: "value" },
}
Translator.getTranslationsForLanguage()
Get all translations for a specific language:
Copy translator.getTranslationsForLanguage("en")
The returned object looks something like this:
Translator.setTranslations()
Replace all translations, for all languages, with the new ones:
Copy translator.setTranslations({ en: { key: "value" }})
Translator.setTranslationsForLanguage()
Replace all translations for a specific language:
Copy translator.setTranslationsForLanguage("en", { key: "value" })
Translator.addTranslations()
Update all translations, in all languages, using a merge:
Copy translator.addTranslations({ de: { key: "value" } })
Translator.addTranslationsForLanguage()
Add translations, for a specific language, using a merge:
Copy translator.addTranslationsForLanguage("en", { key: "value" })
Translator.get()
Retrieve a translation:
Copy translator.get("key")
Retrieve a translation with a nested key:
Copy translator.get("nested.key")
Retrieve translation for another language:
Copy translator.get("key", { language: "de" })
Retrieve translation with another fallback language:
Copy translator.get("key", { fallbackLanguage: "de" })
Retrieve translation without interpolating it:
Copy translator.get("key", { interpolate: false })
Retrieve a translation and interpolate some values, using array syntax:
Copy // given translation: {{1}} and {{2}} are colors
translator.get("key", ["red", "blue"])
Retrieve a translation and interpolate some values, using object syntax:
Copy // given translations: {{color1}} and {{color2}} are colors
translator.get("key", { color1: "red", color2: "blue" })
Retrieve translation with a custom formatter, interpolator and placeholder:
Copy translator.get("key", { formatter, interpolator, placeholder })
Translator.has()
Check if translation exists:
Copy translator.has("key")
Check if translation exists for a specific language:
Copy translator.has("key", { language: "en" })
Check if translation exists in the current language or in a specific fallback language:
Copy translator.has("key", { fallbackLanguage: "de" })
Translator.t()
Create a standalone translation function:
Copy const t = translator.t()
t("key")
// same as
translator.get("key")
Create a translation function for a specific scope:
Copy const t = translator.t("nested.scope")
t("key")
// same as
translator.get("nested.scope.key")
Override scope with ~
:
Copy const t = translator.t("nested.key")
t("~key")
// same as
translator.get("key")
Translator.listen()
Listen to any kind of changes, like language change, translations change, etc.:
Copy const unsubscribe = translator.listen(() => console.log("something has changed"))
unsubscribe()