-
-
Notifications
You must be signed in to change notification settings - Fork 591
Open
Description
Current behavior
When using CustomLoader class using new keyword from the documentation:
provideTranslateService({
loader: provideTranslateLoader(() => new MyCustomLoader())
})Typescript will throw:
Argument of type '() => MyCustomLoader' is not assignable to parameter of type 'Type<TranslateLoader>'.
Type '() => MyCustomLoader' provides no match for the signature 'new (...args: any[]): TranslateLoader'.ts(2345)Expected behavior
Either allowing new keyword or using factory instead of class.
How do you think that we should fix this?
Not sure what would be the best approach.
Minimal reproduction of the problem with instructions
// app.config.ts
provideTranslationFiles({ fileDirectory: ['somedir/i18n'] })// translation-files.provider.ts
export const provideTranslationFiles = (config?: Config) => {
const fallbackLang = config?.language || cfg.language;
const fileDirectory = config?.fileDirectory || cfg.fileDirectory;
return provideTranslateService({
fallbackLang,
loader: provideTranslateLoader(() => new TranslationFilesHttpLoader(fileDirectory)),
});
};// translation-files-http-loader.ts
export class TranslationFilesHttpLoader extends TranslateLoader {
private readonly handler = inject(HttpBackend);
constructor(private readonly fileDirectory: string | string[]) {
super();
}
override getTranslation(language: Language): Observable<TranslationObject> {
const dirs = Array.isArray(this.fileDirectory) ? this.fileDirectory : [this.fileDirectory];
const src$ = dirs.map((dir) => {
return new HttpClient(this.handler)
.get(`${dir}/${language}.json`)
.pipe(catchError(() => of({}))); // handle missing file
});
return forkJoin(src$).pipe(
map((res) => res.reduce((acc, cur) => mergeDeep(acc, cur)) as TranslationObject),
);
}
}Environment
ngx-translate version: 17.0.0
Angular version: 20.3.15
Browser:
- [ ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: 24.11.1
- Platform: Mac
Others:
Metadata
Metadata
Assignees
Labels
No labels