Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"core-js": "^2.6.5",
"crc-32": "^1.2.0",
"gh-pages": "^2.0.1",
"js-cookie": "^2.2.1",
"mobx": "^5.9.4",
"mobx-react": "^5.4.4",
"pako": "^1.0.11",
Expand Down
2 changes: 2 additions & 0 deletions src/common/lang.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { parseQueryString } from '../common/utils/tools';
import { set as setStorage, get as getStorage } from '../common/utils/storageManager';
import { setCookieLanguage } from '../common/utils/cookieManager';
import { supportedLanguages, translate, init } from './i18n';

export const getLanguage = () => {
const queryLang = parseQueryString().l;
const lang = queryLang in supportedLanguages ? queryLang : getStorage('lang') || 'en';
setStorage('lang', lang);
setCookieLanguage(lang);
return lang;
};

Expand Down
61 changes: 61 additions & 0 deletions src/common/utils/cookieManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const Cookies = require('js-cookie');

export const CookieStorage = function(cookieName, cookieDomain) {
this.initialized = false;
this.cookieName = cookieName;
this.domain = cookieDomain;
this.path = '/';
this.expires = new Date('Thu, 1 Jan 2037 12:00:00 GMT');
this.value = {};
};

CookieStorage.prototype = {
read() {
const cookieValue = Cookies.get(this.cookieName);
try {
this.value = cookieValue ? JSON.parse(cookieValue) : {};
} catch (e) {
this.value = {};
}
this.initialized = true;
},
write(val, expireDate, isSecure, sameSite) {
if (!this.initialized) this.read();
this.value = val;
if (expireDate) this.expires = expireDate;
Cookies.set(this.cookieName, this.value, {
expires : this.expires,
path : this.path,
domain : this.domain,
secure : !!isSecure,
sameSite: sameSite || 'strict',
});
},
get(key) {
if (!this.initialized) this.read();
return this.value[key];
},
set(key, val, options) {
if (!this.initialized) this.read();
this.value[key] = val;
Cookies.set(this.cookieName, this.value, {
expires: new Date(this.expires),
path : this.path,
domain : this.domain,
...options,
});
},
remove() {
Cookies.remove(this.cookieName, {
path : this.path,
domain: this.domain,
});
},
};

export const setCookieLanguage = lang => {
if (!Cookies.get('language') || lang) {
const cookie = new CookieStorage('language');
cookie.write(lang.toUpperCase(), undefined, true, 'none');
}
};