vendure-ecommerce / ngx-translate-extract

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

Parser not finding all strings #52

Open SamanthaAdrichem opened 5 days ago

SamanthaAdrichem commented 5 days ago

Not all translateService.instant translations are extracted

package.json

{
    "name": "translations",
    "version": "1.0.0",
    "description": "Translations for the interface",
    "scripts": {
        "scan-translations": "ngx-translate-extract --input ./api_translations ./interface_instance/src --format pot --output ./found-translations.pot",
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "@angular/compiler": "17.3.11",
        "@types/node": "20.14.9",
        "@vendure/ngx-translate-extract": "9.2.0",
        "braces": "3.0.3",
        "ts-node": "10.9.2",
        "typescript": "5.5.3"
    }
}

media.service.ts

import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {ApprovalStatusEnum} from 'src/app/admin/data/backend/media/approval-status.enum';
import {ApprovalStatusInterface} from 'src/app/admin/data/backend/media/approval-status.interface';
import {MethodEnum} from 'src/app/admin/data/backend/media/method.enum';
import {MethodInterface} from 'src/app/admin/data/backend/media/method.interface';
import {TypeEnum as BadgeTypeEnum} from 'src/app/core/components/badge/type.enum';
import {MediaService as CoreMediaService} from 'src/app/core/data/backend/media/media.service';
import {CacheService} from 'src/app/core/data/cache/cache.service';

@Injectable({providedIn: 'root'})
export class MediaService extends CoreMediaService {
    constructor(
        protected cacheService: CacheService,
        protected httpClient: HttpClient,
        translateService: TranslateService
    ) {
        super(translateService);
    }

    public getApprovalStatuses(): Record<ApprovalStatusEnum, ApprovalStatusInterface> {
        return {
            [ApprovalStatusEnum.APPROVED]: <ApprovalStatusInterface>{
                badgeType: BadgeTypeEnum.SUCCESS,
                value: ApprovalStatusEnum.APPROVED,
                label: this.translateService.instant('media_approval_status_approved')
            },
            [ApprovalStatusEnum.DISAPPROVED]: <ApprovalStatusInterface>{
                badgeType: BadgeTypeEnum.DANGER,
                value: ApprovalStatusEnum.DISAPPROVED,
                label: this.translateService.instant('media_approval_status_disapproved')
            },
            [ApprovalStatusEnum.OPEN]: <ApprovalStatusInterface>{
                badgeType: BadgeTypeEnum.WARNING,
                value: ApprovalStatusEnum.OPEN,
                label: this.translateService.instant('media_approval_status_open')
            },
            [ApprovalStatusEnum.PENDING]: <ApprovalStatusInterface>{
                badgeType: BadgeTypeEnum.WARNING,
                value: ApprovalStatusEnum.PENDING,
                label: this.translateService.instant('media_approval_status_pending')
            },
        };
    }

    public getVerificationMethods(): Record<MethodEnum, MethodInterface> {
        return {
            [MethodEnum.ADMIN]: <MethodInterface>{
                value: MethodEnum.ADMIN,
                label: this.translateService.instant('media_verification_method_admin'),
            },
            [MethodEnum.DNS]: <MethodInterface>{
                value: MethodEnum.DNS,
                label: this.translateService.instant('media_verification_method_dns'),
            },
            [MethodEnum.METATAG]: <MethodInterface>{
                value: MethodEnum.METATAG,
                label: this.translateService.instant('media_verification_method_metatag'),
            },
            [MethodEnum.PAGE]: <MethodInterface>{
                value: MethodEnum.PAGE,
                label: this.translateService.instant('media_verification_method_page'),
            },
            [MethodEnum.NONE]: <MethodInterface>{
                value: MethodEnum.NONE,
                label: this.translateService.instant('media_verification_method_none'),
            },
        };
    }
}

core/media.service.ts

import {Injectable} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {TypeEnum as BadgeTypeEnum} from 'src/app/core/components/badge/type.enum';
import {TypeGroupEnum} from 'src/app/core/data/backend/media/type-group.enum';
import {TypeGroupInterface} from 'src/app/core/data/backend/media/type-group.interface';

@Injectable()
export class MediaService {
    constructor(
        protected translateService: TranslateService
    ) {}

    public getMediaTypeGroups(): Record<TypeGroupEnum, TypeGroupInterface> {
        return <Record<TypeGroupEnum, TypeGroupInterface>>{
            [TypeGroupEnum.GROUPA]: <TypeGroupInterface>{
                badgeType: BadgeTypeEnum.CYAN,
                id: TypeGroupEnum.GROUP_A,
                label: this.translateService.instant('media_type_group_a')
            },
            [TypeGroupEnum.GROUPB]: <TypeGroupInterface>{
                badgeType: BadgeTypeEnum.LIGHT_BROWN,
                id: TypeGroupEnum.GROUP_B,
                label: this.translateService.instant('media_type_group_b')
            },
        };
    }
}

command

npx ngx-translate-extract --input interface_instance/src/app/admin/data/backend/media/media.service.ts --format pot --output test.pot

result:

Enabled parsers:
- PipeParser
- DirectiveParser
- ServiceParser
- MarkerParser

Enabled post processors:
(none)

Compiler:
- PoCompiler

Extracting:
- /home/samantha/vhosts/translations/interface_instance/src/app/admin/data/backend/media/media.service.ts

Found 0 strings.

Saving:
- /home/samantha/vhosts/translations/test.txt [CREATED]

Done.
SamanthaAdrichem commented 5 days ago

It seems to be a bug when extending as an alias

So most likely a bug in https://github.com/phenomnomnominal/tsquery? Can someone check this? Because for me the service parser and how it gets to the ast-helpers and does the queries is a bit much. I found a work-around for now :) (add protected infront of the translateService in the extend as well)

Create a folder src in there create these 2 files

src/base.ts

import {Injectable} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';

@Injectable()
export class Base {
    constructor(
        protected translateService: TranslateService
    ) {}

    public getBase(): string {
        return this.translateService.instant('test base');
    }
}

src/base-extend.ts

import {Injectable} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {Base as CoreBase} from 'src/base';

@Injectable({providedIn: 'root'})
export class BaseExtend extends CoreBase {
    constructor(
        translateService: TranslateService
    ) {
        super(translateService);
    }

    public getExtend(): string {
        return this.translateService.instant('Extend string');
    }
}

run the scanner npx ngx-translate-extract --input src/base-extend.ts --format pot --output test.txt

result:

Enabled parsers:
- PipeParser
- DirectiveParser
- ServiceParser
- MarkerParser

Enabled post processors:
(none)

Compiler:
- PoCompiler

Extracting:
- src/base-extend.ts
translateService

Found 0 strings.

Saving:
- test.txt [MERGED]

Done.

Change base-extend.ts by removing the as alias

import {Injectable} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {Base} from 'src/base';

@Injectable({providedIn: 'root'})
export class BaseExtend extends Base {
    constructor(
        translateService: TranslateService
    ) {
        super(translateService);
    }

    public getExtend(): string {
        return this.translateService.instant('Extend string');
    }
}

Run the scanner again

npx ngx-translate-extract --input src/base-extend.ts --format pot --output test.txt

Result:

Enabled parsers:
- PipeParser
- DirectiveParser
- ServiceParser
- MarkerParser

Enabled post processors:
(none)

Compiler:
- PoCompiler

Extracting:
- src/base-extend.ts
translateService

Found 1 strings.

Saving:
- test.txt [MERGED]

Done.
chriscodeweb commented 5 days ago

Nice find! 👍 I hope they fix it!

pmpak commented 3 days ago

That's a very interesting corner case. I'll try to have a look