]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, ViewChild, Inject, LOCALE_ID } from '@angular/core' | |
2 | import { I18N_LOCALES, getShortLocale } 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' | |
7 | ||
8 | @Component({ | |
9 | selector: 'my-language-chooser', | |
10 | templateUrl: './language-chooser.component.html', | |
11 | styleUrls: [ './language-chooser.component.scss' ] | |
12 | }) | |
13 | export class LanguageChooserComponent { | |
14 | @ViewChild('modal', { static: true }) modal: ElementRef | |
15 | ||
16 | languages: { id: string, label: string, iso: string }[] = [] | |
17 | ||
18 | constructor ( | |
19 | private modalService: NgbModal, | |
20 | @Inject(LOCALE_ID) private localeId: string | |
21 | ) { | |
22 | const l = Object.keys(I18N_LOCALES) | |
23 | .map(k => ({ id: k, label: I18N_LOCALES[k] , iso: getShortLocale(k)})) | |
24 | ||
25 | this.languages = sortBy(l, 'label') | |
26 | } | |
27 | ||
28 | show () { | |
29 | this.modalService.open(this.modal, { centered: true }) | |
30 | } | |
31 | ||
32 | buildLanguageLink (lang: { id: string }) { | |
33 | return window.location.origin + '/' + lang.id | |
34 | } | |
35 | ||
36 | getCurrentLanguage () { | |
37 | const english = 'English' | |
38 | const locale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) | |
39 | ||
40 | if (locale) return I18N_LOCALES[locale] || english | |
41 | return english | |
42 | } | |
43 | } |