mirror of
https://github.com/Spythere/genera-tor.git
synced 2026-05-03 05:28:13 +00:00
chore: added loading locale from browser data
This commit is contained in:
+46
-6
@@ -22,6 +22,7 @@ import { defineComponent } from 'vue';
|
|||||||
import packageInfo from '../package.json';
|
import packageInfo from '../package.json';
|
||||||
import { useStore } from './store/store';
|
import { useStore } from './store/store';
|
||||||
import orderStorageMixin from './mixins/orderStorageMixin';
|
import orderStorageMixin from './mixins/orderStorageMixin';
|
||||||
|
import StorageManager from './managers/storageManager';
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
mixins: [orderStorageMixin],
|
mixins: [orderStorageMixin],
|
||||||
@@ -31,20 +32,59 @@ export default defineComponent({
|
|||||||
|
|
||||||
return { offlineReady, needRefresh, updateServiceWorker };
|
return { offlineReady, needRefresh, updateServiceWorker };
|
||||||
},
|
},
|
||||||
|
|
||||||
data() {
|
data() {
|
||||||
return { appVersion: packageInfo.version, store: useStore() };
|
return { appVersion: packageInfo.version, store: useStore() };
|
||||||
},
|
},
|
||||||
|
|
||||||
created() {
|
created() {
|
||||||
document.title = `GeneraTOR ${this.appVersion}`;
|
this.init();
|
||||||
this.store.orderDarkMode = this.getOrderSetting('dark-mode') === 'true';
|
},
|
||||||
|
|
||||||
const query = new URLSearchParams(window.location.search);
|
methods: {
|
||||||
|
init() {
|
||||||
|
this.loadLang();
|
||||||
|
this.loadSettings();
|
||||||
|
this.handleQueries();
|
||||||
|
},
|
||||||
|
|
||||||
const id = query.get('sceneryId');
|
loadSettings() {
|
||||||
|
document.title = `GeneraTOR ${this.appVersion}`;
|
||||||
|
this.store.orderDarkMode = this.getOrderSetting('dark-mode') === 'true';
|
||||||
|
},
|
||||||
|
|
||||||
if (id != null) {
|
handleQueries() {
|
||||||
this.store.orderMode = 'OrderTrainPicker';
|
const query = new URLSearchParams(window.location.search);
|
||||||
|
|
||||||
|
const id = query.get('sceneryId');
|
||||||
|
|
||||||
|
if (id != null) {
|
||||||
|
this.store.orderMode = 'OrderTrainPicker';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
changeLang(lang: string) {
|
||||||
|
this.$i18n.locale = lang;
|
||||||
|
this.store.currentAppLocale = lang;
|
||||||
|
|
||||||
|
StorageManager.setStringValue('lang', lang);
|
||||||
|
},
|
||||||
|
|
||||||
|
loadLang() {
|
||||||
|
const storageLang = StorageManager.getStringValue('lang');
|
||||||
|
|
||||||
|
if (storageLang) {
|
||||||
|
this.changeLang(storageLang);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!window.navigator.language) return;
|
||||||
|
|
||||||
|
const naviLanguage = window.navigator.language.toString();
|
||||||
|
|
||||||
|
if (!naviLanguage.startsWith('pl')) {
|
||||||
|
this.changeLang('en');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
export default class StorageManager {
|
||||||
|
static registerStorage(name: string) {
|
||||||
|
window.localStorage.setItem(name, '1');
|
||||||
|
}
|
||||||
|
|
||||||
|
static unregisterStorage(name: string) {
|
||||||
|
window.localStorage.removeItem(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
static isRegistered(name: string) {
|
||||||
|
return window.localStorage.getItem(name) ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static setBooleanValue(key: string, val: boolean) {
|
||||||
|
window.localStorage.setItem(key, val.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
static setNumericValue(key: string, val: number) {
|
||||||
|
window.localStorage.setItem(key, val.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
static setStringValue(key: string, val: string) {
|
||||||
|
window.localStorage.setItem(key, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
static setValue(key: string, val: any) {
|
||||||
|
if (typeof val == 'boolean') this.setBooleanValue(key, val);
|
||||||
|
else if (typeof val == 'number') this.setNumericValue(key, val);
|
||||||
|
else if (typeof val == 'string') this.setStringValue(key, val);
|
||||||
|
else this.setStringValue(key, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
static removeValue(key: string) {
|
||||||
|
window.localStorage.removeItem(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
static getValue(key: string) {
|
||||||
|
return window.localStorage.getItem(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
static getBooleanValue(key: string): boolean {
|
||||||
|
return window.localStorage.getItem(key) === 'true' ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static getStringValue(key: string): string {
|
||||||
|
return window.localStorage.getItem(key) || '';
|
||||||
|
}
|
||||||
|
|
||||||
|
static getNumericValue(key: string): number {
|
||||||
|
const itemValue = window.localStorage.getItem(key);
|
||||||
|
return itemValue ? parseInt(itemValue) : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,8 @@ import {
|
|||||||
export const useStore = defineStore('store', {
|
export const useStore = defineStore('store', {
|
||||||
state: () => {
|
state: () => {
|
||||||
return {
|
return {
|
||||||
|
currentAppLocale: 'pl',
|
||||||
|
|
||||||
helperModalOpen: false,
|
helperModalOpen: false,
|
||||||
orderDarkMode: false,
|
orderDarkMode: false,
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user