From 0f1a78f969ce83c6ae1732214dacc2d36cea4cf1 Mon Sep 17 00:00:00 2001 From: mikekotikov Date: Fri, 15 Nov 2024 15:24:08 +0100 Subject: [PATCH] FIO-9354: Fix custom translation not applied to error message --- src/utils/i18n.js | 4 ++++ test/forms/translationErrorMessages.js | 25 +++++++++++++++++++++++++ test/unit/Webform.unit.js | 24 ++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 test/forms/translationErrorMessages.js diff --git a/src/utils/i18n.js b/src/utils/i18n.js index b1711d910c..c92b2ce950 100644 --- a/src/utils/i18n.js +++ b/src/utils/i18n.js @@ -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); diff --git a/test/forms/translationErrorMessages.js b/test/forms/translationErrorMessages.js new file mode 100644 index 0000000000..f4a78afd5e --- /dev/null +++ b/test/forms/translationErrorMessages.js @@ -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", +} + diff --git a/test/unit/Webform.unit.js b/test/unit/Webform.unit.js index 28cb768d2f..145d157693 100644 --- a/test/unit/Webform.unit.js +++ b/test/unit/Webform.unit.js @@ -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')) { @@ -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';