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