Created
July 23, 2021 11:55
-
-
Save yigitfindikli/f29130c635038372419adf28203eb747 to your computer and use it in GitHub Desktop.
PrimeNG I18N API with ngx-translate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnDestroy } from '@angular/core'; | |
import { TranslateService } from '@ngx-translate/core'; | |
import { ConfirmationService, PrimeNGConfig } from 'primeng/api'; | |
import { Subscription } from 'rxjs'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'], | |
providers: [ConfirmationService] | |
}) | |
export class AppComponent implements OnDestroy { | |
lang: string = "en"; | |
date: any; | |
subscription: Subscription; | |
constructor(public translate: TranslateService, public primeNGConfig: PrimeNGConfig, private confirmationService: ConfirmationService) { | |
translate.addLangs(['en', 'fr']); | |
translate.setDefaultLang('en'); | |
const browserLang = translate.getBrowserLang(); | |
let lang = browserLang.match(/en|fr/) ? browserLang : 'en'; | |
this.changeLang(lang); | |
this.subscription = this.translate.stream('primeng').subscribe(data => { | |
this.primeNGConfig.setTranslation(data); | |
}); | |
} | |
changeLang(lang: string) { | |
this.translate.use(lang); | |
} | |
ngOnDestroy() { | |
if (this.subscription) { | |
this.subscription.unsubscribe(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment