Class: I18nTranslation

I18nTranslation()

<brut-i18n-translation>

Manages a translation based on a key and value, with the value potentially having interpolation.

This is intended to be server-rendered with the subset of keys the server manages that are relevant to the front-end. Any other code on the page can then locate an element with the desired key and call translation to get the human-readable key. It is assumed that the server would render in the language required by the visitor, so there is no need to first select by locale.

Constructor

new I18nTranslation()

Properties:
Name Type Description
key string

an i18n key, presumably dot-delimited, however it can be any valid attribute value.

value string

the value of the key, in the browser's locale. It may contain placeholders for interpolation using %{«placeholder»} syntax.

Source:
Example
<brut-i18n-translation key="greeting" value="Hello %{username}"></brut-i18n-translation>

Methods

translation(interpolatedValues)

Called by other JavaScript to get the translated string.

Parameters:
Name Type Description
interpolatedValues Object

Object where the keys are placeholders in the string for interpolation and the values are the values to replace. Placeholders not in the translated value are ignored. Missing placeholders won't cause an error, but the placeholder will be present verbatim in the translated string.

Source:
Example
const element = document.querySeletor("brut-i18n-translation[key='greeting']")
if (element) {
  const translation = element.translation({ username: "Pat" })
  alert(translation) // Shows 'Hello Pat'
}