Skip to content

Commit

Permalink
Merge pull request #5908 from formio/fix/FIO-9354_i18n_field_name_tra…
Browse files Browse the repository at this point in the history
…nslation

FIO-9354: Fix custom translation not applied to error message
  • Loading branch information
brendanbond authored Nov 18, 2024
2 parents 854241f + 0f1a78f commit dd1d115
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/utils/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ export class I18n {

t(text, ...args) {
if (this.currentLanguage[text]) {
const customTranslationFieldName = args[0]?.field;
if (customTranslationFieldName && this.currentLanguage[customTranslationFieldName]) {
args[0].field = this.currentLanguage[customTranslationFieldName]
}
return Evaluator.interpolateString(this.currentLanguage[text], ...args);
}
return Evaluator.interpolateString(text, ...args);
Expand Down
25 changes: 25 additions & 0 deletions test/forms/translationErrorMessages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default {
name: "textrandom",
path: "textrandom",
type: "form",
display: "form",
components: [
{
label: "My textField",
applyMaskOn: "change",
tableView: true,
validate: {
minLength: 5,
minWords: 2
},
validateWhenHidden: false,
key: "textField",
type: "textfield",
input: true
},
],
created: "2024-11-14T15:52:30.402Z",
modified: "2024-11-15T07:03:41.301Z",
machineName: "glvmkehegcvqksg:text",
}

24 changes: 24 additions & 0 deletions test/unit/Webform.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import webformWithNestedWizard from '../forms/webformWIthNestedWizard';
import formWithUniqueValidation from '../forms/formWithUniqueValidation.js';
import formWithConditionalEmail from '../forms/formWithConditionalEmail.js';
import formsWithSimpleConditionals from '../forms/formsWithSimpleConditionals.js';
import translationErrorMessages from '../forms/translationErrorMessages.js';
const SpySanitize = sinon.spy(FormioUtils, 'sanitize');

if (_.has(Formio, 'Components.setComponents')) {
Expand Down Expand Up @@ -928,6 +929,29 @@ describe('Webform tests', function() {
}).catch(done);
});

it('Should translate field name in error messages', (done) => {
const element = document.createElement('div');
const form = new Webform(element, {
language: 'en',
i18n: {
en: {
'My textField': 'My Value'
},
}
});
form.setForm(translationErrorMessages).then(() => {
const textField = form.getComponent('textField');
textField.setValue('123');
textField.onChange()
setTimeout(() => {
assert.equal(form.errors.length, 2);
assert.equal(form.errors[0].message, 'My Value must have at least 5 characters.');
assert.equal(form.errors[1].message, 'My Value must have at least 2 words.');
done();
}, 300);
}).catch(done);
});

it('Should translate value in franch if _userInput option is provided and value does not present in reserved translation names', done => {
const formElement = document.createElement('div');
const selectLabel = 'Select test label';
Expand Down

0 comments on commit dd1d115

Please sign in to comment.