Localisation support in GOV.UK Frontend #2740
Replies: 7 comments 9 replies
-
from hmrc perspective on the javascript side, we provide a bundled version of the govuk-frontend javascript along with the hmrc specific bits from hmrc-frontend that most tax services use so they don't call the init method themselves. But I don't think that would be a problem and I can't think of any issues with CSP for us- guess our code might change to look for service provided translations in the window scope and merge those with default translations provided by our library for welsh |
Beta Was this translation helpful? Give feedback.
-
Glad this is being tackled! A few questions about the javascript implementation... The page template currently includes the Would there be a way to specify the translations for multiple languages within the Would govuk-frontend contain localisations for any languages other than English? What would the Our service does have a Content Security Policy set. |
Beta Was this translation helpful? Give feedback.
-
To support ports of the nunjucks templates to other frameworks such as React, I would ask that the localised strings for all supported languages are included as a json file that can be imported from the govuk-frontend npm package |
Beta Was this translation helpful? Give feedback.
-
I wonder if it would be worth considering allowing the components to be initialised with the translations for more than one language at the same time, something like this: window.GOVUKFrontend.initAll({
accordion: {
i18n: {
cy: {
show_all_sections: "Dangoswch bob adran",
hide_all_sections: "Cuddiwch bob adran",
show_this_section: "Dangoswch yr adran hon",
hide_this_section: "Cuddio'r adran hon"
},
en: {
show_all_sections: "Show all sections",
hide_all_sections: "Hide all sections",
show_this_section: "Show this section",
hide_this_section: "Hide this section"
}
}
}
}) ...And then have some way to "set" which locale each component should use. Perhaps the component code could check the current The advantage to this might be that you'd then only need to compile a single javascript file which all pages could reference, rather than one for each language with some logic on which one reference on a given page. It might be simpler to implement too? A disadvantage might be that the single compiled file would be bigger, with users then having to download the translations for languages they may not see. This would only happen on the first page load though hopefully (and then it'd be cached). |
Beta Was this translation helpful? Give feedback.
-
Hi Vanita I'm coming at this from a non developer pov - more of a content bod and former UK English localisation person for a transactional service - so my thoughts may or may not be relevant or I may have missed that you've got this covered already. It'd be great if the (english or whatever starting language) " base" strings could be easily extracted and imported into a translation database / service, which does machine translation and enables human intervention / checking. Translated, approved strings can then get squirted back into the front end, with relevant tagging so system knows which language to display. Maintaining a translation memory of translated strings is good, to keep costs down and translation consistency and quality up. If there was some gizmo that enabled localisation people to see strings in context too that'd really helpful. Hope that helps - cheers Jeremy |
Beta Was this translation helpful? Give feedback.
-
Ooh tough problem! Good to see you're digging into it. I was mostly coming to read about your thing but then I wanted to share an idea. The idea of passing in the translated strings via JS seems like it'd be something that'd be tough going for GOV.UK to do as it'd be hard to compile our JS with it intersecting with locale files and similar. I did wonder whether you'd ruled out/considered using data attributes in the HTML for it? This feels like quite a natural use-case for them which seems like it could be simpler. That could allow abstracting the JS to need to be aware of locale at all and maintain a separation of concerns between content logic. You could have English as a default hardcoded and just fallback if a data attribute isn't provided. E.g. for an accordion:
|
Beta Was this translation helpful? Give feedback.
-
Thanks to everybody for providing us with really useful feedback on this proposal. We have decided to give users the ability to pass translations to components via data-* attributes, in addition to passing them through JavaScript. Translations provided through data-* attributes will take precedence over any translations passed via JavaScript. Nunjucks macro options will correspond directly to these data-* attributes. For more details, see @querkmachine’s summary here: #2780 (comment) |
Beta Was this translation helpful? Give feedback.
-
Localisation support in GOV.UK Frontend
Closing date: Closed - deadline was 12th August 2022
Status: Accepted. You can follow implementation progress here.
Update - 31 August 2022
Data attributes
We will add new data-* attributes to allow customisation of hardcoded English strings, for those users who are not using Nunjucks macros. These will take precedence over any translations passed by JavaScript.
Example of passing translations via data attributes:
Scope
This proposal relates to:
It does not cover:
Summary
GOV.UK Frontend currently contains components that include hardcoded English language text in Nunjucks and JavaScript code. Currently, service teams can only change this text by making a copy of the HTML or JavaScript of the component and changing the hardcoded text manually.
We intend to modify components that currently have hardcoded English text to allow this text to be customised, including the ability to change the content order where necessary.
We'd like your feedback on these proposed changes and how they might work for your services. You can:
💬 add comments to this discussion
✉️ send feedback to [email protected]
⬆️ upvote this comment if you agree with the proposed changes and have no other feedback to give
Background
Nunjucks
Most components in GOV.UK Frontend have text that can be customised using Nunjucks parameters. However, there are a small number of components where English language text, or a fixed content order that aligns with the English language, is hardcoded into the component’s HTML. One example is the header component’s ‘Menu’ button text.
Components currently containing hardcoded English or a fixed content order include:
JavaScript
Some components have accompanying JavaScript modules which inject hardcoded English text into the HTML of the component. This includes:
For example, it’s not currently possible to translate the “Show”/”Hide” buttons in the accordion component.
Problem
The inability for service teams who support non-English languages to use these GOV.UK Frontend components directly has created forked implementations of these components.
In addition to being inconvenient to create, these forked components can fall out of sync with future updates to GOV.UK Frontend, creating an ongoing maintenance burden for these teams.
Proposal
Nunjucks
We will add new Nunjucks macro options for any hardcoded English strings, to allow them to be customised. This will include, where necessary, the ability to customise the order that text strings are assembled in.
By introducing new macro options, this approach should be backwards compatible and will preserve the current text and content order for existing implementations.
JavaScript
We will allow translation strings to be passed into component JavaScript at the point of initialising, either via the components constructor or the
initAll
method. Translations will be passed as an object with key value pairs.Example of passing translations via
initAll
:Example of passing translations to a specific component:
We are concerned that this approach may not work for everyone, depending on how you import and initialise the GOV.UK Frontend JavaScript into your project. If you use a bundler in your project, don’t initialise our JavaScript code in your HTML, or your site has a Content Security Policy (CSP) we would especially like to hear about how this approach would work for you.. Likewise, if you have other concerns about how this would work for your service.
Each component will have its own default strings, making this backwards compatible with existing implementations and preserving the existing text if no translation strings have been supplied.
We will also provide a way of supplying different plural forms for translation strings that interpolate numbers. This is needed for components like the character count, for example:
This is based on the naming specification created by Unicode and used by JavaScript’s Internationalization API.
Dynamic variables
For both Nunjucks and JavaScript, a standardised syntax for interpolation of dynamic variables into the string will be created. We intend to use the
%{variableName}
format that is already utilised by GOV.UK’s localisation files.Release plan
We intend to release localisation support in multiple stages.
The first part will be shipped as part of a future release of GOV.UK Frontend v4, and will include:
Once our support for Internet Explorer has been reduced (including ending our JavaScript support for IE), we will be able to take advantage of the Internationalization API to add additional features, such as:
Beta Was this translation helpful? Give feedback.
All reactions