rbalet / ngx-translate-multi-http-loader

A loader for ngx-translate that loads translations with http calls
MIT License
77 stars 15 forks source link

HttpBackend mistakes in angular 18+ #34

Closed Rober9614 closed 3 months ago

Rober9614 commented 3 months ago

According to the new version of Angular, the framework no longer uses the "assets" folder and instead uses the "public" folder. However, when trying to use the "i18n" folder within "public," the library fails to locate the files according to the specified path.

Solution: Move to "HttpClient" dependency.

Example:

`export class MultiTranslateHttpLoader implements TranslateLoader {

constructor(private _handler: HttpClient, private _resourcesPrefix: string[] | ITranslationResource[]) { }

public getTranslation(lang: string): Observable<any> {

    const requests: Observable<Object | {}>[] = this._resourcesPrefix.map((resource) => {

        let path: string = resource.prefix ? `${resource.prefix}${lang}${resource.suffix || '.json'}` : `${resource}${lang}.json`;

        return this._handler.get(path).pipe(
            catchError((err) => {

                if (!resource.optional) {
                    throw new Error(`Something went wrong for the following translation file: ${err}`);
                }

                return of({});
            }),
        );

    });

    return forkJoin(requests).pipe(map((response) => deepmerge(...response)));

}

}`

rbalet commented 3 months ago

Hi @Rober9614.

Thx for the bug report. Could you provide a stackblitz ?

Because this is actually working on my projects.

I'd prefer avoid using HttpClient as this would create other errors :/

rbalet commented 3 months ago

Hi @Rober9614.

I made a new project with the public logic, instead of the /assets one and it worked perfectly.

The only way it may not work (in my opinion), is if you had let the /assets and therefore have a wrong path.

I've updated the README to make it clearer

return new MultiTranslateHttpLoader(_httpBackend, ['/assets/i18n/core/', '/assets/i18n/vendors/']); // /i18n/core/ on angular >= v18 with the new public logic

I'm closing this issue. Please, feel free to reopen it if I'm mistaking and provide more information.

Cheers