Skip to content

Dynamic data bindings

Florian Hartmann edited this page Apr 25, 2016 · 3 revisions

This is obsolete

Dynamic data bindings were removed in x18n v2.0.0. They probably should never been part of x18n as it doesn't really fit into the library. This documentation is only here for people that still use x18n v1.


Dynamic data bindings

There are two use cases for dynamic data bindings:

  • You always interpolate the same string, this can be done more elegant with dynamic data bindings.
  • You want x18n to evaluate up the interpolation / plural value each time the translations are updated / the language changes. This is useful when working with adapters.

So here's how it works:

X18n globally evaluates everything between ${ and } when looking up translations, interpolations and plurals.

window.user = { name: 'John' }

x18n.register('de', {
	greeting: 'Hello ${user.name}'
});

t('greeting'); // 'Hello John'

window.user.name = 'Michael';
t('greeting'); // 'Hello Michael'

Disabling dynamic data bindings

Dynamic data bindings are a template engine like feature, and they might conflict with your template engine. That's why you can easily disable them:

x18n.dynamicBindings = false;

Clone this wiki locally