[BUG] Translations confusion with several forms on the same page
Bug Description
We have 2 similar forms embedded in the same page. Some fields are shared, but translation is different since they reflect 2 different contexts. We are loading the forms with the standard js
Formio.createForm(document.getElementById('[id]'),
'https://...',
{"language":"en","i18n":{"de": [....] }}}
).then(function(form) {
....
form.language = 'de';
.... } )
This code is repeated twice. Each form is displayed in a tab (but same issue is observed if both forms are just one after the other).
Randomly a form is not translated whereas the other one is correctly translated, or the translations of one form is applied totally or partially to the other one. Of course, this second flavor of the issue only concerns translations of identical keys.
Environment
- Hosting type
- [] Form.io
- [x ] Local deployment
- Version:
- Formio.js version: Latest (currently: v4.1.0)
- Frontend framework: Rails
- Browsers: Chrome 77 on OSX 10.13 and 10.14, Chrome 67 on Chromebook and also Firefox, safari. It looks like something may be related to how forms are redrawn. For example when previously hidden fields are displayed, the translation is almost always the wrong one.
Example
Page with 2 tabs and 2 forms: https://www.switchup.de/v2/widgets/is24/recommendations
The form is in German, but it's easy to spot the confusion. There are strings with "Strom" that should be displayed only on the "Strom" tab and other strings with "Gas" that should be displayed only on the "Gas" tab.
Another way to spot is to reload several times. The translations of the buttons in the "Strom" tab will change.
Source of form 1: https://mitra.switchup.de/forms/electricity-is24.js?selector=mytab
Source of form 2: https://mitra.switchup.de/forms/gas-is24.js?selector=mytab2
This may have something to do with the i18n library is not working well and not isolating between form instances. Sounds like a bug. We will take a look.
@gcrofils, you may want to try passing a new i18next instance in the options object during form instantiation - it looks like it's defaulting to a global instance: https://github.com/formio/formio.js/blob/e6cc73f41d0bfe618f9a14f5bded76fc8cbdf4fe/src/Webform.js#L28
@travist Indeed, it will be great to see it working without digging into i18next.
@aiwebb Thank you very much for this hint. For the last days, I've been struggling to achieve this without touching too much the current code. And I failed and failed again. May I ask for a bit of help here ? I would tremendously appreciate a few lines of code to point me to the right direction. Thanks !
@gcrofils, the i18next instance is being shared across both Webforms. After it has been initialized by the first Webform that loads, it is not reinitialized by the second Webform:
https://github.com/formio/formio.js/blob/e6cc73f41d0bfe618f9a14f5bded76fc8cbdf4fe/src/Webform.js#L312-L336
This may present as a race condition where the localization applied to both forms varies with each refresh.
It should probably be creating a fresh instance via i18next.createInstance() unless this.options.i18n is definitely known to correspond to an existing i18next instance - we would welcome a pull request with that!
@gcrofils It's possible that my fix in #2972 solves this problem. I don't believe it's made it into a release yet, but when it does I'd be happy to help you test it out.
@gcrofils It's possible that my fix in #2972 solves this problem. I don't believe it's made it into a release yet, but when it does I'd be happy to help you test it out.
I see your pull request's commit id (2259dba) in v4.10.4 commits But I am still not able to translate two instances of forms. Here is the jsfiddle of it happening: https://jsfiddle.net/Vedhachala/xav9qLfc/32/
@Vedhachala I forked your fiddle and I think it's working correctly, but I'm not 100% sure what you're expecting it to do. A couple of notes:
- I don't think that v4.10.4 includes 2259dba (you linked to the compare view, which includes that commit because it's in the diff between
v4.10.4andmaster). It looks like v4.11.0 does include it, though. - You had a typo ("langauge") in your render options, which is why you had to set
form.languageafter creating it. - Unfortunately, my PR doesn't create a new instance of i18next for each form, so in order for two forms on the same page to render in different languages in parallel, you'd need to create and pass your own instance to each form.
It probably makes sense for formio to initialize a new instance of i18next for each form, but I would defer to @travist and @randallknutson on whether that's really what they want. In translation tools I've been building, I bundle my own JS with i18next and pass a unique instance to each form.
@Vedhachala I forked your fiddle and I think it's working correctly, but I'm not 100% sure what you're expecting it to do. A couple of notes:
- I don't think that v4.10.4 includes 2259dba (you linked to the compare view, which includes that commit because it's in the diff between
v4.10.4andmaster). It looks like v4.11.0 does include it, though.- You had a typo ("langauge") in your render options, which is why you had to set
form.languageafter creating it.- Unfortunately, my PR doesn't create a new instance of i18next for each form, so in order for two forms on the same page to render in different languages in parallel, you'd need to create and pass your own instance to each form.
It probably makes sense for formio to initialize a new instance of i18next for each form, but I would defer to @travist and @randallknutson on whether that's really what they want. In translation tools I've been building, I bundle my own JS with i18next and pass a unique instance to each form.
@shawnbot thanks for the analysis. I was expecting my second instance of the form also to be translated using the translations provided in the options of the second form. Right now all the forms are getting translated using the first registered translation.
@Vedhachala There are two ways to go about this:
-
Access i18next via the first form and use that to create a new instance. The trick is that you have to initialize the second form inside the
.then()callback of the first. I don't think there's another way to access the instance of i18next that formio.js uses internally. -
Bring your own version of i18next (either from a CDN or in your own JS bundle) and initialize a new instance of it for each form:
const i18next1 = window.i18next.createInstance() const i18next2 = window.i18next.createInstance() Formio.createForm(..., json1, { i18next: i18next1, ...translations }).then(form1 => { ... }) Formio.createForm(..., json2, { i18next: i18next2, ...translations }).then(form2 => { ... })
The first one appears to work in this fiddle.
@shawnbot Thanks, this works great in the jsfiddle. However, in our application, we are using angular wrapper and the two form instances are a form builder and a form renderer. Something similar to the demo here where you have live builder and renderer.
I did the following and it does not work:
- As there is no "ready" output event in the angular form builder component, I am listening to change event and getting the i18next instance from there (only the first time) like this
event.builder.i18next. - After step one I am rendering form renderer dom, and sending the i18next instance (as you did in the fiddle) in as [options].
HTML file in my component looks like this
<formio [form] = "formJSON" [options]="_options"></formio>and my _options variable is this:{ i18next: {observers: {…}, options: {…}, services: {…}, logger: Logger, modules: {…}}, i18n: {resources: { fr: { translation: { "Name": "fr name (2)", "Apply": "fr apply (2)" } } }}, disableAlerts: true, language: "fr" }
I did a little investigation into angular-formio project. I found this function that is constructing the options and I see i18next is never passed even when it is sent in the options input. I cross checked it in the FormioOptions Interface too. There is no entry for i18next
So it would be nice if you guys can clarify if the issue is with angular-formio as I mentioned or am I missing something while trying this via angular.
Thanks, Vedhachala
You should be able to pass along this option using the renderOptions parameter within the Angular component like so.
<formio [form]="formJSON" [renderOptions]="{
i18next: i18NextInstance
}"></formio>
@travist @shawnbot Thanks for the help. The first method worked for me.