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

The strings are not extracted from a Pipe who extends of Translate pipe #218

Open mberthouzoz opened 4 years ago

mberthouzoz commented 4 years ago

Environment

Angular: 8.2.14 ngx-translate-extract: 5.0.1 and 7.0.3

Description

I'm trying to extract the string from a Pipe. This Pipe extends from TranslatePipe.

Code

import { DatePipe } from '@angular/common';
import { ChangeDetectorRef, Pipe, PipeTransform } from '@angular/core';
import { TranslatePipe, TranslateService } from '@ngx-translate/core';

@Pipe({
  name: 'containerStatusLabel',
  pure: false
})
export class StatusLabelPipe extends TranslatePipe implements PipeTransform {
  constructor(
    private translateService: TranslateService,
    private changeDetectorRef: ChangeDetectorRef
  ) {
    super(translateService, changeDetectorRef);
  }

  transform(status: string, args?: string): string {
    switch (status) {
      case 'DELIVERED':
        return super.transform('Delivered');
      case 'SHIPPED':
        return super.transform('Shipped');
      case 'IN STOCK':
        return super.transform('In stock');
      case 'CANCELLED':
        return super.transform('Cancelled');
      case 'CREATED':
        return super.transform('Created');
      default:
        return '';
    }
  }
}

How to reproduce

ngx-translate-extract -f pot -i ./src -o ./i18n/template2.pot

Result

The strings( 'Delivered', 'Shipped', ...) are not extracted. I don't see these strings in the pot file.

Expected

The strings should be present in the pot file.


Someone has the same error or has an idea how to get the strings in the pot file without using a marker?

EnilPajic commented 3 years ago

We have same problem. Unfortunately, ngx-translate-extract does not extract by type (or inherited type), but merely by name. You can se here that it has hardcoded pipe name.

I've created PR to solve this (also to solve hardcoded directive names, marker name, service name and service available methods). It seems that this PR will be here for long, so I've published these changes on npm, so you can use it as @enilp/ngx-translate-extract@7.0.4.

So, in your case, you would use it (notice the --pipe containerStatusLabel) as:

ngx-translate-extract  --pipe containerStatusLabel -f pot -i ./src -o ./i18n/template2.pot

(Multiple pipes can be specified if you use both pipes, the built one translate and containerStatusLabel): --pipe translate --pipe containerStatusLabel