1 import { Component, ElementRef, Inject, LOCALE_ID, ViewChild } from '@angular/core'
2 import { getDevLocale, isOnDevLocale, sortBy } from '@app/helpers'
3 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
4 import { getCompleteLocale, getShortLocale, I18N_LOCALES } from '@shared/core-utils/i18n'
7 selector: 'my-language-chooser',
8 templateUrl: './language-chooser.component.html',
9 styleUrls: [ './language-chooser.component.scss' ]
11 export class LanguageChooserComponent {
12 @ViewChild('modal', { static: true }) modal: ElementRef
14 languages: { id: string, label: string, iso: string }[] = []
17 private modalService: NgbModal,
18 @Inject(LOCALE_ID) private localeId: string
20 const l = Object.keys(I18N_LOCALES)
21 .map(k => ({ id: k, label: I18N_LOCALES[k], iso: getShortLocale(k) }))
23 this.languages = sortBy(l, 'label')
27 this.modalService.open(this.modal, { centered: true })
30 buildLanguageLink (lang: { id: string }) {
31 return window.location.origin + '/' + lang.id
34 getCurrentLanguage () {
35 const english = 'English'
36 const locale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
38 if (locale) return I18N_LOCALES[locale] || english