i18n-polyfill icon indicating copy to clipboard operation
i18n-polyfill copied to clipboard

ngx-translate not parsing subclasses

Open johannks opened this issue 6 years ago • 8 comments

Hello

It seems like ngx-translate doesn't support inheritance.

It doesn't parse a subclass which extends a parent class (with a protected i18n: I18n) unless the subclass has a constructor, re-declares and imports all injectables, and calls super(...).

It's a workaround, but would it be possible to not have to do this?

johannks avatar Jun 10 '18 21:06 johannks

Having similar issue... im importing the the pollyfill in an shared service - but only the classes which directly import the pollyfill get parsed... what's the problem here? Is there a workaround?

FloNeu avatar Sep 17 '18 20:09 FloNeu

My workaround was to create a constructor in the child, which re-declared and re-imported all injectables. It then only calls super(...) to invoke the parent's constructor.

johannks avatar Sep 29 '18 01:09 johannks

Having similar issue... im importing the the pollyfill in an shared service - but only the classes which directly import the pollyfill get parsed... what's the problem here? Is there a workaround?

I'm having the exact same issue. Did you ever come across a solution FloNeu?

timminss avatar Jun 26 '19 14:06 timminss

Hi, there... Short answer no - Longer Answer - kind of :) I didn't wast to much time on it - but I guess the parser checks for the import and only parses the file if he finds it to save time and not having to check every file, which makes sense...

I just started to add a some-component.key.ts angular-service file to my components, which imports the methode and defines the keys I want to inject and use in it. If I want to re-use a key I just inject the service in another component. I found it makes it easier and more transparent for me which keys are outdated or still in use...

On Wed, Jun 26, 2019, 16:23 timminss [email protected] wrote:

Having similar issue... im importing the the pollyfill in an shared service - but only the classes which directly import the pollyfill get parsed... what's the problem here? Is there a workaround?

I'm having the exact same issue. Did you ever come across a solution FloNeu?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ngx-translate/i18n-polyfill/issues/30?email_source=notifications&email_token=AAKPZ6CH5UVH2GJ3NSGEBQTP4N3XJA5CNFSM4FEHQMU2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYTWLHQ#issuecomment-505898398, or mute the thread https://github.com/notifications/unsubscribe-auth/AAKPZ6FMIRE62RKYVSGH65DP4N3XJANCNFSM4FEHQMUQ .

FloNeu avatar Jun 26 '19 14:06 FloNeu

Thanks for your reply FloNeu! Would you mind going into a little more detail about your solution if you've got the time?

Is some-component.key.ts a service (only used by 'some-component') which has many hard coded items, and returns the correct one based on a function input?

timminss avatar Jun 26 '19 14:06 timminss

@timminss Hello, sorry - i did forget to come back to this... for example, this is the translation.key.ts file of my login-component ( i exchanged the default '_' methode with 'translation_key' what can be done when calling the parser with -m translation_key parameter ):

@Injectable( { providedIn: 'root' } )
export class FgLoginTranslationKeys {
    public readonly HEADLINE_TAB_LOGIN = translation_key( 'login_headline_tab_login' );
    public readonly HEADLINE_SUB_TAB_LOGIN = translation_key( 'login_headline_sub_tab_login' );
constructor() {}}

then in my component file constructor i import it like this.

class component {
constructor(
 $keys: FgLoginTranslationKeys
) {}}

and then use the key in my template:

<h1>{{ translate( $keys.HEADLINE_TAB_LOGIN ) | async }}</h1>

not that here the translate-methode is a base-component methode i inherit to all my components which wrapps the ngx-translation-service methode stream ( wich returns new translation when the language is changed ), like:

public translate( key: string ): Observable<any> {
    return this.$component.$translate.stream( key );
  }

and finally the npm-script i use to extract the translation master.json:

"i18n:get-master": "ngx-translate-extract -i ./src -o ./src/assets/i18n/master.json -m translation_key",

Hope that's useful - all the best, Florian

FloNeu avatar Jun 29 '19 10:06 FloNeu

Migrated to Angular 9. Tried migrated i18n to use $localize but It still did not fully work thus I have to revert back. Painful.

Confirmed that the extractor actually does not work on sub classes. Is there any way to force it work?

Please anyone shed me some light !? Much appreciated in advance.

mickeyjohn avatar Nov 25 '20 17:11 mickeyjohn

Had to use the workaround as @johannks mentioned before that is injecting i18n:I18n on every component's constructor.

mickeyjohn avatar Nov 25 '20 20:11 mickeyjohn