ngx-translate / http-loader

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

Angular 10 and IE11 support #83

Closed ricardobarata closed 3 years ago

ricardobarata commented 3 years ago

Current behavior

The translation is not loaded image

Expected behavior

For the translation file to be loaded

Minimal reproduction of the problem with instructions

Build a page with angular 10 using the default @ngx-translate/core and @ngx-translate/http-loader configuration shown on github

Here is my configuration:

import { HttpClient, HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CoreModule } from './core/core.module';
import { HttpInterceptorModule } from './http.interceptor';

export function HttpLoaderFactory(http: HttpClient): TranslateLoader {
    return new TranslateHttpLoader(http, 'assets/i18n/', '.json');
}

@NgModule({
    declarations: [AppComponent],
    imports: [
        BrowserModule,
        AppRoutingModule,
        BrowserAnimationsModule,
        HttpClientModule,
        HttpInterceptorModule,
        CoreModule,
        TranslateModule.forRoot({
            defaultLanguage: 'en-GB',
            loader: {
                provide: TranslateLoader,
                useFactory: HttpLoaderFactory,
                deps: [HttpClient],
            },
        }),
    ],
    bootstrap: [AppComponent],
})
export class AppModule { }
import { DOCUMENT } from '@angular/common';
import { Component, Inject } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
    selector: 'app-root',
    template: '<router-outlet></router-outlet>',
    styleUrls: ['./app.component.scss'],
})
export class AppComponent {
    private supportedLanguages: { [key: string]: { [direction: string]: string } } = {
        'en-GB': { direction: 'ltr' },
        'en-US': { direction: 'ltr' },
        // es: { direction: 'ltr' },
        // he: { direction: 'rtl' }, -> showcase that this is possible
    };

    constructor(private translate: TranslateService, @Inject(DOCUMENT) private document: Document) {
        // Iterate through the browser language preferences in order until we find one that we support
        if (navigator.languages) {
            for (const lang of navigator.languages) {
                if (this.supportedLanguages[lang]) {
                    translate.use(lang);
                    document.documentElement.lang = lang;
                    if (this.supportedLanguages[lang].direction === 'rtl') {
                        document.body.dir = 'rtl';
                    } else {
                        document.body.dir = 'ltr';
                    }
                    break;
                }
            }
        }
    }
}

Environment

@ngx-translate/core": "13.0.0" @ngx-translate/http-loader": "6.0.0" Angular version: 10.1.4 tsconfig.json has "target": "es5" and my .browserslistrc has IE 11

Browser:

ricardobarata commented 3 years ago

After some more digging it seems that this is fine:

{
  "HELLO": "hello"
}

however this is not:

{
  "HELLO": { "TEST": "hello" }
}

At this point I think the problem might be in ngx-translate/core rather than ngx-translate/http-loader so I'm going to close the issue