gt-node 1.0.1 → 1.0.3
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/CHANGELOG.md +18 -0
- package/dist/index.cjs +1 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +2 -23
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# gt-node
|
|
2
2
|
|
|
3
|
+
## 1.0.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`6345dc5`](https://github.com/generaltranslation/gt/commit/6345dc5e3fe0a1e3ead9a3c30a0adaa4037d50a8)]:
|
|
8
|
+
- gt-i18n@1.0.3
|
|
9
|
+
|
|
10
|
+
## 1.0.2
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- [#1846](https://github.com/generaltranslation/gt/pull/1846) [`7fb4a74`](https://github.com/generaltranslation/gt/commit/7fb4a74c52065694a40deafcf4596acc09e17f58) Thanks [@ErnestM1234](https://github.com/ErnestM1234)! - Add `parseLocale(request)` to resolve server-rendered React locales from the configured cookie, the `Accept-Language` header, or the default locale.
|
|
15
|
+
|
|
16
|
+
Share cookie and `Accept-Language` parsing across the framework packages.
|
|
17
|
+
|
|
18
|
+
- Updated dependencies [[`006e071`](https://github.com/generaltranslation/gt/commit/006e071bf87ffe80f2d18958ddfa8f18cc2d85d2), [`7fb4a74`](https://github.com/generaltranslation/gt/commit/7fb4a74c52065694a40deafcf4596acc09e17f58), [`1f33d5f`](https://github.com/generaltranslation/gt/commit/1f33d5f76ffc879d2d21aa2508e07e1d3b66c4e3)]:
|
|
19
|
+
- gt-i18n@1.0.2
|
|
20
|
+
|
|
3
21
|
## 1.0.1
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -26,26 +26,6 @@ function withGT(locale, fn) {
|
|
|
26
26
|
//#endregion
|
|
27
27
|
//#region src/helpers/getRequestLocale.ts
|
|
28
28
|
/**
|
|
29
|
-
* Parse the Accept-Language header into an array of locales
|
|
30
|
-
* @param header - The Accept-Language header
|
|
31
|
-
* @returns An array of locales
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* const locales = parseAcceptLanguage('fr-FR,fr;q=0.9,en;q=0.8');
|
|
35
|
-
* console.log(locales); // ['fr-FR', 'fr', 'en']
|
|
36
|
-
*/
|
|
37
|
-
function parseAcceptLanguage(header) {
|
|
38
|
-
return header.split(",").map((entry) => {
|
|
39
|
-
const [locale, quality] = entry.trim().split(";");
|
|
40
|
-
const qPart = quality?.split("=")[1];
|
|
41
|
-
const q = qPart !== void 0 ? parseFloat(qPart) : 1;
|
|
42
|
-
return {
|
|
43
|
-
locale: locale.trim(),
|
|
44
|
-
quality: isNaN(q) ? 1 : q
|
|
45
|
-
};
|
|
46
|
-
}).sort((a, b) => b.quality - a.quality).map((entry) => entry.locale);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
29
|
* Resolve the preferred locale from the request Accept-Language header, fallback to the default locale if no match is found
|
|
50
30
|
* @param request - The request object
|
|
51
31
|
* @returns The preferred locale
|
|
@@ -65,8 +45,7 @@ function parseAcceptLanguage(header) {
|
|
|
65
45
|
function getRequestLocale(request) {
|
|
66
46
|
const i18nConfig = (0, gt_i18n_internal.getI18nConfig)();
|
|
67
47
|
const acceptLanguage = request.headers["accept-language"];
|
|
68
|
-
const
|
|
69
|
-
const preferredLocales = headerValue ? parseAcceptLanguage(headerValue) : [];
|
|
48
|
+
const preferredLocales = (0, gt_i18n_internal.parseAcceptLanguage)(acceptLanguage);
|
|
70
49
|
return i18nConfig.determineLocale(preferredLocales) || i18nConfig.getDefaultLocale();
|
|
71
50
|
}
|
|
72
51
|
//#endregion
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["I18nCache","AsyncConditionStore","getAsyncConditionStore"],"sources":["../src/setup/initializeGT.ts","../src/setup/withGT.ts","../src/helpers/getRequestLocale.ts"],"sourcesContent":["import { setAsyncConditionStore } from '../async-i18n-cache/singleton-operations';\nimport type { InitializeGTParams } from './types';\nimport {\n I18nCache,\n initializeI18nConfig,\n setI18nCache,\n} from 'gt-i18n/internal';\nimport { AsyncConditionStore } from '../async-i18n-cache/AsyncConditionStore';\n\n/**\n * Configure GT for node runtime. This must be called to setup GT for node runtime.\n * @param {InitializeGTParams} config - The configuration for the GT instance\n * TODO: auto detect if can find gt.config.json files\n */\nexport function initializeGT(params: InitializeGTParams): void {\n initializeI18nConfig(params);\n\n const i18nCache = new I18nCache<string>(params);\n const conditionStore = new AsyncConditionStore();\n\n setI18nCache(i18nCache);\n setAsyncConditionStore(conditionStore);\n}\n","import { getAsyncConditionStore } from '../async-i18n-cache/singleton-operations';\n\n/**\n * This function wraps entry points to provide GT context\n */\nexport function withGT<T>(locale: string, fn: () => T): T {\n return getAsyncConditionStore().run<T>(locale, fn);\n}\n","import { getI18nConfig } from 'gt-i18n/internal';\n\n/**\n * A request object like interface\n * @interface RequestLike\n * @property {Record<string, string | string[] | undefined>} headers - The request headers\n */\ninterface RequestLike {\n headers: Record<string, string | string[] | undefined>;\n}\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["I18nCache","AsyncConditionStore","getAsyncConditionStore"],"sources":["../src/setup/initializeGT.ts","../src/setup/withGT.ts","../src/helpers/getRequestLocale.ts"],"sourcesContent":["import { setAsyncConditionStore } from '../async-i18n-cache/singleton-operations';\nimport type { InitializeGTParams } from './types';\nimport {\n I18nCache,\n initializeI18nConfig,\n setI18nCache,\n} from 'gt-i18n/internal';\nimport { AsyncConditionStore } from '../async-i18n-cache/AsyncConditionStore';\n\n/**\n * Configure GT for node runtime. This must be called to setup GT for node runtime.\n * @param {InitializeGTParams} config - The configuration for the GT instance\n * TODO: auto detect if can find gt.config.json files\n */\nexport function initializeGT(params: InitializeGTParams): void {\n initializeI18nConfig(params);\n\n const i18nCache = new I18nCache<string>(params);\n const conditionStore = new AsyncConditionStore();\n\n setI18nCache(i18nCache);\n setAsyncConditionStore(conditionStore);\n}\n","import { getAsyncConditionStore } from '../async-i18n-cache/singleton-operations';\n\n/**\n * This function wraps entry points to provide GT context\n */\nexport function withGT<T>(locale: string, fn: () => T): T {\n return getAsyncConditionStore().run<T>(locale, fn);\n}\n","import { getI18nConfig, parseAcceptLanguage } from 'gt-i18n/internal';\n\n/**\n * A request object like interface\n * @interface RequestLike\n * @property {Record<string, string | string[] | undefined>} headers - The request headers\n */\ninterface RequestLike {\n headers: Record<string, string | string[] | undefined>;\n}\n\n/**\n * Resolve the preferred locale from the request Accept-Language header, fallback to the default locale if no match is found\n * @param request - The request object\n * @returns The preferred locale\n *\n * @example\n * const locale = getRequestLocale({ headers: { 'accept-language': 'fr-FR,fr;q=0.9,en;q=0.8' } });\n * console.log(locale); // 'fr'\n *\n * @example\n * app.get('/', (req, res) => {\n * const locale = getRequestLocale(req);\n * withGT(locale, () => {\n * res.send(`Locale: ${locale}`);\n * });\n * });\n */\nexport function getRequestLocale(request: RequestLike): string {\n // Setup\n const i18nConfig = getI18nConfig();\n\n // Get the accept-language header\n const acceptLanguage = request.headers['accept-language'];\n const preferredLocales = parseAcceptLanguage(acceptLanguage);\n return (\n i18nConfig.determineLocale(preferredLocales) ||\n i18nConfig.getDefaultLocale()\n );\n}\n"],"mappings":";;;;;;;;;;AAcA,SAAgB,aAAa,QAAkC;AAC7D,EAAA,GAAA,iBAAA,sBAAqB,OAAO;CAE5B,MAAM,YAAY,IAAIA,iBAAAA,UAAkB,OAAO;CAC/C,MAAM,iBAAiB,IAAIC,4BAAAA,qBAAqB;AAEhD,EAAA,GAAA,iBAAA,cAAa,UAAU;AACvB,6BAAA,uBAAuB,eAAe;;;;;;;AChBxC,SAAgB,OAAU,QAAgB,IAAgB;AACxD,QAAOC,4BAAAA,wBAAwB,CAAC,IAAO,QAAQ,GAAG;;;;;;;;;;;;;;;;;;;;;ACsBpD,SAAgB,iBAAiB,SAA8B;CAE7D,MAAM,cAAA,GAAA,iBAAA,gBAA4B;CAGlC,MAAM,iBAAiB,QAAQ,QAAQ;CACvC,MAAM,oBAAA,GAAA,iBAAA,qBAAuC,eAAe;AAC5D,QACE,WAAW,gBAAgB,iBAAiB,IAC5C,WAAW,kBAAkB"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { n as getAsyncConditionStore, r as setAsyncConditionStore, t as AsyncConditionStore } from "./AsyncConditionStore-Dr27x7HT.mjs";
|
|
2
|
-
import { I18nCache, getGT, getI18nConfig, getMessages, getTranslations, initializeI18nConfig, setI18nCache, tx } from "gt-i18n/internal";
|
|
2
|
+
import { I18nCache, getGT, getI18nConfig, getMessages, getTranslations, initializeI18nConfig, parseAcceptLanguage, setI18nCache, tx } from "gt-i18n/internal";
|
|
3
3
|
import { declareVar, decodeMsg, decodeOptions, decodeVars, derive, getDefaultLocale, getLocale, getLocaleProperties, getLocales, getVersionId, msg } from "gt-i18n";
|
|
4
4
|
//#region src/setup/initializeGT.ts
|
|
5
5
|
/**
|
|
@@ -25,26 +25,6 @@ function withGT(locale, fn) {
|
|
|
25
25
|
//#endregion
|
|
26
26
|
//#region src/helpers/getRequestLocale.ts
|
|
27
27
|
/**
|
|
28
|
-
* Parse the Accept-Language header into an array of locales
|
|
29
|
-
* @param header - The Accept-Language header
|
|
30
|
-
* @returns An array of locales
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* const locales = parseAcceptLanguage('fr-FR,fr;q=0.9,en;q=0.8');
|
|
34
|
-
* console.log(locales); // ['fr-FR', 'fr', 'en']
|
|
35
|
-
*/
|
|
36
|
-
function parseAcceptLanguage(header) {
|
|
37
|
-
return header.split(",").map((entry) => {
|
|
38
|
-
const [locale, quality] = entry.trim().split(";");
|
|
39
|
-
const qPart = quality?.split("=")[1];
|
|
40
|
-
const q = qPart !== void 0 ? parseFloat(qPart) : 1;
|
|
41
|
-
return {
|
|
42
|
-
locale: locale.trim(),
|
|
43
|
-
quality: isNaN(q) ? 1 : q
|
|
44
|
-
};
|
|
45
|
-
}).sort((a, b) => b.quality - a.quality).map((entry) => entry.locale);
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
28
|
* Resolve the preferred locale from the request Accept-Language header, fallback to the default locale if no match is found
|
|
49
29
|
* @param request - The request object
|
|
50
30
|
* @returns The preferred locale
|
|
@@ -64,8 +44,7 @@ function parseAcceptLanguage(header) {
|
|
|
64
44
|
function getRequestLocale(request) {
|
|
65
45
|
const i18nConfig = getI18nConfig();
|
|
66
46
|
const acceptLanguage = request.headers["accept-language"];
|
|
67
|
-
const
|
|
68
|
-
const preferredLocales = headerValue ? parseAcceptLanguage(headerValue) : [];
|
|
47
|
+
const preferredLocales = parseAcceptLanguage(acceptLanguage);
|
|
69
48
|
return i18nConfig.determineLocale(preferredLocales) || i18nConfig.getDefaultLocale();
|
|
70
49
|
}
|
|
71
50
|
//#endregion
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/setup/initializeGT.ts","../src/setup/withGT.ts","../src/helpers/getRequestLocale.ts"],"sourcesContent":["import { setAsyncConditionStore } from '../async-i18n-cache/singleton-operations';\nimport type { InitializeGTParams } from './types';\nimport {\n I18nCache,\n initializeI18nConfig,\n setI18nCache,\n} from 'gt-i18n/internal';\nimport { AsyncConditionStore } from '../async-i18n-cache/AsyncConditionStore';\n\n/**\n * Configure GT for node runtime. This must be called to setup GT for node runtime.\n * @param {InitializeGTParams} config - The configuration for the GT instance\n * TODO: auto detect if can find gt.config.json files\n */\nexport function initializeGT(params: InitializeGTParams): void {\n initializeI18nConfig(params);\n\n const i18nCache = new I18nCache<string>(params);\n const conditionStore = new AsyncConditionStore();\n\n setI18nCache(i18nCache);\n setAsyncConditionStore(conditionStore);\n}\n","import { getAsyncConditionStore } from '../async-i18n-cache/singleton-operations';\n\n/**\n * This function wraps entry points to provide GT context\n */\nexport function withGT<T>(locale: string, fn: () => T): T {\n return getAsyncConditionStore().run<T>(locale, fn);\n}\n","import { getI18nConfig } from 'gt-i18n/internal';\n\n/**\n * A request object like interface\n * @interface RequestLike\n * @property {Record<string, string | string[] | undefined>} headers - The request headers\n */\ninterface RequestLike {\n headers: Record<string, string | string[] | undefined>;\n}\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/setup/initializeGT.ts","../src/setup/withGT.ts","../src/helpers/getRequestLocale.ts"],"sourcesContent":["import { setAsyncConditionStore } from '../async-i18n-cache/singleton-operations';\nimport type { InitializeGTParams } from './types';\nimport {\n I18nCache,\n initializeI18nConfig,\n setI18nCache,\n} from 'gt-i18n/internal';\nimport { AsyncConditionStore } from '../async-i18n-cache/AsyncConditionStore';\n\n/**\n * Configure GT for node runtime. This must be called to setup GT for node runtime.\n * @param {InitializeGTParams} config - The configuration for the GT instance\n * TODO: auto detect if can find gt.config.json files\n */\nexport function initializeGT(params: InitializeGTParams): void {\n initializeI18nConfig(params);\n\n const i18nCache = new I18nCache<string>(params);\n const conditionStore = new AsyncConditionStore();\n\n setI18nCache(i18nCache);\n setAsyncConditionStore(conditionStore);\n}\n","import { getAsyncConditionStore } from '../async-i18n-cache/singleton-operations';\n\n/**\n * This function wraps entry points to provide GT context\n */\nexport function withGT<T>(locale: string, fn: () => T): T {\n return getAsyncConditionStore().run<T>(locale, fn);\n}\n","import { getI18nConfig, parseAcceptLanguage } from 'gt-i18n/internal';\n\n/**\n * A request object like interface\n * @interface RequestLike\n * @property {Record<string, string | string[] | undefined>} headers - The request headers\n */\ninterface RequestLike {\n headers: Record<string, string | string[] | undefined>;\n}\n\n/**\n * Resolve the preferred locale from the request Accept-Language header, fallback to the default locale if no match is found\n * @param request - The request object\n * @returns The preferred locale\n *\n * @example\n * const locale = getRequestLocale({ headers: { 'accept-language': 'fr-FR,fr;q=0.9,en;q=0.8' } });\n * console.log(locale); // 'fr'\n *\n * @example\n * app.get('/', (req, res) => {\n * const locale = getRequestLocale(req);\n * withGT(locale, () => {\n * res.send(`Locale: ${locale}`);\n * });\n * });\n */\nexport function getRequestLocale(request: RequestLike): string {\n // Setup\n const i18nConfig = getI18nConfig();\n\n // Get the accept-language header\n const acceptLanguage = request.headers['accept-language'];\n const preferredLocales = parseAcceptLanguage(acceptLanguage);\n return (\n i18nConfig.determineLocale(preferredLocales) ||\n i18nConfig.getDefaultLocale()\n );\n}\n"],"mappings":";;;;;;;;;AAcA,SAAgB,aAAa,QAAkC;AAC7D,sBAAqB,OAAO;CAE5B,MAAM,YAAY,IAAI,UAAkB,OAAO;CAC/C,MAAM,iBAAiB,IAAI,qBAAqB;AAEhD,cAAa,UAAU;AACvB,wBAAuB,eAAe;;;;;;;AChBxC,SAAgB,OAAU,QAAgB,IAAgB;AACxD,QAAO,wBAAwB,CAAC,IAAO,QAAQ,GAAG;;;;;;;;;;;;;;;;;;;;;ACsBpD,SAAgB,iBAAiB,SAA8B;CAE7D,MAAM,aAAa,eAAe;CAGlC,MAAM,iBAAiB,QAAQ,QAAQ;CACvC,MAAM,mBAAmB,oBAAoB,eAAe;AAC5D,QACE,WAAW,gBAAgB,iBAAiB,IAC5C,WAAW,kBAAkB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gt-node",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Node.js utilities for General Translation",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"definition": "dist/index.d.cts"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"gt-i18n": "1.0.
|
|
47
|
+
"gt-i18n": "1.0.3",
|
|
48
48
|
"generaltranslation": "9.0.0"
|
|
49
49
|
},
|
|
50
50
|
"exports": {
|