Skip to content

Commit 6658f64

Browse files
authored
refactor: move language manager to utils (#2735)
1 parent d6f9f3f commit 6658f64

File tree

5 files changed

+111
-111
lines changed

5 files changed

+111
-111
lines changed

web/assets/js/langs.js

-103
This file was deleted.

web/assets/js/util/index.js

+104
Original file line numberDiff line numberDiff line change
@@ -669,4 +669,108 @@ class URLBuilder {
669669

670670
return `${protocol}//${host}${port}${base}${path}`;
671671
}
672+
}
673+
674+
class LanguageManager {
675+
static supportedLanguages = [
676+
{
677+
name: "English",
678+
value: "en-US",
679+
icon: "🇺🇸",
680+
},
681+
{
682+
name: "فارسی",
683+
value: "fa-IR",
684+
icon: "🇮🇷",
685+
},
686+
{
687+
name: "简体中文",
688+
value: "zh-CN",
689+
icon: "🇨🇳",
690+
},
691+
{
692+
name: "繁體中文",
693+
value: "zh-TW",
694+
icon: "🇹🇼",
695+
},
696+
{
697+
name: "日本語",
698+
value: "ja-JP",
699+
icon: "🇯🇵",
700+
},
701+
{
702+
name: "Русский",
703+
value: "ru-RU",
704+
icon: "🇷🇺",
705+
},
706+
{
707+
name: "Tiếng Việt",
708+
value: "vi-VN",
709+
icon: "🇻🇳",
710+
},
711+
{
712+
name: "Español",
713+
value: "es-ES",
714+
icon: "🇪🇸",
715+
},
716+
{
717+
name: "Indonesian",
718+
value: "id-ID",
719+
icon: "🇮🇩",
720+
},
721+
{
722+
name: "Український",
723+
value: "uk-UA",
724+
icon: "🇺🇦",
725+
},
726+
{
727+
name: "Türkçe",
728+
value: "tr-TR",
729+
icon: "🇹🇷",
730+
},
731+
{
732+
name: "Português",
733+
value: "pt-BR",
734+
icon: "🇧🇷",
735+
}
736+
]
737+
738+
static getLanguage() {
739+
let lang = CookieManager.getCookie("lang");
740+
741+
if (!lang) {
742+
if (window.navigator) {
743+
lang = window.navigator.language || window.navigator.userLanguage;
744+
745+
if (LanguageManager.isSupportLanguage(lang)) {
746+
CookieManager.setCookie("lang", lang, 150);
747+
} else {
748+
CookieManager.setCookie("lang", "en-US", 150);
749+
window.location.reload();
750+
}
751+
} else {
752+
CookieManager.setCookie("lang", "en-US", 150);
753+
window.location.reload();
754+
}
755+
}
756+
757+
return lang;
758+
}
759+
760+
static setLanguage(language) {
761+
if (!LanguageManager.isSupportLanguage(language)) {
762+
language = "en-US";
763+
}
764+
765+
CookieManager.setCookie("lang", language, 150);
766+
window.location.reload();
767+
}
768+
769+
static isSupportLanguage(language) {
770+
const languageFilter = LanguageManager.supportedLanguages.filter((lang) => {
771+
return lang.value === language
772+
})
773+
774+
return languageFilter.length > 0;
775+
}
672776
}

web/html/common/js.html

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
<script src="{{ .base_path }}assets/js/axios-init.js?{{ .cur_ver }}"></script>
88
<script src="{{ .base_path }}assets/js/util/date-util.js?{{ .cur_ver }}"></script>
99
<script src="{{ .base_path }}assets/js/util/index.js?{{ .cur_ver }}"></script>
10-
<script src="{{ .base_path }}assets/js/langs.js"></script>
1110
<script>
1211
const basePath = '{{ .base_path }}';
1312
axios.defaults.baseURL = basePath;

web/html/login.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,9 @@ <h1 class="title headline zoom">
449449
<a-row justify="center" class="centered">
450450
<a-col :span="24">
451451
<a-select ref="selectLang" v-model="lang"
452-
@change="setLang(lang)" style="width: 200px;"
452+
@change="LanguageManager.setLanguage(lang)" style="width: 200px;"
453453
:dropdown-class-name="themeSwitcher.currentTheme">
454-
<a-select-option :value="l.value" label="English" v-for="l in supportLangs">
454+
<a-select-option :value="l.value" label="English" v-for="l in LanguageManager.supportedLanguages">
455455
<span role="img" aria-label="l.name" v-text="l.icon"></span>
456456
&nbsp;&nbsp;<span v-text="l.name"></span>
457457
</a-select-option>
@@ -493,7 +493,7 @@ <h1 class="title headline zoom">
493493
lang: ""
494494
},
495495
async created() {
496-
this.lang = getLang();
496+
this.lang = LanguageManager.getLanguage();
497497
this.secretEnable = await this.getSecretStatus();
498498
},
499499
methods: {

web/html/xui/settings.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@
179179
<template #control>
180180
<a-select ref="selectLang"
181181
v-model="lang"
182-
@change="setLang(lang)"
182+
@change="LanguageManager.setLanguage(lang)"
183183
:dropdown-class-name="themeSwitcher.currentTheme"
184184
style="width: 100%">
185-
<a-select-option :value="l.value" :label="l.value" v-for="l in supportLangs">
185+
<a-select-option :value="l.value" :label="l.value" v-for="l in LanguageManager.supportedLanguages">
186186
<span role="img" :aria-label="l.name" v-text="l.icon"></span> &nbsp;&nbsp; <span v-text="l.name"></span>
187187
</a-select-option>
188188
</a-select>
@@ -344,7 +344,7 @@
344344
<template #title>{{ i18n "pages.settings.telegramBotLanguage"}}</template>
345345
<template #control>
346346
<a-select ref="selectBotLang" v-model="allSetting.tgLang" :dropdown-class-name="themeSwitcher.currentTheme" style="width: 100%">
347-
<a-select-option :value="l.value" :label="l.value" v-for="l in supportLangs">
347+
<a-select-option :value="l.value" :label="l.value" v-for="l in LanguageManager.supportedLanguages">
348348
<span role="img" :aria-label="l.name" v-text="l.icon"></span> &nbsp;&nbsp; <span v-text="l.name"></span>
349349
</a-select-option>
350350
</a-select>
@@ -672,7 +672,7 @@
672672
allSetting: new AllSetting(),
673673
saveBtnDisable: true,
674674
user: {},
675-
lang: getLang(),
675+
lang: LanguageManager.getLanguage(),
676676
remarkModels: { i: 'Inbound', e: 'Email', o: 'Other' },
677677
remarkSeparators: [' ', '-', '_', '@', ':', '~', '|', ',', '.', '/'],
678678
datepickerList: [{ name: 'Gregorian (Standard)', value: 'gregorian' }, { name: 'Jalalian (شمسی)', value: 'jalalian' }],

0 commit comments

Comments
 (0)