Remove uneccessary details to link titles
[oweals/peertube.git] / client / src / app / menu / language-chooser.component.ts
index 45fa73e762a15dea6a001badc95d1e1a166afd85..c9b775921d18346f8a70c5e8756052806e5b21db 100644 (file)
@@ -1,6 +1,9 @@
-import { Component, ElementRef, ViewChild } from '@angular/core'
-import { I18N_LOCALES } from '../../../../shared'
+import { Component, ElementRef, ViewChild, Inject, LOCALE_ID } from '@angular/core'
+import { I18N_LOCALES, getShortLocale } from '../../../../shared'
 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
+import { sortBy } from '@app/shared/misc/utils'
+import { getCompleteLocale } from '@shared/models/i18n'
+import { isOnDevLocale, getDevLocale } from '@app/shared/i18n/i18n-utils'
 
 @Component({
   selector: 'my-language-chooser',
@@ -8,21 +11,33 @@ import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
   styleUrls: [ './language-chooser.component.scss' ]
 })
 export class LanguageChooserComponent {
-  @ViewChild('modal') modal: ElementRef
+  @ViewChild('modal', { static: true }) modal: ElementRef
 
-  languages: { id: string, label: string }[] = []
+  languages: { id: string, label: string, iso: string }[] = []
 
-  constructor (private modalService: NgbModal) {
-    this.languages = Object.keys(I18N_LOCALES)
-      .map(k => ({ id: k, label: I18N_LOCALES[k] }))
+  constructor (
+    private modalService: NgbModal,
+    @Inject(LOCALE_ID) private localeId: string
+  ) {
+    const l = Object.keys(I18N_LOCALES)
+                    .map(k => ({ id: k, label: I18N_LOCALES[k] , iso: getShortLocale(k) }))
+
+    this.languages = sortBy(l, 'label')
   }
 
   show () {
-    this.modalService.open(this.modal)
+    this.modalService.open(this.modal, { centered: true })
   }
 
   buildLanguageLink (lang: { id: string }) {
     return window.location.origin + '/' + lang.id
   }
 
+  getCurrentLanguage () {
+    const english = 'English'
+    const locale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)
+
+    if (locale) return I18N_LOCALES[locale] || english
+    return english
+  }
 }