Search code examples
angularangular6angularfire2ngx-translate

How can I translate text input in AngularFire2 Firestore use ngx-translate?


<div class="row"><h6>{{item.ageList}}</h6></div>
<div class="row"><h6>{{item.amenities}}</h6></div>

Note: I wanna translate data after getting of Firestore such as

<div class="row"><h6>{{'item.ageList' | translate}}</h6></div>
<div class="row"><h6>{{'item.amenities' | translate}}</h6></div>

Firestore object image


Solution

  • In ngx-translate's repo you do have Firebase's example.
    https://github.com/ngx-translate/core/blob/master/FIREBASE_EXAMPLE.md

    EDIT. After put some work I can give you working example.
    1. Put your firebase's config into envrionment.ts

    export const environment = {
      production: false,
      firebase: {
        apiKey: '',
        authDomain: '',
        databaseURL: '',
        projectId: '',
        storageBucket: '',
        messagingSenderId: ''
      },
    };
    

    You can get it from Firebase Console of your project.
    2. Create firestore-trans-loader.ts in shared directory, next to app directory

    import { TranslateLoader } from '@ngx-translate/core';
    import { AngularFirestore } from '@angular/fire/firestore';
    import { Observable } from 'rxjs';
    
    export class FirestoreTransLoader implements TranslateLoader {
    
      constructor(private db: AngularFirestore) {
      }
    
      getTranslation(lang: string, prefix: string = 'translates'): Observable<any> {
        return this.db.doc(`${prefix}/${lang}`).valueChanges();
      }
    
    }
    

    3. Prepare your app.module.ts .

    export function FirestoreTranslationsLoaderFactory(db: AngularFirestore) {
      return new FirestoreTransLoader(db);
    }
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        AngularFireModule.initializeApp(environment.firebase),
        AngularFirestoreModule,
        TranslateModule.forRoot({
          loader: {provide: TranslateLoader, useFactory: FirestoreTranslationsLoaderFactory, deps: [AngularFirestore]}
        })
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    4. And finally use it in your component

    import { Component } from '@angular/core';
    import { TranslateService } from '@ngx-translate/core';
    
    @Component({
      selector: 'app-root',
      template: `
        <p>{{ helloKey | translate }}</p>
        <select (change)="onLangChange($event)">
          <option value="en">English</option>
          <option value="pl">Polish</option>
        </select>
    
      `,
    })
    export class AppComponent {
    
      helloKey = 'HELLO';
      constructor(private translate: TranslateService) {
        translate.setDefaultLang('en');
        translate.use('en');
      }
    
      onLangChange(event) {
        this.translate.use(event.target.value);
      }
    
    }
    

    Here you do have complete repo without config where you can find out details (if I forgot about something).
    https://github.com/Mr-Eluzive/angularfire-ngx-translate-example Also I have put some work into this so maybe I will try to push it into ngx-translate repo.