diff --git a/src/dashboard.py b/src/dashboard.py index 2d07475..5e3d1d1 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -2134,20 +2134,50 @@ def API_Welcome_Finish(): DashboardConfig.SetConfig("Other", "welcome_session", False) return ResponseObject() +class Locale: + def __init__(self): + self.localePath = './static/locale/' + self.activeLanguages = {} + with open(os.path.join(f"{self.localePath}active_languages.json"), "r") as f: + self.activeLanguages = json.loads(''.join(f.readlines())) + + + def getLanguage(self) -> dict | None: + currentLanguage = DashboardConfig.GetConfig("Server", "dashboard_language")[1] + if currentLanguage == "en": + return None + if os.path.exists(os.path.join(f"{self.localePath}{currentLanguage}.json")): + with open(os.path.join(f"{self.localePath}{currentLanguage}.json"), "r") as f: + return dict(json.loads(''.join(f.readlines()))) + else: + return None + + def updateLanguage(self, lang_id): + if not os.path.exists(os.path.join(f"{self.localePath}{lang_id}.json")): + DashboardConfig.SetConfig("Server", "dashboard_language", "en") + else: + DashboardConfig.SetConfig("Server", "dashboard_language", lang_id) + + +Locale = Locale() + @app.get(f'{APP_PREFIX}/api/locale') -def API_Local_CurrentLang(): - # if param is None or len(param) == 0: - # with open(os.path.join("./static/locale/active_languages.json"), "r") as f: - # return ResponseObject(data=''.join(f.readlines())) +def API_Locale_CurrentLang(): + return ResponseObject(data=Locale.getLanguage()) + +@app.get(f'{APP_PREFIX}/api/locale/available') +def API_Locale_Available(): + return ResponseObject(data=Locale.activeLanguages) + +@app.post(f'{APP_PREFIX}/api/locale/update') +def API_Locale_Update(): + data = request.get_json() + if 'lang_id' not in data.keys(): + return ResponseObject(False, "Please specify a lang_id") + Locale.updateLanguage(data['lang_id']) + return ResponseObject(data=Locale.getLanguage()) - _, param = DashboardConfig.GetConfig("Server", "dashboard_language") - if param == "en": - return ResponseObject() - - if os.path.exists(os.path.join(f"./static/locale/{param}.json")): - with open(os.path.join(f"./static/locale/{param}.json"), "r") as f: - return ResponseObject(data=''.join(f.readlines())) diff --git a/src/static/app/src/components/settingsComponent/dashboardLanguage.vue b/src/static/app/src/components/settingsComponent/dashboardLanguage.vue new file mode 100644 index 0000000..ce7aff3 --- /dev/null +++ b/src/static/app/src/components/settingsComponent/dashboardLanguage.vue @@ -0,0 +1,86 @@ + + + + + + + + + + + + + {{ currentLanguage?.lang_name_localized }} + + + + + + + + {{ x.lang_name_localized }} + + {{ x.lang_name }} + + + + + + + + + + + + \ No newline at end of file diff --git a/src/static/app/src/main.js b/src/static/app/src/main.js index 5fc6407..7731e10 100644 --- a/src/static/app/src/main.js +++ b/src/static/app/src/main.js @@ -14,7 +14,7 @@ import {DashboardConfigurationStore} from "@/stores/DashboardConfigurationStore. import {fetchGet} from "@/utilities/fetch.js"; let Locale; -await fetch("/api/locale").then(res => res.json()).then(res => Locale = JSON.parse(res.data)) +await fetch("/api/locale").then(res => res.json()).then(res => Locale = res.data) const app = createApp(App) @@ -29,5 +29,7 @@ pinia.use(({ store }) => { app.use(pinia) const store = DashboardConfigurationStore() -window.Locale = Locale; +// window.Locale = Locale; + +store.Locale = Locale; app.mount('#app') \ No newline at end of file diff --git a/src/static/app/src/utilities/locale.js b/src/static/app/src/utilities/locale.js index af32804..9feff4b 100644 --- a/src/static/app/src/utilities/locale.js +++ b/src/static/app/src/utilities/locale.js @@ -1,12 +1,16 @@ +import {DashboardConfigurationStore} from "@/stores/DashboardConfigurationStore.js"; + export const GetLocale = (key) => { - if (window.Locale === null) + const store = DashboardConfigurationStore() + + if (store.Locale === null) return key - const reg = Object.keys(window.Locale) + const reg = Object.keys(store.Locale) const match = reg.filter(x => { return key.match(new RegExp('^' + x + '$', 'gi')) !== null }) if (match.length === 0 || match.length > 1){ return key } - return key.replace(new RegExp(match[0], 'gi'), window.Locale[match[0]]) + return key.replace(new RegExp(match[0], 'gi'), store.Locale[match[0]]) } \ No newline at end of file diff --git a/src/static/app/src/views/settings.vue b/src/static/app/src/views/settings.vue index 91695ba..41a7988 100644 --- a/src/static/app/src/views/settings.vue +++ b/src/static/app/src/views/settings.vue @@ -1,5 +1,4 @@
+ +
+ {{ x.lang_name_localized }} + + {{ x.lang_name }} + +