Translations
Documentation of Form.io Translations
Introduction
The Form.io Platform uses the i18Next system as the mechanism for performing all of the translations within the JavaScript Renderer and Builder. We recommend reading up on that documentation to understand all of the mechanisms that are available to perform translations within your application and forms rendered by Form.io. The Form.io Renderer exposes the i18next platform within the i18next
variable within the renderer, and all options for this library can be introduced through the i18n
configuration variable. For example, the following illustrates how the i18next framework can be initialized and accessed within the Form.io renderer.
Once you have these, the documentation for the i18Next framework applies. The following provides some examples of how you can render a form with internationalization.
Rendering Translations
In order to provide translations to the renderer, you can introduce these translations using the i18n configurations. There are also a few methods within the Form.io Renderer that can be used to change the language dynamically. For example, you can provide the following HTML code to introduce some buttons within your application that will trigger a language change like so.
Once you do this, you can then provide the following code to define the form, provide the translations, and then dynamically change those translations when the buttons have been clicked. The code will look as follows.
This code within a JSFiddle will work as follows.
Dynamic Translations using Resources
While the example above is very helpful, the only caveat is that it is not dynamic and requires an application change in order to introduce new translations. What would be better is if we can make the translations dynamic using Form.io Resources so that any non-developer can contribute to the translation process. This can be achieved using the following method.
Create a Language Resource
The first step is to create a Language resource within your Project, called Language. Within this resource, we will add the following fields.
A Select Field for the Language Code. For the DataSource, use "Values" and provide the language as the Label, and the lang-code as the value like the following illustrates.
A DataMap component, which contains a Translation TextField like the following illustrates.
This resource will look like the following.
Now press Create Resource button to create the new Resource.
Once this is created, you will then create a new submission for each translation like the following illustrates for Español.
Create the submission to add the "language". This is what will be used to manage all of the translations so you can now input the rest of your language translations the same way until you have multiple to choose from.
Language Resource "Read All Submissions" Access
For most applications, the end user will be authenticated when they are switching between languages, but in some cases, the user will be anonymous. As with any resource, you will now need to ensure that the proper Role has access to the Read All Submissions permission within the Access section of the Language form.
Dynamic Language Controller
Now that we have our language resource form, and have established the correct access, we can now write the logic that will perform the dynamic language translations. The form controller that establishes a dynamic language is pretty simple. We just need to send an API call to the form resource submission API (where we should have "read all" submission access) to fetch the submission that is associated with the language that was chosen. We also should implement some in-memory cache so that we do not need to re-fetch the submissions as the user clicks between different languages. We will just need to put most of the logic within the window.setLanguage
method used above. Here is what our Form Controller will look like.
Here is a JSFiddle demonstrating this dynamic loading system working.
Last updated