Skip to content

Commit

Permalink
fix: add translations to the type of validation message
Browse files Browse the repository at this point in the history
  • Loading branch information
gerjanvangeest committed May 27, 2024
1 parent 1e54376 commit 0179b3f
Show file tree
Hide file tree
Showing 41 changed files with 335 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-chicken-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': patch
---

[FeedbackValidation] add a translation of the validation feedback type to the beginning of the validation message
75 changes: 75 additions & 0 deletions packages/ui/components/form-core/src/localizeNamespaceLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-disable import/no-extraneous-dependencies */
export const localizeNamespaceLoader = /** @param {string} locale */ locale => {
switch (locale) {
case 'bg-BG':
return import('@lion/ui/form-core-translations/bg-BG.js');
case 'bg':
return import('@lion/ui/form-core-translations/bg.js');
case 'cs-CZ':
return import('@lion/ui/form-core-translations/cs-CZ.js');
case 'cs':
return import('@lion/ui/form-core-translations/cs.js');
case 'de-DE':
return import('@lion/ui/form-core-translations/de-DE.js');
case 'de':
return import('@lion/ui/form-core-translations/de.js');
case 'en-AU':
return import('@lion/ui/form-core-translations/en-AU.js');
case 'en-GB':
return import('@lion/ui/form-core-translations/en-GB.js');
case 'en-US':
return import('@lion/ui/form-core-translations/en-US.js');
case 'en-PH':
case 'en':
return import('@lion/ui/form-core-translations/en.js');
case 'es-ES':
return import('@lion/ui/form-core-translations/es-ES.js');
case 'es':
return import('@lion/ui/form-core-translations/es.js');
case 'fr-FR':
return import('@lion/ui/form-core-translations/fr-FR.js');
case 'fr-BE':
return import('@lion/ui/form-core-translations/fr-BE.js');
case 'fr':
return import('@lion/ui/form-core-translations/fr.js');
case 'hu-HU':
return import('@lion/ui/form-core-translations/hu-HU.js');
case 'hu':
return import('@lion/ui/form-core-translations/hu.js');
case 'it-IT':
return import('@lion/ui/form-core-translations/it-IT.js');
case 'it':
return import('@lion/ui/form-core-translations/it.js');
case 'nl-BE':
return import('@lion/ui/form-core-translations/nl-BE.js');
case 'nl-NL':
return import('@lion/ui/form-core-translations/nl-NL.js');
case 'nl':
return import('@lion/ui/form-core-translations/nl.js');
case 'pl-PL':
return import('@lion/ui/form-core-translations/pl-PL.js');
case 'pl':
return import('@lion/ui/form-core-translations/pl.js');
case 'ro-RO':
return import('@lion/ui/form-core-translations/ro-RO.js');
case 'ro':
return import('@lion/ui/form-core-translations/ro.js');
case 'ru-RU':
return import('@lion/ui/form-core-translations/ru-RU.js');
case 'ru':
return import('@lion/ui/form-core-translations/ru.js');
case 'sk-SK':
return import('@lion/ui/form-core-translations/sk-SK.js');
case 'sk':
return import('@lion/ui/form-core-translations/sk.js');
case 'uk-UA':
return import('@lion/ui/form-core-translations/uk-UA.js');
case 'uk':
return import('@lion/ui/form-core-translations/uk.js');
case 'zh-CN':
case 'zh':
return import('@lion/ui/form-core-translations/zh.js');
default:
return import('@lion/ui/form-core-translations/en.js');
}
};
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
import { html, LitElement } from 'lit';
import { css, html, LitElement } from 'lit';
import { LocalizeMixin } from '@lion/ui/localize-no-side-effects.js';
import { localizeNamespaceLoader } from '../localizeNamespaceLoader.js';

/**
* @typedef {import('./Validator.js').Validator} Validator
* @typedef {import('lit').TemplateResult} TemplateResult
* @typedef {import('../../types/validate/ValidateMixinTypes.js').FeedbackMessage} FeedbackMessage
*/

/**
* @param {string} string
*/
const capitalize = string => `${string[0].toUpperCase()}${string.slice(1)}`;

/**
* @desc Takes care of accessible rendering of error messages
* Should be used in conjunction with FormControl having ValidateMixin applied
*/
export class LionValidationFeedback extends LitElement {
export class LionValidationFeedback extends LocalizeMixin(LitElement) {
static get properties() {
return {
feedbackData: { attribute: false },
};
}

static localizeNamespaces = [
{ 'lion-form-core': localizeNamespaceLoader },
...super.localizeNamespaces,
];

static get styles() {
return [
css`
.validation-feedback__type {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(100%);
clip: rect(1px, 1px, 1px, 1px);
white-space: nowrap;
border: 0;
margin: 0;
padding: 0;
}
`,
];
}

/**
* @overridable
* @param {Object} opts
Expand Down Expand Up @@ -58,6 +89,9 @@ export class LionValidationFeedback extends LitElement {
${this.feedbackData &&
this.feedbackData.map(
({ message, type, validator }) => html`
<div class="validation-feedback__type">
${this._localizeManager.msg(`lion-form-core:validation${capitalize(type)}`)}
</div>
${this._messageTemplate({ message, type, validator })}
`,
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ export function runValidateMixinSuite(customConfig) {
expect(el.hasFeedbackFor).to.deep.equal(['error']);
const { _feedbackNode } = getFormControlMembers(el);
await el.updateComplete;
await el.updateComplete;
await _feedbackNode.updateComplete;
const messageHtmlNode = _feedbackNode.shadowRoot?.querySelector(`#${messageHtmlId}`);
expect(messageHtmlNode?.outerHTML).to.equal(messageHtml);
expect(messageHtmlNode?.tagName).to.equal('DIV');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ describe('lion-validation-feedback', () => {
const el = /** @type {LionValidationFeedback} */ (
await fixture(html`<lion-validation-feedback></lion-validation-feedback>`)
);
expect(el).shadowDom.to.equal('');
expect(el.shadowRoot?.textContent).to.not.include('hello');
el.feedbackData = [{ message: 'hello', type: 'error', validator: new AlwaysInvalid() }];
await el.updateComplete;
expect(el).shadowDom.to.equal('hello');
expect(el.shadowRoot?.textContent).to.include('hello');
});

