1 import { Component, ElementRef, ViewChild, Inject, LOCALE_ID } from '@angular/core'
2 import { I18N_LOCALES } from '../../../../shared'
3 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
4 import { sortBy } from '@app/shared/misc/utils'
5 import { getCompleteLocale } from '@shared/models/i18n'
6 import { isOnDevLocale, getDevLocale } from '@app/shared/i18n/i18n-utils'
9 selector: 'my-language-chooser',
10 templateUrl: './language-chooser.component.html',
11 styleUrls: [ './language-chooser.component.scss' ]
13 export class LanguageChooserComponent {
14 @ViewChild('modal', { static: true }) modal: ElementRef
16 languages: { id: string, label: string }[] = []
19 private modalService: NgbModal,
20 @Inject(LOCALE_ID) private localeId: string
22 const l = Object.keys(I18N_LOCALES)
23 .map(k => ({ id: k, label: I18N_LOCALES[k] }))
25 this.languages = sortBy(l, 'label')
29 this.modalService.open(this.modal, { centered: true })
32 buildLanguageLink (lang: { id: string }) {
33 return window.location.origin + '/' + lang.id
36 getCurrentLanguage () {
37 const english = 'English'
38 const locale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
40 if (locale) return I18N_LOCALES[locale] || english