this.currentLoader.getTranslation is not a function

See original GitHub issue

I’m trying to familiarize myself with this package. I loaded ng2-translate with the code available in the documentation with some changes (like providers etc.) but I’m still getting an issue,

this.currentLoader.getTranslation is not a function

Here is my code

import { Component, Injectable, provide } from '@angular/core';
import { Http } from '@angular/http';
import { bootstrap } from '@angular/platform-browser-dynamic';
import { TRANSLATE_PROVIDERS, TranslateService, TranslatePipe, TranslateLoader, TranslateStaticLoader } from 'ng2-translate/ng2-translate';
import { HTTP_PROVIDERS } from '@angular/http';

import { TransactionListcomponent } from './transactions/transaction-list.component';

@Component({
    selector: 'my-app',
    template: `
    <div>
        <h1>{{pageTitle}}</h1>
        <h1>Hello, {{name}}!</h1>
        Say "<b>{{ 'HELLO' | translate:'{value: "world"}' }}</b>" to: <input [value]="name" (input)="name = $event.target.value">
        <br/>
        Change language:
        <select (change)="translate.use($event.target.value)">
            <option *ngFor="#lang of ['fr', 'en']" [selected]="lang === translate.currentLang">{{lang}}</option>
        </select>
        <my-transactions></my-transactions>
    </div>
    `,
    directives: [TransactionListcomponent],
    pipes: [TranslatePipe],
    providers: [TranslateService, TranslateLoader]
})

export class AppComponent {
    pageTitle: string = 'Test app';
    name: string = 'World';

    constructor(translate: TranslateService) {
        var userLang = navigator.language.split('-')[0]; // use navigator lang if available
        userLang = /(fr|en)/gi.test(userLang) ? userLang : 'en';

         // this language will be used as a fallback when a translation isn't found in the current language
        translate.setDefaultLang('en');
        console.log(translate)

         // the lang to use, if the lang isn't available, it will use the current loader to get them
        translate.use(userLang);
    }
 }

// Instantiate TranslateService in the bootstrap so that we can keep it as a singleton
 bootstrap(AppComponent, [
    HTTP_PROVIDERS,
    { 
        provide: TranslateLoader,
        useFactory: (http: Http) => new TranslateStaticLoader(http, 'app/assets/i18n', '.json'),
        deps: [Http]
    },
    // use TranslateService here, and not TRANSLATE_PROVIDERS (which will define a default TranslateStaticLoader)
    TranslateService
]);

Does anyone can help me?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8

github_iconTop GitHub Comments

9reactions
oussamaABIDcommented, Sep 18, 2017

user TranslateHttpLoader.

 import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    HttpClientModule,
    TranslateModule.forRoot(), BrowserModule,
//....
  ],
  providers: [{
    provide: TranslateLoader,
    useFactory: (http: HttpClient) => new TranslateHttpLoader(http, '/assets/i18n/', '.json'),
    deps: [HttpClient]
  }],
  bootstrap: [AppComponent]
0reactions
C0ZENcommented, May 30, 2019

This is a very old question nevertheless I had the same problem today after migrating to Angular 8.
This may help someone.

Note: The problem was not coming from the migration to Angular 8.

app.module.ts

imports: [
    TranslateModule.forRoot({
        loader: HttpLoaderMultipleProvider
    }),
]

http-loader-multiple.provider.ts

export const HttpLoaderMultipleProvider = {
    deps: [
        HttpClient
    ],
    multi: true, // This was causing the error
    provide: TranslateLoader,
    useFactory: (HttpLoaderMultipleFactory)
};

I just removed the multi value and the problem was fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: this.currentLoader.getTranslation(...).pipe is not a function
I am using ngx-translate for one of the angular projects. When I try to load translation data from http post request, it is...
Read more >
This.http.get is not a function at TranslateHttpLoader ...
I'm receiving the following error when I try to execute ionic serve: ERROR TypeError: this.http.get is not a function at TranslateHttpLoader ...
Read more >
Getting started with translation in Angular 5 with ngx-translate
SetDefaultLang is used to set a fallback language when a translation isn't found in the current language. use the lang to use, if...
Read more >
ngx-translate/core - UNPKG
currentLoader.getTranslation(lang).pipe(share());\n this.loadingTranslations = loadingTranslations.pipe(\n take(1),\n map((res: Object) => this.compiler.
Read more >
Ngx-Translate: Cannot Read Property 'Currentlang ... - ADocLib
currentLoader.getTranslation is not a function. Here is my code import { Component Injectable provide } from '@angular/core'; import { Http } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found