Translations
Documentation of Form.io Translations
Introduction
The Form.io Platform uses the i18Next system as the mechanism for displaying all of the translations within the JavaScript Renderer and Builder. Refer to the i18Next documentation to understand all of the mechanisms that are available to handle translations within an application and forms rendered by Form.io. The Form.io Renderer exposes the i18next platform within thei18next
variable in the renderer, and all options for this library can be introduced through the i18n
configuration variable.
The following example illustrates how the i18next framework can be initialized and accessed within the Form.io renderer:
Following initialization, the features documented for the i18Next framework apply. The following topics provide additional guidance on rendering a form with internationalization:
Rendering Translations
In order to provide translations to the renderer, introduce these translations using i18n configurations. There are also a few methods within the Form.io Renderer that can be used to change the language dynamically. For example, the following HTML code introduces buttons within the application that will trigger a language change:
After this, provide the following code to define the form, provide the translated text, and then dynamically change the text when the buttons have been clicked. The code will look as follows:
Right-to-left language support
In the above example, note the setAttribute()
element used to accommodate languages rendered in a right-to-left direction. Include any additional language codes that may need to be rendered this way.
This code within a JSFiddle will work as follows:
Dynamic Translations using Resources
The above example s not dynamic and requires an application change in order to introduce new translations. 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, add the following fields:
A Select Field for the Language Code. For the Data Source, use Values, and provide the language as the Label. Then, add the lang-code as the value as seen:
A Data Map component, which contains a Translation Text Field like the following:
The completed resource will look like the following:
Now press Create Resource button to create the new Resource.
Once this is created, use the resource to create a new submission for each translation. The following example illustrates the fields for Español:
Create the submission to add the "language". This will be used to manage all of the translations in a standard way. Input the rest of the language translations the same way until there are 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. In some cases, the user may be anonymous. As with any resource, 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 there is a language resource with the correct access, write the logic that will perform the dynamic language display. The form controller that establishes a dynamic language is pretty simple.
It will send an API call to the form resource submission API (where the user should have "read all" submission access) to fetch the Submission that is associated with the language that was chosen.
Implement some in-memory cache to reduce re-fetching the submissions as the user clicks between different languages. Put most of the logic within the window.setLanguage
method used above. Here is what the Form Controller should look like:
Here is a JSFiddle demonstrating this dynamic loading system working:
Custom Error Translation
Different error messages may be used based on what the current language is. You can achieve this by setting the i18n translations in the application. Below is an example of how to add translations to custom error messages
Start by creating a new form.
Add a Text Field to the form.
Open Settings > Validation > Custom Validation add the following code:
valid = input === 'example' ? true : 'mycustomerror';
In the application, create the form above and add translations for the error message. The code should look something like the following:
At this point, error message language will be set based on what the current language is.
To interpolate data from the form, use the {{}} notation. For example, to interpolate the current input being typed in, change 'Your name must be example' to 'Your name can't be {{data.textField}}'
Last updated
Was this helpful?