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.
1
Formio.createForm(document.getElementById('formio'), 'https://examples.form.io/example', {
2
i18n: {...} // These options are passed to the i18Next framework
3
}).then((form) => {
4
console.log(form.i18next); // Exposes the i18Next framework instance.
5
6
form.language = 'es'; // Changes the language of the i18Next framework.
7
});
Copied!
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.
1
<link rel="stylesheet" href="https://cdn.form.io/formiojs/formio.full.min.css">
2
<script src="https://cdn.form.io/formiojs/formio.full.min.js"></script>
3
<div class="btn-group">
4
<button type="button" class="btn btn-primary" onclick="setLanguage('sp')">Español</button>
5
<button type="button" class="btn btn-primary" onclick="setLanguage('en')">English</button>
6
<button type="button" class="btn btn-primary" onclick="setLanguage('ch')">中文</button>
7
</div>
8
<div id="formio"></div>
Copied!
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.
1
Formio.createForm(document.getElementById('formio'), {
2
components: [
3
{
4
type: 'textfield',
5
key: 'firstName',
6
label: 'First Name',
7
placeholder: 'Enter your first name',
8
input: true
9
},
10
{
11
type: 'textfield',
12
key: 'lastName',
13
label: 'Last Name',
14
placeholder: 'Enter your last name',
15
input: true
16
},
17
{
18
type: 'survey',
19
key: 'questions',
20
label: 'Survey',
21
values: [
22
{
23
label: 'Great',
24
value: 'great'
25
},
26
{
27
label: 'Good',
28
value: 'good'
29
},
30
{
31
label: 'Poor',
32
value: 'poor'
33
}
34
],
35
questions: [
36
{
37
label: 'How would you rate the Form.io platform?',
38
value: 'howWouldYouRateTheFormIoPlatform'
39
},
40
{
41
label: 'How was Customer Support?',
42
value: 'howWasCustomerSupport'
43
},
44
{
45
label: 'Overall Experience?',
46
value: 'overallExperience'
47
}
48
]
49
},
50
{
51
type: 'button',
52
action: 'submit',
53
label: 'Submit',
54
theme: 'primary'
55
}
56
]
57
}, {
58
language: 'sp',
59
i18n: {
60
sp: {
61
'First Name': 'Nombre de pila',
62
'Last Name': 'Apellido',
63
'Enter your first name': 'Ponga su primer nombre',
64
'Enter your last name': 'Introduce tu apellido',
65
'How would you rate the Form.io platform?': '¿Cómo calificaría la plataforma Form.io?',
66
'How was Customer Support?': '¿Cómo fue el servicio de atención al cliente?',
67
'Overall Experience?': '¿Experiencia general?',
68
Survey: 'Encuesta',
69
Excellent: 'Excelente',
70
Great: 'Estupendo',
71
Good: 'Bueno',
72
Average: 'Promedio',
73
Poor: 'Pobre',
74
'Submit': 'Enviar',
75
complete: 'Presentación Completa',
76
},
77
ch: {
78
'First Name': '名字',
79
'Last Name': '姓',
80
'Enter your first name': '输入你的名字',
81
'Enter your last name': '输入你的姓氏',
82
'How would you rate the Form.io platform?': '你如何评价Form.io平台?',
83
'How was Customer Support?': '客户支持如何?',
84
'Overall Experience?': '总体体验?',
85
Survey: '调查',
86
Excellent: '优秀',
87
Great: '大',
88
Good: '好',
89
Average: '平均',
90
Poor: '错',
91
'Submit': '提交',
92
complete: '提交完成',
93
}
94
}
95
}).then(function(form) {
96
window.setLanguage = function(lang) {
97
form.language = lang;
98
};
99
});
Copied!
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 achieve 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 TextField 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 chose 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.
1
var languages = {en: true};
2
window.setLanguage = function(lang) {
3
// If we already have loaded the language, then just set it with the sdk.
4
if (languages[lang]) {
5
return form.language = lang;
6
}
7
8
// Fetch the language from the Resource.
9
Formio.fetch('https://formiodata.form.io/language/submission?data.language=' + lang).then(function(resp) {
10
return resp.json();
11
}).then(function(result) {
12
// Ensure future sets will use the cached language.
13
languages[lang] = true;
14
15
// Add this language.
16
form.addLanguage(lang, result[0].data.translations);
17
18
// Set this language.
19
form.language = lang;
20
});
21
};
Copied!
Here is a JSFiddle demonstrating this dynamic loading system working.
Last modified 3mo ago