@eggjs/i18n 4.0.0-beta.35 → 4.0.0-beta.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app/extend/application.d.ts +10 -6
- package/dist/app/extend/application.js +41 -69
- package/dist/app/extend/context.d.ts +75 -71
- package/dist/app/extend/context.js +149 -173
- package/dist/app.d.ts +57 -52
- package/dist/app.js +94 -95
- package/dist/config/config.default.d.ts +51 -49
- package/dist/config/config.default.js +15 -14
- package/dist/index.d.ts +20 -16
- package/dist/index.js +23 -20
- package/dist/locales.d.ts +7 -3
- package/dist/locales.js +48 -61
- package/dist/types.d.ts +29 -27
- package/dist/types.js +1 -2
- package/dist/utils.d.ts +5 -2
- package/dist/utils.js +8 -6
- package/package.json +25 -32
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { Application } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { Application } from "egg";
|
|
2
|
+
|
|
3
|
+
//#region src/app/extend/application.d.ts
|
|
4
|
+
declare class I18nApplication extends Application {
|
|
5
|
+
_I18N_RESOURCES: Record<string, Record<string, string>>;
|
|
6
|
+
isSupportLocale(locale: string): boolean;
|
|
7
|
+
gettext(locale: string, key: string, value?: any, ...args: any[]): string;
|
|
8
|
+
__(locale: string, key: string, value?: any, ...args: any[]): string;
|
|
7
9
|
}
|
|
10
|
+
//#endregion
|
|
11
|
+
export { I18nApplication as default };
|
|
@@ -1,76 +1,48 @@
|
|
|
1
|
-
import { debuglog, format } from 'node:util';
|
|
2
|
-
import { Application } from 'egg';
|
|
3
1
|
import { isObject } from "../../utils.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// __('zh', '{a} {b} {b} {a}', {a: 'foo', b: 'bar'})
|
|
33
|
-
// =>
|
|
34
|
-
// foo bar bar foo
|
|
35
|
-
return formatWithObject(text, value);
|
|
36
|
-
}
|
|
37
|
-
if (Array.isArray(value)) {
|
|
38
|
-
// __(locale, key, array)
|
|
39
|
-
// __('zh', '{0} {1} {1} {0}', ['foo', 'bar'])
|
|
40
|
-
// =>
|
|
41
|
-
// foo bar bar foo
|
|
42
|
-
return formatWithArray(text, value);
|
|
43
|
-
}
|
|
44
|
-
// __(locale, key, value)
|
|
45
|
-
return format(text, value);
|
|
46
|
-
}
|
|
47
|
-
// __(locale, key, value1, ...)
|
|
48
|
-
return format(text, value, ...args);
|
|
49
|
-
}
|
|
50
|
-
__(locale, key, value, ...args) {
|
|
51
|
-
return this.gettext(locale, key, value, ...args);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
2
|
+
import { debuglog, format } from "node:util";
|
|
3
|
+
import { Application } from "egg";
|
|
4
|
+
|
|
5
|
+
//#region src/app/extend/application.ts
|
|
6
|
+
const debug = debuglog("egg/i18n/app/extend/application");
|
|
7
|
+
var I18nApplication = class extends Application {
|
|
8
|
+
_I18N_RESOURCES;
|
|
9
|
+
isSupportLocale(locale) {
|
|
10
|
+
return !!this._I18N_RESOURCES[locale];
|
|
11
|
+
}
|
|
12
|
+
gettext(locale, key, value, ...args) {
|
|
13
|
+
if (!locale || !key) return "";
|
|
14
|
+
let text = (this._I18N_RESOURCES[locale] ?? {})[key];
|
|
15
|
+
if (text === void 0) text = key;
|
|
16
|
+
debug("%s: %j => %j", locale, key, text);
|
|
17
|
+
if (!text) return "";
|
|
18
|
+
if (value === void 0) return text;
|
|
19
|
+
if (args.length === 0) {
|
|
20
|
+
if (isObject(value)) return formatWithObject(text, value);
|
|
21
|
+
if (Array.isArray(value)) return formatWithArray(text, value);
|
|
22
|
+
return format(text, value);
|
|
23
|
+
}
|
|
24
|
+
return format(text, value, ...args);
|
|
25
|
+
}
|
|
26
|
+
__(locale, key, value, ...args) {
|
|
27
|
+
return this.gettext(locale, key, value, ...args);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
54
30
|
const ARRAY_INDEX_RE = /\{(\d+)\}/g;
|
|
55
31
|
function formatWithArray(text, values) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
// not match index, return original text
|
|
62
|
-
return original;
|
|
63
|
-
});
|
|
32
|
+
return text.replace(ARRAY_INDEX_RE, (original, matched) => {
|
|
33
|
+
const index = parseInt(matched);
|
|
34
|
+
if (index < values.length) return values[index];
|
|
35
|
+
return original;
|
|
36
|
+
});
|
|
64
37
|
}
|
|
65
38
|
const Object_INDEX_RE = /\{(.+?)\}/g;
|
|
66
39
|
function formatWithObject(text, values) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// not match index, return original text
|
|
73
|
-
return original;
|
|
74
|
-
});
|
|
40
|
+
return text.replace(Object_INDEX_RE, (original, matched) => {
|
|
41
|
+
const value = values[matched];
|
|
42
|
+
if (value) return value;
|
|
43
|
+
return original;
|
|
44
|
+
});
|
|
75
45
|
}
|
|
76
|
-
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
export { I18nApplication as default };
|
|
@@ -1,72 +1,76 @@
|
|
|
1
|
-
import { Context } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
1
|
+
import { Context } from "egg";
|
|
2
|
+
|
|
3
|
+
//#region src/app/extend/context.d.ts
|
|
4
|
+
declare class I18nContext extends Context {
|
|
5
|
+
/**
|
|
6
|
+
* get current request locale
|
|
7
|
+
* @member Context#locale
|
|
8
|
+
* @return {String} lower case locale string, e.g.: 'zh-cn', 'en-us'
|
|
9
|
+
*/
|
|
10
|
+
get locale(): string;
|
|
11
|
+
set locale(l: string);
|
|
12
|
+
/**
|
|
13
|
+
* `ctx.__` 的别名。
|
|
14
|
+
* @see {@link Context#__}
|
|
15
|
+
* @function Context#gettext
|
|
16
|
+
*/
|
|
17
|
+
gettext(key: string, value?: any, ...args: any[]): string;
|
|
18
|
+
/**
|
|
19
|
+
* 如果开启了 I18n 多语言功能,那么会出现此 API,通过它可以获取到当前请求对应的本地化数据。
|
|
20
|
+
*
|
|
21
|
+
* 详细使用说明,请查看 {@link I18n}
|
|
22
|
+
* - `ctx.__ = function (key, value[, value2, ...])`: 类似 `util.format` 接口
|
|
23
|
+
* - `ctx.__ = function (key, values)`: 支持数组下标占位符方式,如
|
|
24
|
+
* - `__` 的别名是 `gettext(key, value)`
|
|
25
|
+
*
|
|
26
|
+
* > NOTE: __ 是两个下划线哦!
|
|
27
|
+
* @function Context#__
|
|
28
|
+
* @example
|
|
29
|
+
* ```js
|
|
30
|
+
* ctx.__('{0} {0} {1} {1}'), ['foo', 'bar'])
|
|
31
|
+
* ctx.gettext('{0} {0} {1} {1}'), ['foo', 'bar'])
|
|
32
|
+
* =>
|
|
33
|
+
* foo foo bar bar
|
|
34
|
+
* ```
|
|
35
|
+
* ##### Controller 下的使用示例
|
|
36
|
+
*
|
|
37
|
+
* ```js
|
|
38
|
+
* module.exports = function* () {
|
|
39
|
+
* this.body = {
|
|
40
|
+
* message: this.__('Welcome back, %s!', this.user.name),
|
|
41
|
+
* // 或者使用 gettext,如果觉得 __ 不好看的话
|
|
42
|
+
* // message: this.gettext('Welcome back, %s!', this.user.name),
|
|
43
|
+
* user: this.user,
|
|
44
|
+
* };
|
|
45
|
+
* };
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* ##### View 文件下的使用示例
|
|
49
|
+
*
|
|
50
|
+
* ```html
|
|
51
|
+
* <li>{{ __('Email') }}: {{ user.email }}</li>
|
|
52
|
+
* <li>
|
|
53
|
+
* {{ __('Hello %s, how are you today?', user.name) }}
|
|
54
|
+
* </li>
|
|
55
|
+
* <li>
|
|
56
|
+
* {{ __('{0} {0} {1} {1}'), ['foo', 'bar']) }}
|
|
57
|
+
* </li>
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* ##### locale 参数获取途径
|
|
61
|
+
*
|
|
62
|
+
* 优先级从上到下:
|
|
63
|
+
*
|
|
64
|
+
* - query: `/?locale=en-US`
|
|
65
|
+
* - cookie: `locale=zh-TW`
|
|
66
|
+
* - header: `Accept-Language: zh-CN,zh;q=0.5`
|
|
67
|
+
*/
|
|
68
|
+
__(key: string, value?: any, ...args: any[]): string;
|
|
69
|
+
__locale: string;
|
|
70
|
+
__getLocale(): string;
|
|
71
|
+
__localeOrigin: string;
|
|
72
|
+
__getLocaleOrigin(): string;
|
|
73
|
+
__setLocale(locale: string): void;
|
|
72
74
|
}
|
|
75
|
+
//#endregion
|
|
76
|
+
export { I18nContext as default };
|
|
@@ -1,176 +1,152 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import { Context } from 'egg';
|
|
3
1
|
import { formatLocale } from "../../utils.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (writeCookie && cookieLocale !== locale && !this.headerSent) {
|
|
141
|
-
updateCookie(this, locale);
|
|
142
|
-
}
|
|
143
|
-
debug('Locale: %s from %s', locale, localeOrigin);
|
|
144
|
-
this.__locale = locale;
|
|
145
|
-
this.__localeOrigin = localeOrigin;
|
|
146
|
-
return locale;
|
|
147
|
-
}
|
|
148
|
-
__getLocaleOrigin() {
|
|
149
|
-
if (this.__localeOrigin) {
|
|
150
|
-
return this.__localeOrigin;
|
|
151
|
-
}
|
|
152
|
-
this.__getLocale();
|
|
153
|
-
return this.__localeOrigin;
|
|
154
|
-
}
|
|
155
|
-
__setLocale(locale) {
|
|
156
|
-
this.__locale = locale;
|
|
157
|
-
this.__localeOrigin = 'set';
|
|
158
|
-
if (this.app.config.i18n.writeCookie && !this.headerSent) {
|
|
159
|
-
updateCookie(this, locale);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
2
|
+
import { debuglog } from "node:util";
|
|
3
|
+
import { Context } from "egg";
|
|
4
|
+
|
|
5
|
+
//#region src/app/extend/context.ts
|
|
6
|
+
const debug = debuglog("egg/i18n/app/extend/context");
|
|
7
|
+
var I18nContext = class extends Context {
|
|
8
|
+
/**
|
|
9
|
+
* get current request locale
|
|
10
|
+
* @member Context#locale
|
|
11
|
+
* @return {String} lower case locale string, e.g.: 'zh-cn', 'en-us'
|
|
12
|
+
*/
|
|
13
|
+
get locale() {
|
|
14
|
+
return this.__getLocale();
|
|
15
|
+
}
|
|
16
|
+
set locale(l) {
|
|
17
|
+
this.__setLocale(l);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* `ctx.__` 的别名。
|
|
21
|
+
* @see {@link Context#__}
|
|
22
|
+
* @function Context#gettext
|
|
23
|
+
*/
|
|
24
|
+
gettext(key, value, ...args) {
|
|
25
|
+
return this.app.gettext(this.locale, key, value, ...args);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 如果开启了 I18n 多语言功能,那么会出现此 API,通过它可以获取到当前请求对应的本地化数据。
|
|
29
|
+
*
|
|
30
|
+
* 详细使用说明,请查看 {@link I18n}
|
|
31
|
+
* - `ctx.__ = function (key, value[, value2, ...])`: 类似 `util.format` 接口
|
|
32
|
+
* - `ctx.__ = function (key, values)`: 支持数组下标占位符方式,如
|
|
33
|
+
* - `__` 的别名是 `gettext(key, value)`
|
|
34
|
+
*
|
|
35
|
+
* > NOTE: __ 是两个下划线哦!
|
|
36
|
+
* @function Context#__
|
|
37
|
+
* @example
|
|
38
|
+
* ```js
|
|
39
|
+
* ctx.__('{0} {0} {1} {1}'), ['foo', 'bar'])
|
|
40
|
+
* ctx.gettext('{0} {0} {1} {1}'), ['foo', 'bar'])
|
|
41
|
+
* =>
|
|
42
|
+
* foo foo bar bar
|
|
43
|
+
* ```
|
|
44
|
+
* ##### Controller 下的使用示例
|
|
45
|
+
*
|
|
46
|
+
* ```js
|
|
47
|
+
* module.exports = function* () {
|
|
48
|
+
* this.body = {
|
|
49
|
+
* message: this.__('Welcome back, %s!', this.user.name),
|
|
50
|
+
* // 或者使用 gettext,如果觉得 __ 不好看的话
|
|
51
|
+
* // message: this.gettext('Welcome back, %s!', this.user.name),
|
|
52
|
+
* user: this.user,
|
|
53
|
+
* };
|
|
54
|
+
* };
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* ##### View 文件下的使用示例
|
|
58
|
+
*
|
|
59
|
+
* ```html
|
|
60
|
+
* <li>{{ __('Email') }}: {{ user.email }}</li>
|
|
61
|
+
* <li>
|
|
62
|
+
* {{ __('Hello %s, how are you today?', user.name) }}
|
|
63
|
+
* </li>
|
|
64
|
+
* <li>
|
|
65
|
+
* {{ __('{0} {0} {1} {1}'), ['foo', 'bar']) }}
|
|
66
|
+
* </li>
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* ##### locale 参数获取途径
|
|
70
|
+
*
|
|
71
|
+
* 优先级从上到下:
|
|
72
|
+
*
|
|
73
|
+
* - query: `/?locale=en-US`
|
|
74
|
+
* - cookie: `locale=zh-TW`
|
|
75
|
+
* - header: `Accept-Language: zh-CN,zh;q=0.5`
|
|
76
|
+
*/
|
|
77
|
+
__(key, value, ...args) {
|
|
78
|
+
return this.gettext(key, value, ...args);
|
|
79
|
+
}
|
|
80
|
+
__getLocale() {
|
|
81
|
+
if (this.__locale) return this.__locale;
|
|
82
|
+
const { localeAlias, defaultLocale, cookieField, queryField, writeCookie } = this.app.config.i18n;
|
|
83
|
+
const cookieLocale = this.cookies.get(cookieField, { signed: false });
|
|
84
|
+
let locale = this.query[queryField];
|
|
85
|
+
let localeOrigin = "query";
|
|
86
|
+
if (!locale && cookieLocale) {
|
|
87
|
+
locale = cookieLocale;
|
|
88
|
+
localeOrigin = "cookie";
|
|
89
|
+
}
|
|
90
|
+
if (!locale) {
|
|
91
|
+
let languages = this.acceptsLanguages();
|
|
92
|
+
if (languages) if (Array.isArray(languages)) {
|
|
93
|
+
if (languages[0] === "*") languages = languages.slice(1);
|
|
94
|
+
if (languages.length > 0) for (const l of languages) {
|
|
95
|
+
const lang = formatLocale(l);
|
|
96
|
+
if (this.app.isSupportLocale(lang) || localeAlias[lang]) {
|
|
97
|
+
locale = lang;
|
|
98
|
+
localeOrigin = "header";
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
locale = languages;
|
|
104
|
+
localeOrigin = "header";
|
|
105
|
+
}
|
|
106
|
+
if (!locale) {
|
|
107
|
+
locale = defaultLocale;
|
|
108
|
+
localeOrigin = "default";
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (locale in localeAlias) {
|
|
112
|
+
const originalLocale = locale;
|
|
113
|
+
locale = localeAlias[locale];
|
|
114
|
+
debug("Used alias, received %s but using %s", originalLocale, locale);
|
|
115
|
+
}
|
|
116
|
+
locale = formatLocale(locale);
|
|
117
|
+
if (!this.app.isSupportLocale(locale)) {
|
|
118
|
+
debug("Locale %s is not supported. Using default (%s)", locale, defaultLocale);
|
|
119
|
+
locale = defaultLocale;
|
|
120
|
+
}
|
|
121
|
+
if (writeCookie && cookieLocale !== locale && !this.headerSent) updateCookie(this, locale);
|
|
122
|
+
debug("Locale: %s from %s", locale, localeOrigin);
|
|
123
|
+
this.__locale = locale;
|
|
124
|
+
this.__localeOrigin = localeOrigin;
|
|
125
|
+
return locale;
|
|
126
|
+
}
|
|
127
|
+
__getLocaleOrigin() {
|
|
128
|
+
if (this.__localeOrigin) return this.__localeOrigin;
|
|
129
|
+
this.__getLocale();
|
|
130
|
+
return this.__localeOrigin;
|
|
131
|
+
}
|
|
132
|
+
__setLocale(locale) {
|
|
133
|
+
this.__locale = locale;
|
|
134
|
+
this.__localeOrigin = "set";
|
|
135
|
+
if (this.app.config.i18n.writeCookie && !this.headerSent) updateCookie(this, locale);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
163
138
|
function updateCookie(ctx, locale) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
debug('Saved cookie with locale %s, options: %j', locale, cookieOptions);
|
|
139
|
+
const { cookieMaxAge, cookieField, cookieDomain } = ctx.app.config.i18n;
|
|
140
|
+
const cookieOptions = {
|
|
141
|
+
httpOnly: false,
|
|
142
|
+
maxAge: cookieMaxAge,
|
|
143
|
+
signed: false,
|
|
144
|
+
domain: cookieDomain,
|
|
145
|
+
overwrite: true
|
|
146
|
+
};
|
|
147
|
+
ctx.cookies.set(cookieField, locale, cookieOptions);
|
|
148
|
+
debug("Saved cookie with locale %s, options: %j", locale, cookieOptions);
|
|
175
149
|
}
|
|
176
|
-
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
export { I18nContext as default };
|