2020-02-28 13:52:21 +01:00
|
|
|
import { Component, ElementRef, ViewChild, Inject, LOCALE_ID } from '@angular/core'
|
2018-06-28 13:59:48 +02:00
|
|
|
import { I18N_LOCALES } from '../../../../shared'
|
2018-08-09 14:55:06 +02:00
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
|
2018-08-27 11:20:06 +02:00
|
|
|
import { sortBy } from '@app/shared/misc/utils'
|
2020-02-28 13:52:21 +01:00
|
|
|
import { getCompleteLocale } from '@shared/models/i18n'
|
|
|
|
import { isOnDevLocale, getDevLocale } from '@app/shared/i18n/i18n-utils'
|
2018-06-28 13:59:48 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-language-chooser',
|
|
|
|
templateUrl: './language-chooser.component.html',
|
|
|
|
styleUrls: [ './language-chooser.component.scss' ]
|
|
|
|
})
|
|
|
|
export class LanguageChooserComponent {
|
2019-07-24 16:05:59 +02:00
|
|
|
@ViewChild('modal', { static: true }) modal: ElementRef
|
2018-06-28 13:59:48 +02:00
|
|
|
|
2018-08-09 14:55:06 +02:00
|
|
|
languages: { id: string, label: string }[] = []
|
2018-06-28 13:59:48 +02:00
|
|
|
|
2020-02-28 13:52:21 +01:00
|
|
|
constructor (
|
|
|
|
private modalService: NgbModal,
|
|
|
|
@Inject(LOCALE_ID) private localeId: string
|
|
|
|
) {
|
2018-08-27 11:20:06 +02:00
|
|
|
const l = Object.keys(I18N_LOCALES)
|
|
|
|
.map(k => ({ id: k, label: I18N_LOCALES[k] }))
|
|
|
|
|
|
|
|
this.languages = sortBy(l, 'label')
|
2018-06-28 13:59:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
show () {
|
2020-02-05 20:54:37 +01:00
|
|
|
this.modalService.open(this.modal, { centered: true })
|
2018-06-28 13:59:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buildLanguageLink (lang: { id: string }) {
|
|
|
|
return window.location.origin + '/' + lang.id
|
|
|
|
}
|
|
|
|
|
2020-02-28 13:52:21 +01:00
|
|
|
getCurrentLanguage () {
|
|
|
|
const english = 'English'
|
|
|
|
const locale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
|
|
|
|
if (locale) return I18N_LOCALES[locale] || english
|
|
|
|
return english
|
|
|
|
}
|
2018-06-28 13:59:48 +02:00
|
|
|
}
|