]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Component, Input, OnInit } from '@angular/core';\r |
2 | \r | |
eafe8130 | 3 | import * as _ from 'lodash';\r |
81eedcae TL |
4 | import { defineLocale } from 'ngx-bootstrap/chronos';\r |
5 | import { BsLocaleService } from 'ngx-bootstrap/datepicker';\r | |
6 | \r | |
eafe8130 | 7 | import { LanguageService } from '../../services/language.service';\r |
81eedcae | 8 | import { languageBootstrapMapping, SupportedLanguages } from './supported-languages.enum';\r |
11fdf7f2 TL |
9 | \r |
10 | @Component({\r | |
11 | selector: 'cd-language-selector',\r | |
12 | templateUrl: './language-selector.component.html',\r | |
13 | styleUrls: ['./language-selector.component.scss']\r | |
14 | })\r | |
15 | export class LanguageSelectorComponent implements OnInit {\r | |
16 | @Input()\r | |
17 | isDropdown = true;\r | |
18 | \r | |
9f95a23c | 19 | supportedLanguages: Record<string, any> = SupportedLanguages;\r |
11fdf7f2 TL |
20 | selectedLanguage: string;\r |
21 | \r | |
eafe8130 | 22 | constructor(private localeService: BsLocaleService, private languageService: LanguageService) {}\r |
81eedcae | 23 | \r |
11fdf7f2 | 24 | ngOnInit() {\r |
eafe8130 | 25 | this.selectedLanguage = this.languageService.getLocale();\r |
81eedcae | 26 | this.defineUsedLanguage();\r |
eafe8130 TL |
27 | \r |
28 | this.languageService.getLanguages().subscribe((langs) => {\r | |
29 | this.supportedLanguages = _.pick(this.supportedLanguages, langs) as Object;\r | |
30 | });\r | |
81eedcae TL |
31 | }\r |
32 | \r | |
33 | /**\r | |
34 | * Sets ngx-bootstrap local based on the current language selection\r | |
35 | *\r | |
36 | * ngx-bootstrap locals documentation:\r | |
37 | * https://valor-software.com/ngx-bootstrap/#/datepicker#locales\r | |
38 | */\r | |
39 | private defineUsedLanguage() {\r | |
40 | const lang = this.selectedLanguage.slice(0, 2);\r | |
41 | if (lang in languageBootstrapMapping) {\r | |
42 | defineLocale(lang, languageBootstrapMapping[lang]);\r | |
43 | this.localeService.use(lang);\r | |
44 | }\r | |
11fdf7f2 TL |
45 | }\r |
46 | \r | |
eafe8130 TL |
47 | /**\r |
48 | * Jest is being more restricted regarding spying on the reload method.\r | |
49 | * This will allow us to spyOn this method instead.\r | |
50 | */\r | |
51 | reloadWindow() {\r | |
11fdf7f2 TL |
52 | window.location.reload();\r |
53 | }\r | |
eafe8130 TL |
54 | \r |
55 | changeLanguage(lang: string) {\r | |
56 | this.languageService.setLocale(lang);\r | |
57 | this.reloadWindow();\r | |
58 | }\r | |
11fdf7f2 | 59 | }\r |