it('renders the validation type attribute', async () => {
Expand Down Expand Up @@ -73,4 +73,32 @@ describe('lion-validation-feedback', () => {

clock.restore();
});

it('shares to the user the type of validation feedback', async () => {
const el = /** @type {LionValidationFeedback} */ (
await fixture(html`<lion-validation-feedback></lion-validation-feedback>`)
);

el.feedbackData = [{ message: 'hello', type: 'error', validator: new AlwaysInvalid() }];
await el.updateComplete;

const validationFeedbackType = el.shadowRoot?.querySelector('.validation-feedback__type');
expect(validationFeedbackType?.textContent?.trim()).to.equal('Error');

el.feedbackData = [{ message: 'hello', type: 'info', validator: new AlwaysInvalid() }];
await el.updateComplete;

expect(validationFeedbackType?.textContent?.trim()).to.equal('Info');
});

describe('accessibility', () => {
it('passes a11y audit when with a message', async () => {
const el = /** @type {LionValidationFeedback} */ (
await fixture(html`<lion-validation-feedback></lion-validation-feedback>`)
);
el.feedbackData = [{ message: 'hello', type: 'error', validator: new AlwaysInvalid() }];
await el.updateComplete;
await expect(el).to.be.accessible();
});
});
});
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/bg-BG.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import bg from './bg.js';

export default {
...bg,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/bg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Грешка',
validationWarning: 'Предупреждение',
validationSuccess: 'Успех',
validationInfo: 'Информация',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/cs-CZ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import cs from './cs.js';

export default {
...cs,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/cs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Chyba',
validationWarning: 'Varování',
validationSuccess: 'Úspěch',
validationInfo: 'Informace',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/de-DE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import de from './de.js';

export default {
...de,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/de.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Fehler',
validationWarning: 'Warnhinweis',
validationSuccess: 'Erfolgreich',
validationInfo: 'Info',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/en-AU.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import en from './en.js';

export default {
...en,
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/en-GB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import en from './en.js';

export default {
...en,
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/en-US.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import en from './en.js';

export default {
...en,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Error',
validationWarning: 'Warning',
validationSuccess: 'Success',
validationInfo: 'Info',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/es-ES.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import es from './es.js';

export default {
...es,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Error',
validationWarning: 'Advertencia',
validationSuccess: 'Satisfactorio',
validationInfo: 'Información',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/fr-BE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import fr from './fr.js';

export default {
...fr,
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/fr-FR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import fr from './fr.js';

export default {
...fr,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/fr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Erreur',
validationWarning: 'Avertissement',
validationSuccess: 'Succès',
validationInfo: 'Info',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/hu-HU.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import hu from './hu.js';

export default {
...hu,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/hu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Hiba',
validationWarning: 'Figyelmeztetés',
validationSuccess: 'Sikeres',
validationInfo: 'Információ',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/it-IT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import it from './it.js';

export default {
...it,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/it.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Errore',
validationWarning: 'Avvertenza',
validationSuccess: 'Operazione riuscita',
validationInfo: 'Info',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/nl-BE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import nl from './nl.js';

export default {
...nl,
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/nl-NL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import nl from './nl.js';

export default {
...nl,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/nl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Fout',
validationWarning: 'Waarschuwing',
validationSuccess: 'Succes',
validationInfo: 'Informatie',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/pl-PL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pl from './pl.js';

export default {
...pl,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/pl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Błąd',
validationWarning: 'Ostrzeżenie',
validationSuccess: 'Zrealizowano pomyślnie',
validationInfo: 'Informacja',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/ro-RO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ro from './ro.js';

export default {
...ro,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/ro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Eroare',
validationWarning: 'Atenție',
validationSuccess: 'Succes',
validationInfo: 'Informații',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/ru-RU.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ru from './ru.js';

export default {
...ru,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/ru.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Ошибка',
validationWarning: 'Предупреждение',
validationSuccess: 'Успешно',
validationInfo: 'Информация',
};
5 changes: 5 additions & 0 deletions packages/ui/components/form-core/translations/sk-SK.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sk from './sk.js';

export default {
...sk,
};
6 changes: 6 additions & 0 deletions packages/ui/components/form-core/translations/sk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
validationError: 'Chyba',
validationWarning: 'Varovanie',
validationSuccess: 'Úspešné',
validationInfo: 'Info',
};
Loading

0 comments on commit 0179b3f

Please sign in to comment.