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

TranslateService used as an injected const does not work in a const method (like HttpInterceptorFn) #44

Closed
Laurensvdw opened this issue Apr 22, 2024 · 4 comments · Fixed by #46

Comments

@Laurensvdw
Copy link

A fix was done for readonly inject earlier in version 9.1.1. Additionally somewhat related support is required for cases like ResolveFn, HttpInterceptorFn,... which are const's rather than classes.

In ng14 or something the following syntax
@Injectable() export class ErrorToastInterceptor implements HttpInterceptor {
was (optionally) changed to
export const errorToastInterceptor: HttpInterceptorFn = (req: HttpRequest<unknown>, next: HttpHandlerFn) => {

Where the TranslationService is injected as a const, like:
const translateService = inject(TranslateService);

translateService.instant('KEY_NAME') is unfortunately not picked up :( I guess a method has to be added to the ast-helpers.ts to find the const translateService injectable, in a const method.

Thanks for looking into it!

@andrea-spotsoftware
Copy link

@Laurensvdw looks like that problem exists for all functions.
I'm using ngrx signal store and there are a lot of functions with injected TranslateService instance. Unfortunately, everything is ignored by the extract tool.

@pmpak
Copy link

pmpak commented May 2, 2024

This is happening because the service parser checks only inside class declarations. I did an attempt to extend it and see if it would have been easy to parse function expressions but while it looked that it worked it fell apart in some edge cases. For example, from the code below it extracted the ' , ' from the join instead of the translation.key

export const errorInterceptor: HttpInterceptorFn = (req, next) => {
  const translateService = inject(TranslateService);
  const router = inject(Router);

  translateService.instant('translation.key').join(', ');
};

I will give it another try when I find some more time to check it out.

@michaelbromley
Copy link
Member

This is now published as a pre-release v9.2.0-next.0, under the "next" tag. Let me know any feedback once you have tried it on your code base and all being well, I'll do a general release.

@Laurensvdw
Copy link
Author

I would gladly test this but the client's apps are still on Angular 16, being 8.x of this package 😭 Quite an important detail I forgot to mention... So move along, we'll update probably during the summer. 😨 As LTS is 8 Nov. 2024 for ng16

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