ngx-translate-extract icon indicating copy to clipboard operation
ngx-translate-extract copied to clipboard

Translation keys passed to instant() are missed depending on TranslateService's visibility

Open looorent opened this issue 4 years ago • 4 comments

Environment

  • Angular 9.1.9
  • @biesbjerg/ngx-translate-extract: 7.0.0

Description

When TranslationService is not called as a public or private attribute, arguments passed to instant are not extracted as translation keys. (I did not test other methods such as get). It is also ignored when TranslationService is not directly injected by Angular.

Scenario 1 - visibility readonly

constructor(readonly translate: TranslateService) {}
ngOnInit() {
  this.translate.instant("XYZ"); // XYZ is not extracted as a translation key
}

Scenario 2- service passed as an instance to another non-injectable context

@Injectable()
class InjectableService {
  private otherService: NonInjectableService;
  constructor(private translate: TranslateService) {
    this.otherService = new NonInjectableService(translate);
  }
  ngOnInit() {
    this.otherService.foo();
  }
}

class NonInjectableService {
  constructor(private translate: TranslateService) {}
  foo() {
    this.translate.instant("XYZ"); // XYZ is not extracted as a translation key
  }
}

Workaround

We currently use marker as a workaround but this makes us miss a lot a translations, especially because we use real english expressions/sentences as translation keys with gettext.

looorent avatar Jun 10 '20 06:06 looorent

We are having the same issue in a class which inherits from it's parent which has the translateService injected. The keys that are inside the child class are not picked up by extracting the translations.

  • "@biesbjerg/ngx-translate-extract": "^7.0.2"
  • "@biesbjerg/ngx-translate-extract-marker": "^1.0.0"
export abstract class BaseComponent implements AfterContentChecked, OnDestroy {
    constructor(
        protected translate: TranslateService
      ) { }
}

export class ChildComponent extends BaseComponent  implements OnInit {
    constructor(
        translate: TranslateService
      ) {
        super(translate);
      }

    foo() {
        this.translate.stream('some.key');
    }
}

Is there any workaround to have the translation key extracted, except using the translate marker everywhere? Right now it's easy to forget a translate marker on this.translate.stream/get/instant.

bjarne-callewaert avatar Jul 09 '20 11:07 bjarne-callewaert

Did anyone found a better workaround than having to use 'marker' everywhere?

gercobrandwijk avatar Nov 13 '20 16:11 gercobrandwijk

There is a fork that fix most of the issue: https://github.com/vendure-ecommerce/ngx-translate-extract Nevertheless, the visibility issue is not fixed. Maybe @michaelbromley can give a hint ?

dsnoeck avatar Jan 29 '24 09:01 dsnoeck

@dsnoeck Hi, when I created that fork, I decided not to bring over any existing issues, but you are free to create a new issue on that repo if you like.

michaelbromley avatar Jan 30 '24 11:01 michaelbromley