]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Only show available languages
authorTiago Melo <tmelo@suse.com>
Wed, 2 Oct 2019 11:06:23 +0000 (11:06 +0000)
committerRicardo Dias <rdias@suse.com>
Thu, 10 Oct 2019 15:03:32 +0000 (16:03 +0100)
Read locale from LOCALE_ID, this is defined during build.

Signed-off-by: Tiago Melo <tmelo@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/locale.helper.ts [deleted file]
src/pybind/mgr/dashboard/frontend/src/app/shared/components/language-selector/language-selector.component.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/language.service.spec.ts [new file with mode: 0644]
src/pybind/mgr/dashboard/frontend/src/app/shared/services/language.service.ts [new file with mode: 0644]

diff --git a/src/pybind/mgr/dashboard/frontend/src/app/locale.helper.ts b/src/pybind/mgr/dashboard/frontend/src/app/locale.helper.ts
deleted file mode 100644 (file)
index b2c02e8..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-export class LocaleHelper {
-  static getBrowserLang(): string {
-    const lang = navigator.language;
-
-    if (lang.includes('cs')) {
-      return 'cs';
-    } else if (lang.includes('de')) {
-      return 'de-DE';
-    } else if (lang.includes('en')) {
-      return 'en-US';
-    } else if (lang.includes('es')) {
-      return 'es-ES';
-    } else if (lang.includes('fr')) {
-      return 'fr-FR';
-    } else if (lang.includes('id')) {
-      return 'id-ID';
-    } else if (lang.includes('it')) {
-      return 'it-IT';
-    } else if (lang.includes('ja')) {
-      return 'ja-JP';
-    } else if (lang.includes('pl')) {
-      return 'pl-PL';
-    } else if (lang.includes('pt')) {
-      return 'pt-BR';
-    } else if (lang.includes('zh-TW')) {
-      return 'zh-TW';
-    } else if (lang.includes('zh')) {
-      return 'zh-CN';
-    } else {
-      return undefined;
-    }
-  }
-
-  static getLocale(): string {
-    return window.localStorage.getItem('lang') || this.getBrowserLang() || 'en-US';
-  }
-
-  static setLocale(lang: string) {
-    document.cookie = `cd-lang=${lang}`;
-  }
-}
-
index 8262eff99fc67340235ccd808490f82245ccd0ff..032ae4207a66898f73208d64e1043bb9412ed992 100644 (file)
@@ -1,9 +1,10 @@
 import { Component, Input, OnInit } from '@angular/core';\r
 \r
+import * as _ from 'lodash';\r
 import { defineLocale } from 'ngx-bootstrap/chronos';\r
 import { BsLocaleService } from 'ngx-bootstrap/datepicker';\r
 \r
-import { LocaleHelper } from '../../../locale.helper';\r
+import { LanguageService } from '../../services/language.service';\r
 import { languageBootstrapMapping, SupportedLanguages } from './supported-languages.enum';\r
 \r
 @Component({\r
@@ -15,14 +16,18 @@ export class LanguageSelectorComponent implements OnInit {
   @Input()\r
   isDropdown = true;\r
 \r
-  supportedLanguages = SupportedLanguages;\r
+  supportedLanguages: Object = SupportedLanguages;\r
   selectedLanguage: string;\r
 \r
-  constructor(private localeService: BsLocaleService) {}\r
+  constructor(private localeService: BsLocaleService, private languageService: LanguageService) {}\r
 \r
   ngOnInit() {\r
-    this.selectedLanguage = LocaleHelper.getLocale();\r
+    this.selectedLanguage = this.languageService.getLocale();\r
     this.defineUsedLanguage();\r
+\r
+    this.languageService.getLanguages().subscribe((langs) => {\r
+      this.supportedLanguages = _.pick(this.supportedLanguages, langs) as Object;\r
+    });\r
   }\r
 \r
   /**\r
@@ -48,7 +53,7 @@ export class LanguageSelectorComponent implements OnInit {
   }\r
 \r
   changeLanguage(lang: string) {\r
-    LocaleHelper.setLocale(lang);\r
+    this.languageService.setLocale(lang);\r
     this.reloadWindow();\r
   }\r
 }\r
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/language.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/language.service.spec.ts
new file mode 100644 (file)
index 0000000..5c4ad82
--- /dev/null
@@ -0,0 +1,34 @@
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { TestBed } from '@angular/core/testing';
+
+import { configureTestBed } from '../../../testing/unit-test-helper';
+import { LanguageService } from './language.service';
+
+describe('LanguageService', () => {
+  let service: LanguageService;
+  let httpTesting: HttpTestingController;
+
+  configureTestBed({
+    providers: [LanguageService],
+    imports: [HttpClientTestingModule]
+  });
+
+  beforeEach(() => {
+    service = TestBed.get(LanguageService);
+    httpTesting = TestBed.get(HttpTestingController);
+  });
+
+  afterEach(() => {
+    httpTesting.verify();
+  });
+
+  it('should be created', () => {
+    expect(service).toBeTruthy();
+  });
+
+  it('should call create', () => {
+    service.getLanguages().subscribe();
+    const req = httpTesting.expectOne('ui-api/langs');
+    expect(req.request.method).toBe('GET');
+  });
+});
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/language.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/language.service.ts
new file mode 100644 (file)
index 0000000..80b602a
--- /dev/null
@@ -0,0 +1,22 @@
+import { HttpClient } from '@angular/common/http';
+import { Inject, LOCALE_ID } from '@angular/core';
+import { Injectable } from '@angular/core';
+
+@Injectable({
+  providedIn: 'root'
+})
+export class LanguageService {
+  constructor(private http: HttpClient, @Inject(LOCALE_ID) protected localeId: string) {}
+
+  getLocale(): string {
+    return this.localeId || 'en-US';
+  }
+
+  setLocale(lang: string) {
+    document.cookie = `cd-lang=${lang}`;
+  }
+
+  getLanguages() {
+    return this.http.get<string[]>('ui-api/langs');
+  }
+}