ngx-translate-extract
ngx-translate-extract copied to clipboard
Translate enum values under a namespace and be exhaustive
Linked to https://github.com/biesbjerg/ngx-translate-extract/issues/188
Using his same example
export enum Foo {
DAY = "Day",
NIGHT = "Night",
}
It would be great to be able to translate all enum values under a namespace. Something like
// currently does not work ? Or needs to be done someplace special ?
Object.values(Foo).forEach((value) => {
marker(`FOO.${value.toUpperCase()}`);
})
Hi @Startouf, I'm not sure if its doable in those examples due to the fact that your whole code has to be compiled, run through and interpreted in the correct way with various cases.
We were in the same situation as you and what we did was that we created a translate pipe with the markers within. As an example:
export enum Currency {
EURO = 'EUR',
US_DOLLAR = 'USD',
POUND_STERLING = 'GBP'
}
@Pipe({
name: 'currencyTranslate',
pure: false
})
export class CurrencyTranslatePipe implements PipeTransform {
constructor(private translatePipe: TranslatePipe) {
}
transform(value: Currency): string {
return this.translatePipe.transform(this.interpretValue(value));
}
interpretValue(value: Currency): string {
switch (value) {
case Currency.EURO:
return marker('COMMON.CURRENCY.EUR');
case Currency.POUND_STERLING:
return marker('COMMON.CURRENCY.GBP');
case Currency.US_DOLLAR:
return marker('COMMON.CURRENCY.USD');
default:
return '-';
}
}
}
A "downside" of this approach is that you have changes in two files, if you add one enum value. On the other hand you got the advantage that you can cluster your enum values in your translation files with prefixes (as shown in the example) and you got your business logic and translate logic separated.
Hope that helps or might be an idea. Greetings Daniel