Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate enum values under a namespace and be exhaustive #211

Open
Startouf opened this issue Sep 7, 2020 · 1 comment
Open

Translate enum values under a namespace and be exhaustive #211

Startouf opened this issue Sep 7, 2020 · 1 comment

Comments

@Startouf
Copy link

Startouf commented Sep 7, 2020

Linked to #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()}`);
})
@buchtadan
Copy link

buchtadan commented Sep 18, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants