0

How to set the scope when using : this.translocoService.translate('object.test'); ?

My translation files are located in a folder like "MyFolder", so the scope will be MyFolder.

This is the structure of my *.json files:

{
    "demo": "test123",
    
    "object" : {        
        "test" : "My.Test" 
    }
}

This is what I like to do:

export class AppComponent {

constructor(private translocoService: TranslocoService,@Inject(TRANSLOCO_SCOPE) private scope) {}

    ngOnInit() {

    this.translocoService.translate('object.test'); //How to set the scope here?

    }    
}
Christoph1972
  • 786
  • 3
  • 10
  • 21

1 Answers1

0

Solution:

export class AppComponent {

constructor(private translocoService: TranslocoService,@Inject(TRANSLOCO_SCOPE) private scope) {}

    ngOnInit() {

    var testLable = this.translocoService.translate(this.scope + '.object.test');

    }    
}
Christoph1972
  • 786
  • 3
  • 10
  • 21
  • The right way to do this is translate("object.test",{},this.scope), but it won't work until the translations file has loaded, so you'd need to listen for the transloco load success event first - https://ngneat.github.io/transloco/docs/translation-api#events – chrismarx Oct 05 '21 at 20:51