Decide JavaScript and Nunjucks API for passing translation strings
See original GitHub issueWhat
We need to decide:
- what the JavaScript object and keys look like for passing translation strings to component JS
- what the Nunjucks API keys look like for passing translation strings through to component data attributes
- what the data attributes look like for translation strings for JS
Example: are keys formatted like show_all_sections
, showAllSections
? Do they need prefixing with i18n
to separate them from other Nunjucks params? What do the data attributes look like, e.g: data-show-all-sections="hello"
, data-i18n-show-all-sections="hello"
.
Explore the different options and come to a decision on our preferred option.
Why
We want to implement two ways of passing translation strings to component JS. By designing and agreeing the API for this now, we will make sure that the JS and data-attributes options are consistent with each other, and we’ll be setting conventions for enabling translation of strings in new component JS in future.
Who needs to work on this
Developers
Who needs to review this
Developers
Done when
- We’ve made a decision on the above
- We’ve written up our decision on this card (which we can then use when implementing the logic)
Issue Analytics
- State:
- Created a year ago
- Comments:20 (19 by maintainers)
Top Results From Across the Web
Add ability to pass translation strings to accordion component ...
The accordion component has new Nunjucks macro options for passing in translation strings (following the format decided on in Decide JavaScript and Nunjucks...
Read more >nunjucks templating docs
This variable can contain a string that points to a template file, ... If you have passed a javascript method to your template,...
Read more >How to safely render JSON into an inline <script> using ...
We're rendering this from a Nunjucks template, and passing the JSON object as a value data in the context. This can contain arbitrary...
Read more >Deploy a basic "Google Translate" Express.js app on App ...
You will learn how to use the Google Cloud Translation API in a simple web application. This app can be run locally or...
Read more >Four Killer Features of Nunjucks | CSS-Tricks
Nunjucks calls itself “A rich and powerful templating language for JavaScript”, which sounds about right. It's not intentionally super ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The team seems to be happy with the decisions made here, so I’m going to consider this complete.
To summarise:
dataset
).i18n.
. Nunjucks parameters will not, to be consistent with how existing strings are already handled.Html
. The values passed to these should be manually escaped with theescape
/e
filter, to account for users who have the globalautoescape
option turned off.Examples
Passing a translation through a Nunjucks macro.
How this is interpolated into the template:
The equivalent HTML generated by the macro.
Passing a translation through the component’s JavaScript initialisation. This supports both nested and flat objects.
Passing a translation through Frontend’s
initAll
function. This will also support nested and flat objects, within each component’s namespace.After the configurations have been merged and flattened, the resulting object should look something like this:
I was testing in the review app, which has
autoescape
enabled. Turning it off makes a mess unless theescape
filter is used. The filter and config option seem to work identically in terms of what characters they escape.It does seem redundant to have both
Html
andText
versions—one that might be escaped byautoescape
, one that will be escaped with theescape
filter, both technically able to take HTML—we should probably stick withHtml
and explicitly escape it, though I don’t feel too strongly on that point.For what it’s worth, as none of the spikes carried out so far support IE, my means of testing in IE was relatively hacky (read: using console commands to try and extract the data-* attribute value and output it to an
innerHTML
somewhere). It worked in those instances, but I can’t say I have 100% confidence that it’s without caveats.