biesbjerg / ngx-translate-extract

Extract translatable (using ngx-translate) strings and save as a JSON or Gettext pot file
MIT License
525 stars 198 forks source link

Not extracting key within a variable #206

Open agarciabz opened 4 years ago

agarciabz commented 4 years ago

I store a translation key in a variable to reuse it later, but when I'm running a extraction script this key is not generated in my JSON file.

  private getTranslations(): void {
    const ieWarningKey = 'notification.ie';
    // key as a variable
    this.translate.get(ieWarningKey).subscribe((translations) => {
        this.ieWarningMessage = translations[ieWarningKey];
    });
  }

Strangely enough, the extraction works if I pass the key as a string literal.

// key as a literal
this.translate.get('notification.ie').subscribe((translations) => {
    this.ieWarningMessage = translations[ieWarningKey];
});

This is the script I'm using:

ngx-translate-extract --input ./src --output ./src/assets/i18n/en.json --format namespaced-json

My dependencies:

"@angular/core": "~10.0.5",
"typescript": "~3.9.7",
"@ngx-translate/core": "~13.0.0",
"@biesbjerg/ngx-translate-extract": "^7.0.2",
"@biesbjerg/ngx-translate-extract-marker": "^1.0.0",

Workaround: using marker on the variable declaration. It works this way.

private getTranslations(): void {
    const ieWarningKey = marker('notification.ie');
    // key as a variable + using marker on literal
    this.translate.get(ieWarningKey).subscribe((translations) => {
      this.ieWarningMessage = translations[ieWarningKey];
    });
  }

I find very strange that is not extracting if it's from a variable, could you take a look at this? Thanks.

scriptom commented 3 years ago

I believe it is intended that you use the marker declaration for non-trivial cases such as using a variable. I don't think the parser does complex checking such as when are variable used to extract the translation contained in them (which may change, it's unpredictable).

UzairNoman commented 3 years ago

I am facing the exact same problem as @agarciabz. It is only able to extract :

marker("String")

but do not extract:

let stringVar = "String";
marker(stringVar)