Add visitor settings, rework logged-in dropdown (#2514)
[oweals/peertube.git] / client / src / app / menu / language-chooser.component.ts
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'
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 }[] = []
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] }))
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     if (locale) return I18N_LOCALES[locale] || english
40     return english
41   }
42 }