Skip to content

Commit 57d7f29

Browse files
committed
Add translation for validation
1 parent 2fb4b80 commit 57d7f29

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,52 @@ The function `validateFilledFieldsWithValidationRules` us really useful as you c
232232

233233
The function `controllerValidByRules` will check if a validation controller is valid.
234234

235+
This could be an example implementation
236+
```
237+
class FormExample {
238+
239+
@bindable({ defaultBindingMode: bindingMode.twoWay }) public user: User;
240+
241+
private controller: ValidationController;
242+
private rules: Rule<CustomerContactRestModel, any>[][];
243+
244+
public constructor(
245+
private validationControllerFactory: ValidationControllerFactory
246+
) {
247+
this.controller = this.validationControllerFactory.createForCurrentScope();
248+
this.controller.validateTrigger = validateTrigger.changeOrBlur;
249+
}
250+
251+
public bind(): void {
252+
this.setupValidationRules();
253+
validateFilledFieldsWithValidationRules(this.rules, this.user, this.controller);
254+
}
255+
256+
@computedFrom('user')
257+
public get isValid(): boolean {
258+
return controllerValidByRules(this.rules, this.user, this.controller);
259+
}
260+
261+
private setupValidationRules(): void {
262+
this.rules = ValidationRules
263+
.ensure((user: User) => user.lastName)
264+
.displayName('USER.LAST_NAME')
265+
.required()
266+
.ensure((user: User) => user.email)
267+
.displayName('USER.EMAIL')
268+
.email()
269+
.on(this.customerContact).rules;
270+
}
271+
}
272+
```
273+
274+
### i18n integration
275+
You can pass a tranlation string into the `displayName('USER.LAST_NAME')` and it will be translated for you.
276+
277+
Additionally you can translate methods like `.required()` in `src/local/*` as demostrated in the files.
278+
279+
If you use the the method `withMessageKey('YOUR.TRANSLATION')` you can pass a translation string and it will be translated for you.
280+
235281
## Route generator service
236282
If you have router tree like this
237283
```

src/app/main.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const appConfigService = new AppConfigService();
1515
*/
1616
import { Aurelia, LogManager, PLATFORM } from 'aurelia-framework';
1717
import { ConsoleAppender } from 'aurelia-logging-console';
18+
import { ValidationMessageProvider } from 'aurelia-validation';
19+
import { Expression } from 'aurelia-binding';
20+
import { I18N } from 'aurelia-i18n';
1821

1922
/**
2023
* Locals i18n imports
@@ -133,4 +136,19 @@ export async function configure(aurelia: Aurelia): Promise<void> {
133136
const offline = await System.import('offline-plugin/runtime');
134137
offline.install();
135138
*/
139+
140+
// Configure validation translations
141+
ValidationMessageProvider.prototype.getMessage = function (key): Expression {
142+
const i18n = aurelia.container.get(I18N);
143+
const translationId = `VALIDATIONS.${key}`;
144+
let translation = i18n.tr(translationId);
145+
if (translation === translationId) {
146+
translation = i18n.tr(key);
147+
}
148+
return (this as any).parser.parse(translation);
149+
};
150+
ValidationMessageProvider.prototype.getDisplayName = function (...args): string {
151+
const i18n = aurelia.container.get(I18N);
152+
return i18n.tr(args[1]);
153+
};
136154
}

src/locales/de_CH.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"VALIDATIONS": {
3+
"required": "${$displayName} fehlt!"
4+
},
25
"TITLE": "Übersetztungs Titel",
36
"TR_ATTR": "...in Deutsch",
47
"TR_SIGNALER": "Signaler Beispiel funktioniert..."

src/locales/en_US.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"VALIDATIONS": {
3+
"required": "${$displayName} missing!"
4+
},
25
"TITLE": "Translation Title",
36
"TR_ATTR": "...in English",
47
"TR_SIGNALER": "Signaler Example works..."

0 commit comments

Comments
 (0)