Design an Angular multi-language form validation system without separate translation files. Create a translation directive that wraps mat-error, accepts the error key like required or minlength with parameters, and interpolates the correct localized string dynamically.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Designing Angular directive with embedded translation dictionary
The plan is to create a translation service holding language dictionaries in code and an attribute directive applied to <mat-error> that takes an error key and parameters, then dynamically interpolates and updates the error message text without external translation files. The service emits language changes so directives update reactively. This meets the multi-language, no-separate-files requirement while wrapping mat-error as requested.
What you need at the end of the day are three moving parts:
somewhere to keep the texts for the languages you support,
a way of switching the active language, and
something you can hang on to <mat-error> that takes an Angular error key/parameters and turns it into the right string.
Because you don't want to ship XLF/JSON files and wire up a full i18n pipeline you can keep the translations in code and drive everything off a service.
A minimal implementation looks like this:
ts
@Injectable({providedIn: 'root'})
export class ValidationI18nService {
private _lang$ = new BehaviorSubject<'en'|'de'>('en');
private readonly messages = {
en: {
required : 'This field is required.',
minlength : 'Minimum length is {{requiredLength}} characters (you entered {{actualLength}}).',
maxlength : 'Maximum length is {{requiredLength}} characters.',
email : 'Please enter a valid e‑mail address.'
},
de: {
required : 'Dieses Feld ist erforderlich.',
minlength : 'Die Mindestlänge beträgt {{requiredLength}} Zeichen (Sie haben {{actualLength}} eingegeben).',
maxlength : 'Die maximale Länge beträgt {{requiredLength}} Zeichen.',
email : 'Bitte geben Sie eine gültige E‑Mail‑Adresse ein.'
}
};
set lang(code: 'en'|'de') { this._lang$.next(code); }
get lang$(): Observable<'en'|'de'> { return this._lang$.asObservable(); }
translate(key: string): string {
const lang = this._lang$.value;
return this.messages[lang]?.[key] ?? key;
}
}
The directive subscribes to lang$ so it updates automatically when the user flips languages. It accepts an error key and an optional params object to feed into the placeholders:
Here fg.get('name')?.errors?.minlength is the object Angular produces, e.g. {requiredLength: 3, actualLength: 1}, which the directive feeds into the {{requiredLength}}/{{actualLength}} placeholders.
If you prefer a structural wrapper instead of an attribute, the same idea applies:
and use it as <mat-error *appError="'minlength'; params: fg.controls.name.errors?.minlength"></mat-error>.
Because all translations live in ValidationI18nService, there are no external translation files to maintain, and the directive takes care of interpolating error parameters and updating when the language changes.