At autobutler.dk we are currently developing a new flow for car owners to enter. This flow is based on a data-driven API. The new flow will be a front-end client of this API.
We are using Knockout as our front-end framework.
One of the hurdles we encounted was string interpolation. We normally use Ruby and Rails for everything.
In Ruby string interpolation is easy, just "this #{var} that"
will replace var
with the content of the var
-variable.
When using it together with I18n
s t
method, we use %{var}
instead of #{var}
.
If we had a header called with <%= t('.header', var: 'MY VAR') -%>
and this had a %{var}
in it, we would replace it.
Our prolem here is that the value of the variable isn’t known until the client gets to work.
One solution to this is to have the string split into several strings, that you can concatinate:
… really not a solution.
This is why we introduced the following code in our global Knockout scope:
This lets us interpolate Javsacript or Knockout variables into our strings. We can thus do the following:
And have %{email}
in the translation replaced with the contents of my_email_var
.