]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/locale.helper.ts
buildsys: auto-determine current version for makefile
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / locale.helper.ts
1 import locale_cs from '@angular/common/locales/cs';
2 import locale_de from '@angular/common/locales/de';
3 import locale_en from '@angular/common/locales/en';
4 import locale_es from '@angular/common/locales/es';
5 import locale_fr from '@angular/common/locales/fr';
6 import locale_id from '@angular/common/locales/id';
7 import locale_it from '@angular/common/locales/it';
8 import locale_ja from '@angular/common/locales/ja';
9 import locale_ko from '@angular/common/locales/ko';
10 import locale_pl from '@angular/common/locales/pl';
11 import locale_pt from '@angular/common/locales/pt';
12 import locale_zh_Hans from '@angular/common/locales/zh-Hans';
13 import locale_zh_Hant from '@angular/common/locales/zh-Hant';
14 import { LOCALE_ID, TRANSLATIONS, TRANSLATIONS_FORMAT } from '@angular/core';
15
16 declare const require;
17
18 export class LocaleHelper {
19 static getBrowserLang(): string {
20 const lang = navigator.language;
21
22 if (lang.includes('cs')) {
23 return 'cs';
24 } else if (lang.includes('de')) {
25 return 'de-DE';
26 } else if (lang.includes('en')) {
27 return 'en-US';
28 } else if (lang.includes('es')) {
29 return 'es-ES';
30 } else if (lang.includes('fr')) {
31 return 'fr-FR';
32 } else if (lang.includes('id')) {
33 return 'id-ID';
34 } else if (lang.includes('it')) {
35 return 'it-IT';
36 } else if (lang.includes('ja')) {
37 return 'ja-JP';
38 } else if (lang.includes('ko')) {
39 return 'ko-KR';
40 } else if (lang.includes('pl')) {
41 return 'pl-PL';
42 } else if (lang.includes('pt')) {
43 return 'pt-BR';
44 } else if (lang.includes('zh-TW')) {
45 return 'zh-TW';
46 } else if (lang.includes('zh')) {
47 return 'zh-CN';
48 } else {
49 return undefined;
50 }
51 }
52
53 static getLocale(): string {
54 return window.localStorage.getItem('lang') || this.getBrowserLang() || 'en-US';
55 }
56
57 static setLocale(lang: string) {
58 window.localStorage.setItem('lang', lang);
59 }
60
61 static getLocaleData() {
62 let localeData = locale_en;
63 switch (this.getLocale()) {
64 case 'cs':
65 localeData = locale_cs;
66 break;
67 case 'de-DE':
68 localeData = locale_de;
69 break;
70 case 'es-ES':
71 localeData = locale_es;
72 break;
73 case 'fr-FR':
74 localeData = locale_fr;
75 break;
76 case 'id-ID':
77 localeData = locale_id;
78 break;
79 case 'it-IT':
80 localeData = locale_it;
81 break;
82 case 'ja-JP':
83 localeData = locale_ja;
84 break;
85 case 'ko-KR':
86 localeData = locale_ko;
87 break;
88 case 'pl-PL':
89 localeData = locale_pl;
90 break;
91 case 'pt-BR':
92 localeData = locale_pt;
93 break;
94 case 'zh-CN':
95 localeData = locale_zh_Hans;
96 break;
97 case 'zh-TW':
98 localeData = locale_zh_Hant;
99 break;
100 }
101 return localeData;
102 }
103 }
104
105 const i18nProviders = [
106 { provide: LOCALE_ID, useValue: LocaleHelper.getLocale() },
107 {
108 provide: TRANSLATIONS,
109 useFactory: (locale) => {
110 locale = locale || 'en-US';
111 try {
112 return require(`raw-loader!locale/messages.${locale}.xlf`);
113 } catch (error) {
114 return [];
115 }
116 },
117 deps: [LOCALE_ID]
118 },
119 { provide: TRANSLATIONS_FORMAT, useValue: 'xlf' }
120 ];
121
122 export { i18nProviders };