Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit 447bdeb

Browse files
committed
add lang to cookie
1 parent 436da73 commit 447bdeb

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"core-js": "^2.6.5",
112112
"crc-32": "^1.2.0",
113113
"gh-pages": "^2.0.1",
114+
"js-cookie": "^2.2.1",
114115
"mobx": "^5.9.4",
115116
"mobx-react": "^5.4.4",
116117
"pako": "^1.0.11",

src/common/lang.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { parseQueryString } from '../common/utils/tools';
22
import { set as setStorage, get as getStorage } from '../common/utils/storageManager';
3+
import { setCookieLanguage } from '../common/utils/cookieManager';
34
import { supportedLanguages, translate, init } from './i18n';
45

56
export const getLanguage = () => {
67
const queryLang = parseQueryString().l;
78
const lang = queryLang in supportedLanguages ? queryLang : getStorage('lang') || 'en';
89
setStorage('lang', lang);
10+
setCookieLanguage(lang);
911
return lang;
1012
};
1113

src/common/utils/cookieManager.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const Cookies = require('js-cookie');
2+
3+
export const CookieStorage = function(cookieName, cookieDomain) {
4+
this.initialized = false;
5+
this.cookieName = cookieName;
6+
this.domain = cookieDomain;
7+
this.path = '/';
8+
this.expires = new Date('Thu, 1 Jan 2037 12:00:00 GMT');
9+
this.value = {};
10+
};
11+
12+
CookieStorage.prototype = {
13+
read() {
14+
const cookieValue = Cookies.get(this.cookieName);
15+
try {
16+
this.value = cookieValue ? JSON.parse(cookieValue) : {};
17+
} catch (e) {
18+
this.value = {};
19+
}
20+
this.initialized = true;
21+
},
22+
write(val, expireDate, isSecure, sameSite) {
23+
if (!this.initialized) this.read();
24+
this.value = val;
25+
if (expireDate) this.expires = expireDate;
26+
Cookies.set(this.cookieName, this.value, {
27+
expires : this.expires,
28+
path : this.path,
29+
domain : this.domain,
30+
secure : !!isSecure,
31+
sameSite: sameSite || 'strict',
32+
});
33+
},
34+
get(key) {
35+
if (!this.initialized) this.read();
36+
return this.value[key];
37+
},
38+
set(key, val, options) {
39+
if (!this.initialized) this.read();
40+
this.value[key] = val;
41+
Cookies.set(this.cookieName, this.value, {
42+
expires: new Date(this.expires),
43+
path : this.path,
44+
domain : this.domain,
45+
...options,
46+
});
47+
},
48+
remove() {
49+
Cookies.remove(this.cookieName, {
50+
path : this.path,
51+
domain: this.domain,
52+
});
53+
},
54+
};
55+
56+
export const setCookieLanguage = lang => {
57+
if (!Cookies.get('language') || lang) {
58+
const cookie = new CookieStorage('language');
59+
cookie.write(lang.toUpperCase(), undefined, true, 'none');
60+
}
61+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy