gt-node 0.6.3 → 0.6.5
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 +20 -0
- package/dist/AsyncStorageAdapter-D852UaVa.mjs +71 -0
- package/dist/AsyncStorageAdapter-D852UaVa.mjs.map +1 -0
- package/dist/AsyncStorageAdapter-xUXP4qma.cjs +100 -0
- package/dist/AsyncStorageAdapter-xUXP4qma.cjs.map +1 -0
- package/dist/index.cjs +179 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{index.d.ts → index.d.cts} +12 -18
- package/dist/index.d.mts +48 -0
- package/dist/index.mjs +86 -0
- package/dist/index.mjs.map +1 -0
- package/dist/internal.cjs +31 -0
- package/dist/internal.d.cts +54 -0
- package/dist/internal.d.mts +54 -0
- package/dist/internal.mjs +3 -0
- package/dist/types-BA-INDkb.d.mts +15 -0
- package/dist/types-Ctyskm8X.d.cts +15 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.cts +3 -0
- package/dist/types.d.mts +3 -0
- package/dist/types.mjs +1 -0
- package/package.json +36 -26
- package/dist/async-i18n-manager/AsyncStorageAdapter.d.ts +0 -19
- package/dist/async-i18n-manager/AsyncStorageI18nManager.d.ts +0 -17
- package/dist/async-i18n-manager/index.d.ts +0 -3
- package/dist/async-i18n-manager/singleton-operations.d.ts +0 -15
- package/dist/helpers/getRequestLocale.d.ts +0 -27
- package/dist/helpers/index.d.ts +0 -3
- package/dist/index.cjs.min.cjs +0 -36
- package/dist/index.cjs.min.cjs.map +0 -1
- package/dist/index.esm.min.mjs +0 -36
- package/dist/index.esm.min.mjs.map +0 -1
- package/dist/internal.cjs.min.cjs +0 -2
- package/dist/internal.cjs.min.cjs.map +0 -1
- package/dist/internal.d.ts +0 -51
- package/dist/internal.esm.min.mjs +0 -2
- package/dist/internal.esm.min.mjs.map +0 -1
- package/dist/setup/index.d.ts +0 -2
- package/dist/setup/initializeGT.d.ts +0 -7
- package/dist/setup/types.d.ts +0 -11
- package/dist/setup/withGT.d.ts +0 -4
- package/dist/translation-functions/index.d.ts +0 -6
- package/dist/translation-functions/types.d.ts +0 -6
- package/dist/types.cjs.min.cjs +0 -2
- package/dist/types.cjs.min.cjs.map +0 -1
- package/dist/types.d.ts +0 -15
- package/dist/types.esm.min.mjs +0 -2
- package/dist/types.esm.min.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# gt-node
|
|
2
2
|
|
|
3
|
+
## 0.6.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#1251](https://github.com/generaltranslation/gt/pull/1251) [`fc3c699`](https://github.com/generaltranslation/gt/commit/fc3c699d2c952710cc975e26629ac309063dcbc7) Thanks [@bgub](https://github.com/bgub)! - Declare `sideEffects` in each package's `package.json` to enable tree-shaking in consumer bundlers (webpack, esbuild, Rollup). Packages with no module-scope side effects are marked `"sideEffects": false`. Packages with intentional side-effect entry points (`gt-react/browser`, `gt-react/macros`, `gt-next` server entries, `gt-react-native` TurboModule spec) list those files explicitly so they are preserved.
|
|
8
|
+
|
|
9
|
+
- [#1249](https://github.com/generaltranslation/gt/pull/1249) [`50d7628`](https://github.com/generaltranslation/gt/commit/50d7628e23b056e91abf8fa05f6577b74cb91569) Thanks [@bgub](https://github.com/bgub)! - chore: migrate build from Rollup to tsdown
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`47ad56b`](https://github.com/generaltranslation/gt/commit/47ad56bb23a70382ba98a900d968e9a48beee2b8), [`e3a8008`](https://github.com/generaltranslation/gt/commit/e3a8008ed0a3ab82d053f549265f9de7829e94c5), [`fc3c699`](https://github.com/generaltranslation/gt/commit/fc3c699d2c952710cc975e26629ac309063dcbc7), [`50d7628`](https://github.com/generaltranslation/gt/commit/50d7628e23b056e91abf8fa05f6577b74cb91569)]:
|
|
12
|
+
- gt-i18n@0.8.5
|
|
13
|
+
- generaltranslation@8.2.7
|
|
14
|
+
|
|
15
|
+
## 0.6.4
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [[`8b75420`](https://github.com/generaltranslation/gt/commit/8b7542091233fb2c87284a365cc9ab8ce70371d3)]:
|
|
20
|
+
- generaltranslation@8.2.6
|
|
21
|
+
- gt-i18n@0.8.4
|
|
22
|
+
|
|
3
23
|
## 0.6.3
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { I18nManager, StorageAdapter, getI18nManager, setI18nManager } from "gt-i18n/internal";
|
|
2
|
+
import { determineLocale } from "generaltranslation";
|
|
3
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
|
+
//#region src/async-i18n-manager/singleton-operations.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description Get the singleton instance of I18nManager.
|
|
7
|
+
* @returns The singleton instance of I18nManager
|
|
8
|
+
*
|
|
9
|
+
* Node only does string translation
|
|
10
|
+
*/
|
|
11
|
+
function getI18nManager$1() {
|
|
12
|
+
return getI18nManager();
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Set the singleton instance of I18nManager
|
|
16
|
+
* @param {I18nManager<AsyncStorageAdapter>} i18nManager - The I18nManager instance
|
|
17
|
+
*/
|
|
18
|
+
function setI18nManager$1(i18nManager) {
|
|
19
|
+
setI18nManager(i18nManager);
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/async-i18n-manager/AsyncStorageI18nManager.ts
|
|
23
|
+
/**
|
|
24
|
+
* I18nManager implementation that uses AsyncStorage as the storage adapter.
|
|
25
|
+
*/
|
|
26
|
+
var AsyncStorageI18nManager = class extends I18nManager {
|
|
27
|
+
/**
|
|
28
|
+
* Creates an instance of AsyncStorageI18nManager.
|
|
29
|
+
* @param {I18nManagerConstructorParams<AsyncStorageAdapter>} config - The configuration for the AsyncStorageI18nManager
|
|
30
|
+
*/
|
|
31
|
+
constructor(config) {
|
|
32
|
+
super(config);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create the context for the given locale using the store adapter
|
|
36
|
+
*/
|
|
37
|
+
run(locale, fn) {
|
|
38
|
+
return this.storeAdapter.run({ locale: determineLocale(locale, this.config.locales, this.config.customMapping) || this.config.defaultLocale }, fn);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/async-i18n-manager/AsyncStorageAdapter.ts
|
|
43
|
+
const ASYNC_STORAGE_ADAPTER_TYPE = "async-storage-adapter";
|
|
44
|
+
/**
|
|
45
|
+
* AsyncStorageAdapter implementation that uses AsyncLocalStorage as the storage adapter.
|
|
46
|
+
*/
|
|
47
|
+
var AsyncStorageAdapter = class extends StorageAdapter {
|
|
48
|
+
constructor(store) {
|
|
49
|
+
super();
|
|
50
|
+
this.type = ASYNC_STORAGE_ADAPTER_TYPE;
|
|
51
|
+
this.store = store ?? new AsyncLocalStorage();
|
|
52
|
+
}
|
|
53
|
+
run(store, callback) {
|
|
54
|
+
return this.store.run(store, callback);
|
|
55
|
+
}
|
|
56
|
+
getItem(key) {
|
|
57
|
+
const store = this.store.getStore();
|
|
58
|
+
if (!store) return;
|
|
59
|
+
return store[key];
|
|
60
|
+
}
|
|
61
|
+
setItem(key, value) {
|
|
62
|
+
const store = this.store.getStore();
|
|
63
|
+
if (!store) throw new Error("setItem() called outside of an async context. Make sure you are inside a run() call.");
|
|
64
|
+
store[key] = value;
|
|
65
|
+
}
|
|
66
|
+
removeItem(key) {}
|
|
67
|
+
};
|
|
68
|
+
//#endregion
|
|
69
|
+
export { setI18nManager$1 as a, getI18nManager$1 as i, AsyncStorageAdapter as n, AsyncStorageI18nManager as r, ASYNC_STORAGE_ADAPTER_TYPE as t };
|
|
70
|
+
|
|
71
|
+
//# sourceMappingURL=AsyncStorageAdapter-D852UaVa.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AsyncStorageAdapter-D852UaVa.mjs","names":["getI18nManager","getI18nManagerBase","setI18nManager"],"sources":["../src/async-i18n-manager/singleton-operations.ts","../src/async-i18n-manager/AsyncStorageI18nManager.ts","../src/async-i18n-manager/AsyncStorageAdapter.ts"],"sourcesContent":["import {\n getI18nManager as getI18nManagerBase,\n I18nManager,\n setI18nManager as setI18nManagerBase,\n StorageAdapter,\n} from 'gt-i18n/internal';\nimport { AsyncStorageAdapter } from './AsyncStorageAdapter';\nimport { Translation } from 'gt-i18n/types';\n\n/**\n * @description Get the singleton instance of I18nManager.\n * @returns The singleton instance of I18nManager\n *\n * Node only does string translation\n */\nexport function getI18nManager():\n | I18nManager<AsyncStorageAdapter, string>\n | I18nManager<StorageAdapter, string>\n | I18nManager<AsyncStorageAdapter, Translation>\n | I18nManager<StorageAdapter, Translation> {\n const i18nManager = getI18nManagerBase<AsyncStorageAdapter, string>();\n return i18nManager;\n}\n\n/**\n * Set the singleton instance of I18nManager\n * @param {I18nManager<AsyncStorageAdapter>} i18nManager - The I18nManager instance\n */\nexport function setI18nManager(\n i18nManager: I18nManager<AsyncStorageAdapter>\n): void {\n setI18nManagerBase(i18nManager);\n}\n","import { I18nManager } from 'gt-i18n/internal';\nimport { AsyncStorageAdapter } from './AsyncStorageAdapter';\nimport { I18nManagerConstructorParams } from 'gt-i18n/internal/types';\nimport { determineLocale } from 'generaltranslation';\n\n/**\n * I18nManager implementation that uses AsyncStorage as the storage adapter.\n */\nexport class AsyncStorageI18nManager extends I18nManager<\n AsyncStorageAdapter,\n string\n> {\n /**\n * Creates an instance of AsyncStorageI18nManager.\n * @param {I18nManagerConstructorParams<AsyncStorageAdapter>} config - The configuration for the AsyncStorageI18nManager\n */\n constructor(config: I18nManagerConstructorParams<AsyncStorageAdapter>) {\n super(config);\n }\n\n /**\n * Create the context for the given locale using the store adapter\n */\n run<T>(locale: string, fn: () => T): T {\n return this.storeAdapter.run(\n {\n locale:\n determineLocale(\n locale,\n this.config.locales,\n this.config.customMapping\n ) || this.config.defaultLocale,\n },\n fn\n );\n }\n}\n","import { StorageAdapter } from 'gt-i18n/internal';\nimport { AsyncLocalStorage } from 'node:async_hooks';\n\ntype Store = {\n locale: string;\n};\n\nconst ASYNC_STORAGE_ADAPTER_TYPE = 'async-storage-adapter' as const;\n\n/**\n * AsyncStorageAdapter implementation that uses AsyncLocalStorage as the storage adapter.\n */\nclass AsyncStorageAdapter extends StorageAdapter {\n readonly type = ASYNC_STORAGE_ADAPTER_TYPE;\n\n private store: AsyncLocalStorage<Store>;\n\n constructor(store?: AsyncLocalStorage<Store>) {\n super();\n this.store = store ?? new AsyncLocalStorage();\n }\n\n run<T>(store: Store, callback: () => T): T {\n return this.store.run(store, callback);\n }\n\n getItem(key: keyof Store): string | undefined {\n const store = this.store.getStore();\n if (!store) {\n return undefined;\n }\n return store[key];\n }\n\n setItem(key: keyof Store, value: string): void {\n const store = this.store.getStore();\n if (!store) {\n throw new Error(\n 'setItem() called outside of an async context. Make sure you are inside a run() call.'\n );\n }\n store[key] = value;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars\n removeItem(key: keyof Store): void {\n // noop (locale is always set)\n }\n}\n\nexport { AsyncStorageAdapter, ASYNC_STORAGE_ADAPTER_TYPE };\n"],"mappings":";;;;;;;;;;AAeA,SAAgBA,mBAI6B;AAE3C,QADoBC,gBACF;;;;;;AAOpB,SAAgBC,iBACd,aACM;AACN,gBAAmB,YAAY;;;;;;;ACvBjC,IAAa,0BAAb,cAA6C,YAG3C;;;;;CAKA,YAAY,QAA2D;AACrE,QAAM,OAAO;;;;;CAMf,IAAO,QAAgB,IAAgB;AACrC,SAAO,KAAK,aAAa,IACvB,EACE,QACE,gBACE,QACA,KAAK,OAAO,SACZ,KAAK,OAAO,cACb,IAAI,KAAK,OAAO,eACpB,EACD,GACD;;;;;AC3BL,MAAM,6BAA6B;;;;AAKnC,IAAM,sBAAN,cAAkC,eAAe;CAK/C,YAAY,OAAkC;AAC5C,SAAO;cALO;AAMd,OAAK,QAAQ,SAAS,IAAI,mBAAmB;;CAG/C,IAAO,OAAc,UAAsB;AACzC,SAAO,KAAK,MAAM,IAAI,OAAO,SAAS;;CAGxC,QAAQ,KAAsC;EAC5C,MAAM,QAAQ,KAAK,MAAM,UAAU;AACnC,MAAI,CAAC,MACH;AAEF,SAAO,MAAM;;CAGf,QAAQ,KAAkB,OAAqB;EAC7C,MAAM,QAAQ,KAAK,MAAM,UAAU;AACnC,MAAI,CAAC,MACH,OAAM,IAAI,MACR,uFACD;AAEH,QAAM,OAAO;;CAIf,WAAW,KAAwB"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
let gt_i18n_internal = require("gt-i18n/internal");
|
|
2
|
+
let generaltranslation = require("generaltranslation");
|
|
3
|
+
let node_async_hooks = require("node:async_hooks");
|
|
4
|
+
//#region src/async-i18n-manager/singleton-operations.ts
|
|
5
|
+
/**
|
|
6
|
+
* @description Get the singleton instance of I18nManager.
|
|
7
|
+
* @returns The singleton instance of I18nManager
|
|
8
|
+
*
|
|
9
|
+
* Node only does string translation
|
|
10
|
+
*/
|
|
11
|
+
function getI18nManager() {
|
|
12
|
+
return (0, gt_i18n_internal.getI18nManager)();
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Set the singleton instance of I18nManager
|
|
16
|
+
* @param {I18nManager<AsyncStorageAdapter>} i18nManager - The I18nManager instance
|
|
17
|
+
*/
|
|
18
|
+
function setI18nManager(i18nManager) {
|
|
19
|
+
(0, gt_i18n_internal.setI18nManager)(i18nManager);
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/async-i18n-manager/AsyncStorageI18nManager.ts
|
|
23
|
+
/**
|
|
24
|
+
* I18nManager implementation that uses AsyncStorage as the storage adapter.
|
|
25
|
+
*/
|
|
26
|
+
var AsyncStorageI18nManager = class extends gt_i18n_internal.I18nManager {
|
|
27
|
+
/**
|
|
28
|
+
* Creates an instance of AsyncStorageI18nManager.
|
|
29
|
+
* @param {I18nManagerConstructorParams<AsyncStorageAdapter>} config - The configuration for the AsyncStorageI18nManager
|
|
30
|
+
*/
|
|
31
|
+
constructor(config) {
|
|
32
|
+
super(config);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create the context for the given locale using the store adapter
|
|
36
|
+
*/
|
|
37
|
+
run(locale, fn) {
|
|
38
|
+
return this.storeAdapter.run({ locale: (0, generaltranslation.determineLocale)(locale, this.config.locales, this.config.customMapping) || this.config.defaultLocale }, fn);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/async-i18n-manager/AsyncStorageAdapter.ts
|
|
43
|
+
const ASYNC_STORAGE_ADAPTER_TYPE = "async-storage-adapter";
|
|
44
|
+
/**
|
|
45
|
+
* AsyncStorageAdapter implementation that uses AsyncLocalStorage as the storage adapter.
|
|
46
|
+
*/
|
|
47
|
+
var AsyncStorageAdapter = class extends gt_i18n_internal.StorageAdapter {
|
|
48
|
+
constructor(store) {
|
|
49
|
+
super();
|
|
50
|
+
this.type = ASYNC_STORAGE_ADAPTER_TYPE;
|
|
51
|
+
this.store = store ?? new node_async_hooks.AsyncLocalStorage();
|
|
52
|
+
}
|
|
53
|
+
run(store, callback) {
|
|
54
|
+
return this.store.run(store, callback);
|
|
55
|
+
}
|
|
56
|
+
getItem(key) {
|
|
57
|
+
const store = this.store.getStore();
|
|
58
|
+
if (!store) return;
|
|
59
|
+
return store[key];
|
|
60
|
+
}
|
|
61
|
+
setItem(key, value) {
|
|
62
|
+
const store = this.store.getStore();
|
|
63
|
+
if (!store) throw new Error("setItem() called outside of an async context. Make sure you are inside a run() call.");
|
|
64
|
+
store[key] = value;
|
|
65
|
+
}
|
|
66
|
+
removeItem(key) {}
|
|
67
|
+
};
|
|
68
|
+
//#endregion
|
|
69
|
+
Object.defineProperty(exports, "ASYNC_STORAGE_ADAPTER_TYPE", {
|
|
70
|
+
enumerable: true,
|
|
71
|
+
get: function() {
|
|
72
|
+
return ASYNC_STORAGE_ADAPTER_TYPE;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
Object.defineProperty(exports, "AsyncStorageAdapter", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
get: function() {
|
|
78
|
+
return AsyncStorageAdapter;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
Object.defineProperty(exports, "AsyncStorageI18nManager", {
|
|
82
|
+
enumerable: true,
|
|
83
|
+
get: function() {
|
|
84
|
+
return AsyncStorageI18nManager;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
Object.defineProperty(exports, "getI18nManager", {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
get: function() {
|
|
90
|
+
return getI18nManager;
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
Object.defineProperty(exports, "setI18nManager", {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
get: function() {
|
|
96
|
+
return setI18nManager;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
//# sourceMappingURL=AsyncStorageAdapter-xUXP4qma.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AsyncStorageAdapter-xUXP4qma.cjs","names":["I18nManager","StorageAdapter","AsyncLocalStorage"],"sources":["../src/async-i18n-manager/singleton-operations.ts","../src/async-i18n-manager/AsyncStorageI18nManager.ts","../src/async-i18n-manager/AsyncStorageAdapter.ts"],"sourcesContent":["import {\n getI18nManager as getI18nManagerBase,\n I18nManager,\n setI18nManager as setI18nManagerBase,\n StorageAdapter,\n} from 'gt-i18n/internal';\nimport { AsyncStorageAdapter } from './AsyncStorageAdapter';\nimport { Translation } from 'gt-i18n/types';\n\n/**\n * @description Get the singleton instance of I18nManager.\n * @returns The singleton instance of I18nManager\n *\n * Node only does string translation\n */\nexport function getI18nManager():\n | I18nManager<AsyncStorageAdapter, string>\n | I18nManager<StorageAdapter, string>\n | I18nManager<AsyncStorageAdapter, Translation>\n | I18nManager<StorageAdapter, Translation> {\n const i18nManager = getI18nManagerBase<AsyncStorageAdapter, string>();\n return i18nManager;\n}\n\n/**\n * Set the singleton instance of I18nManager\n * @param {I18nManager<AsyncStorageAdapter>} i18nManager - The I18nManager instance\n */\nexport function setI18nManager(\n i18nManager: I18nManager<AsyncStorageAdapter>\n): void {\n setI18nManagerBase(i18nManager);\n}\n","import { I18nManager } from 'gt-i18n/internal';\nimport { AsyncStorageAdapter } from './AsyncStorageAdapter';\nimport { I18nManagerConstructorParams } from 'gt-i18n/internal/types';\nimport { determineLocale } from 'generaltranslation';\n\n/**\n * I18nManager implementation that uses AsyncStorage as the storage adapter.\n */\nexport class AsyncStorageI18nManager extends I18nManager<\n AsyncStorageAdapter,\n string\n> {\n /**\n * Creates an instance of AsyncStorageI18nManager.\n * @param {I18nManagerConstructorParams<AsyncStorageAdapter>} config - The configuration for the AsyncStorageI18nManager\n */\n constructor(config: I18nManagerConstructorParams<AsyncStorageAdapter>) {\n super(config);\n }\n\n /**\n * Create the context for the given locale using the store adapter\n */\n run<T>(locale: string, fn: () => T): T {\n return this.storeAdapter.run(\n {\n locale:\n determineLocale(\n locale,\n this.config.locales,\n this.config.customMapping\n ) || this.config.defaultLocale,\n },\n fn\n );\n }\n}\n","import { StorageAdapter } from 'gt-i18n/internal';\nimport { AsyncLocalStorage } from 'node:async_hooks';\n\ntype Store = {\n locale: string;\n};\n\nconst ASYNC_STORAGE_ADAPTER_TYPE = 'async-storage-adapter' as const;\n\n/**\n * AsyncStorageAdapter implementation that uses AsyncLocalStorage as the storage adapter.\n */\nclass AsyncStorageAdapter extends StorageAdapter {\n readonly type = ASYNC_STORAGE_ADAPTER_TYPE;\n\n private store: AsyncLocalStorage<Store>;\n\n constructor(store?: AsyncLocalStorage<Store>) {\n super();\n this.store = store ?? new AsyncLocalStorage();\n }\n\n run<T>(store: Store, callback: () => T): T {\n return this.store.run(store, callback);\n }\n\n getItem(key: keyof Store): string | undefined {\n const store = this.store.getStore();\n if (!store) {\n return undefined;\n }\n return store[key];\n }\n\n setItem(key: keyof Store, value: string): void {\n const store = this.store.getStore();\n if (!store) {\n throw new Error(\n 'setItem() called outside of an async context. Make sure you are inside a run() call.'\n );\n }\n store[key] = value;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars\n removeItem(key: keyof Store): void {\n // noop (locale is always set)\n }\n}\n\nexport { AsyncStorageAdapter, ASYNC_STORAGE_ADAPTER_TYPE };\n"],"mappings":";;;;;;;;;;AAeA,SAAgB,iBAI6B;AAE3C,SAAA,GAAA,iBAAA,iBAAkB;;;;;;AAOpB,SAAgB,eACd,aACM;AACN,EAAA,GAAA,iBAAA,gBAAmB,YAAY;;;;;;;ACvBjC,IAAa,0BAAb,cAA6CA,iBAAAA,YAG3C;;;;;CAKA,YAAY,QAA2D;AACrE,QAAM,OAAO;;;;;CAMf,IAAO,QAAgB,IAAgB;AACrC,SAAO,KAAK,aAAa,IACvB,EACE,SAAA,GAAA,mBAAA,iBAEI,QACA,KAAK,OAAO,SACZ,KAAK,OAAO,cACb,IAAI,KAAK,OAAO,eACpB,EACD,GACD;;;;;AC3BL,MAAM,6BAA6B;;;;AAKnC,IAAM,sBAAN,cAAkCC,iBAAAA,eAAe;CAK/C,YAAY,OAAkC;AAC5C,SAAO;cALO;AAMd,OAAK,QAAQ,SAAS,IAAIC,iBAAAA,mBAAmB;;CAG/C,IAAO,OAAc,UAAsB;AACzC,SAAO,KAAK,MAAM,IAAI,OAAO,SAAS;;CAGxC,QAAQ,KAAsC;EAC5C,MAAM,QAAQ,KAAK,MAAM,UAAU;AACnC,MAAI,CAAC,MACH;AAEF,SAAO,MAAM;;CAGf,QAAQ,KAAkB,OAAqB;EAC7C,MAAM,QAAQ,KAAK,MAAM,UAAU;AACnC,MAAI,CAAC,MACH,OAAM,IAAI,MACR,uFACD;AAEH,QAAM,OAAO;;CAIf,WAAW,KAAwB"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_AsyncStorageAdapter = require("./AsyncStorageAdapter-xUXP4qma.cjs");
|
|
3
|
+
let gt_i18n = require("gt-i18n");
|
|
4
|
+
let gt_i18n_internal = require("gt-i18n/internal");
|
|
5
|
+
//#region src/setup/initializeGT.ts
|
|
6
|
+
/**
|
|
7
|
+
* Configure GT for node runtime. This must be called to setup GT for node runtime.
|
|
8
|
+
* @param {InitializeGTParams} config - The configuration for the GT instance
|
|
9
|
+
* TODO: auto detect if can find gt.config.json files
|
|
10
|
+
*/
|
|
11
|
+
function initializeGT(params) {
|
|
12
|
+
require_AsyncStorageAdapter.setI18nManager(new require_AsyncStorageAdapter.AsyncStorageI18nManager({
|
|
13
|
+
...params,
|
|
14
|
+
storeAdapter: new require_AsyncStorageAdapter.AsyncStorageAdapter()
|
|
15
|
+
}));
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/setup/withGT.ts
|
|
19
|
+
/**
|
|
20
|
+
* This function wraps entry points to provide GT context
|
|
21
|
+
*/
|
|
22
|
+
function withGT(locale, fn) {
|
|
23
|
+
const i18nManager = require_AsyncStorageAdapter.getI18nManager();
|
|
24
|
+
if (!isAsyncStorageI18nManager(i18nManager)) throw new Error("I18nManager not initialized. Invoke configGT() to initialize.");
|
|
25
|
+
return i18nManager.run(locale, fn);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Check if the I18nManager is an AsyncStorageI18nManager
|
|
29
|
+
* @param i18nManager - The I18nManager instance
|
|
30
|
+
* @returns True if the I18nManager is an AsyncStorageI18nManager, false otherwise
|
|
31
|
+
*/
|
|
32
|
+
function isAsyncStorageI18nManager(i18nManager) {
|
|
33
|
+
return i18nManager.getAdapterType() === "async-storage-adapter" && "run" in i18nManager && typeof i18nManager.run === "function";
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/helpers/getRequestLocale.ts
|
|
37
|
+
/**
|
|
38
|
+
* Parse the Accept-Language header into an array of locales
|
|
39
|
+
* @param header - The Accept-Language header
|
|
40
|
+
* @returns An array of locales
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* const locales = parseAcceptLanguage('fr-FR,fr;q=0.9,en;q=0.8');
|
|
44
|
+
* console.log(locales); // ['fr-FR', 'fr', 'en']
|
|
45
|
+
*/
|
|
46
|
+
function parseAcceptLanguage(header) {
|
|
47
|
+
return header.split(",").map((entry) => {
|
|
48
|
+
const [locale, quality] = entry.trim().split(";");
|
|
49
|
+
const qPart = quality?.split("=")[1];
|
|
50
|
+
const q = qPart !== void 0 ? parseFloat(qPart) : 1;
|
|
51
|
+
return {
|
|
52
|
+
locale: locale.trim(),
|
|
53
|
+
quality: isNaN(q) ? 1 : q
|
|
54
|
+
};
|
|
55
|
+
}).sort((a, b) => b.quality - a.quality).map((entry) => entry.locale);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Resolve the preferred locale from the request Accept-Language header, fallback to the default locale if no match is found
|
|
59
|
+
* @param request - The request object
|
|
60
|
+
* @returns The preferred locale
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* const locale = getRequestLocale({ headers: { 'accept-language': 'fr-FR,fr;q=0.9,en;q=0.8' } });
|
|
64
|
+
* console.log(locale); // 'fr'
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* app.get('/', (req, res) => {
|
|
68
|
+
* const locale = getRequestLocale(req);
|
|
69
|
+
* withGT(locale, () => {
|
|
70
|
+
* res.send(`Locale: ${locale}`);
|
|
71
|
+
* });
|
|
72
|
+
* });
|
|
73
|
+
*/
|
|
74
|
+
function getRequestLocale(request) {
|
|
75
|
+
const i18nManager = require_AsyncStorageAdapter.getI18nManager();
|
|
76
|
+
const defaultLocale = i18nManager.getDefaultLocale();
|
|
77
|
+
const gtInstance = i18nManager.getGTClass();
|
|
78
|
+
const acceptLanguage = request.headers["accept-language"];
|
|
79
|
+
const headerValue = Array.isArray(acceptLanguage) ? acceptLanguage[0] : acceptLanguage;
|
|
80
|
+
if (!headerValue) return defaultLocale;
|
|
81
|
+
const preferredLocales = parseAcceptLanguage(headerValue);
|
|
82
|
+
return gtInstance.determineLocale(preferredLocales) || defaultLocale;
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
Object.defineProperty(exports, "declareStatic", {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
get: function() {
|
|
88
|
+
return gt_i18n.declareStatic;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
Object.defineProperty(exports, "declareVar", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
get: function() {
|
|
94
|
+
return gt_i18n.declareVar;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
Object.defineProperty(exports, "decodeMsg", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
get: function() {
|
|
100
|
+
return gt_i18n.decodeMsg;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
Object.defineProperty(exports, "decodeOptions", {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
get: function() {
|
|
106
|
+
return gt_i18n.decodeOptions;
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
Object.defineProperty(exports, "decodeVars", {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
get: function() {
|
|
112
|
+
return gt_i18n.decodeVars;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
Object.defineProperty(exports, "derive", {
|
|
116
|
+
enumerable: true,
|
|
117
|
+
get: function() {
|
|
118
|
+
return gt_i18n.derive;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
Object.defineProperty(exports, "getDefaultLocale", {
|
|
122
|
+
enumerable: true,
|
|
123
|
+
get: function() {
|
|
124
|
+
return gt_i18n.getDefaultLocale;
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
Object.defineProperty(exports, "getGT", {
|
|
128
|
+
enumerable: true,
|
|
129
|
+
get: function() {
|
|
130
|
+
return gt_i18n_internal.getGT;
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
Object.defineProperty(exports, "getLocale", {
|
|
134
|
+
enumerable: true,
|
|
135
|
+
get: function() {
|
|
136
|
+
return gt_i18n.getLocale;
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
Object.defineProperty(exports, "getLocaleProperties", {
|
|
140
|
+
enumerable: true,
|
|
141
|
+
get: function() {
|
|
142
|
+
return gt_i18n.getLocaleProperties;
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
Object.defineProperty(exports, "getLocales", {
|
|
146
|
+
enumerable: true,
|
|
147
|
+
get: function() {
|
|
148
|
+
return gt_i18n.getLocales;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
Object.defineProperty(exports, "getMessages", {
|
|
152
|
+
enumerable: true,
|
|
153
|
+
get: function() {
|
|
154
|
+
return gt_i18n_internal.getMessages;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
exports.getRequestLocale = getRequestLocale;
|
|
158
|
+
Object.defineProperty(exports, "getVersionId", {
|
|
159
|
+
enumerable: true,
|
|
160
|
+
get: function() {
|
|
161
|
+
return gt_i18n.getVersionId;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
exports.initializeGT = initializeGT;
|
|
165
|
+
Object.defineProperty(exports, "msg", {
|
|
166
|
+
enumerable: true,
|
|
167
|
+
get: function() {
|
|
168
|
+
return gt_i18n.msg;
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
Object.defineProperty(exports, "tx", {
|
|
172
|
+
enumerable: true,
|
|
173
|
+
get: function() {
|
|
174
|
+
return gt_i18n_internal.tx;
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
exports.withGT = withGT;
|
|
178
|
+
|
|
179
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["AsyncStorageI18nManager","AsyncStorageAdapter","getI18nManager","getI18nManager"],"sources":["../src/setup/initializeGT.ts","../src/setup/withGT.ts","../src/helpers/getRequestLocale.ts"],"sourcesContent":["import { setI18nManager } from '../async-i18n-manager/singleton-operations';\nimport type { InitializeGTParams } from './types';\nimport { AsyncStorageI18nManager } from '../async-i18n-manager/AsyncStorageI18nManager';\nimport { AsyncStorageAdapter } from '../async-i18n-manager/AsyncStorageAdapter';\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 const i18nManager = new AsyncStorageI18nManager({\n ...params,\n storeAdapter: new AsyncStorageAdapter(),\n });\n\n setI18nManager(i18nManager);\n}\n","import { getI18nManager } from '../async-i18n-manager/singleton-operations';\nimport { ASYNC_STORAGE_ADAPTER_TYPE } from '../async-i18n-manager/AsyncStorageAdapter';\nimport { I18nManager } from 'gt-i18n/internal';\nimport { AsyncStorageI18nManager } from '../async-i18n-manager/AsyncStorageI18nManager';\n\n/**\n * This function wraps entry points to provide GT context\n */\nexport function withGT<T>(locale: string, fn: () => T): T {\n const i18nManager = getI18nManager();\n if (!isAsyncStorageI18nManager(i18nManager)) {\n throw new Error(\n 'I18nManager not initialized. Invoke configGT() to initialize.'\n );\n }\n\n return i18nManager.run<T>(locale, fn);\n}\n\n// ========== Helper functions ========== //\n\n/**\n * Check if the I18nManager is an AsyncStorageI18nManager\n * @param i18nManager - The I18nManager instance\n * @returns True if the I18nManager is an AsyncStorageI18nManager, false otherwise\n */\nfunction isAsyncStorageI18nManager(\n i18nManager: I18nManager\n): i18nManager is AsyncStorageI18nManager {\n return (\n i18nManager.getAdapterType() === ASYNC_STORAGE_ADAPTER_TYPE &&\n 'run' in i18nManager &&\n typeof i18nManager.run === 'function'\n );\n}\n","import { getI18nManager } from '../async-i18n-manager/singleton-operations';\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 * Parse the Accept-Language header into an array of locales\n * @param header - The Accept-Language header\n * @returns An array of locales\n *\n * @example\n * const locales = parseAcceptLanguage('fr-FR,fr;q=0.9,en;q=0.8');\n * console.log(locales); // ['fr-FR', 'fr', 'en']\n */\nfunction parseAcceptLanguage(header: string): string[] {\n return header\n .split(',')\n .map((entry) => {\n const [locale, quality] = entry.trim().split(';');\n const qPart = quality?.split('=')[1];\n const q = qPart !== undefined ? parseFloat(qPart) : 1;\n return {\n locale: locale.trim(),\n quality: isNaN(q) ? 1 : q,\n };\n })\n .sort((a, b) => b.quality - a.quality)\n .map((entry) => entry.locale);\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 i18nManager = getI18nManager();\n const defaultLocale = i18nManager.getDefaultLocale();\n const gtInstance = i18nManager.getGTClass();\n\n // Get the accept-language header\n const acceptLanguage = request.headers['accept-language'];\n const headerValue = Array.isArray(acceptLanguage)\n ? acceptLanguage[0]\n : acceptLanguage;\n if (!headerValue) return defaultLocale;\n\n // Parse the accept-language header\n const preferredLocales = parseAcceptLanguage(headerValue);\n return gtInstance.determineLocale(preferredLocales) || defaultLocale;\n}\n"],"mappings":";;;;;;;;;;AAUA,SAAgB,aAAa,QAAkC;AAM7D,6BAAA,eAAe,IALSA,4BAAAA,wBAAwB;EAC9C,GAAG;EACH,cAAc,IAAIC,4BAAAA,qBAAqB;EACxC,CAEyB,CAAC;;;;;;;ACR7B,SAAgB,OAAU,QAAgB,IAAgB;CACxD,MAAM,cAAcC,4BAAAA,gBAAgB;AACpC,KAAI,CAAC,0BAA0B,YAAY,CACzC,OAAM,IAAI,MACR,gEACD;AAGH,QAAO,YAAY,IAAO,QAAQ,GAAG;;;;;;;AAUvC,SAAS,0BACP,aACwC;AACxC,QACE,YAAY,gBAAgB,KAAA,2BAC5B,SAAS,eACT,OAAO,YAAY,QAAQ;;;;;;;;;;;;;ACZ/B,SAAS,oBAAoB,QAA0B;AACrD,QAAO,OACJ,MAAM,IAAI,CACV,KAAK,UAAU;EACd,MAAM,CAAC,QAAQ,WAAW,MAAM,MAAM,CAAC,MAAM,IAAI;EACjD,MAAM,QAAQ,SAAS,MAAM,IAAI,CAAC;EAClC,MAAM,IAAI,UAAU,KAAA,IAAY,WAAW,MAAM,GAAG;AACpD,SAAO;GACL,QAAQ,OAAO,MAAM;GACrB,SAAS,MAAM,EAAE,GAAG,IAAI;GACzB;GACD,CACD,MAAM,GAAG,MAAM,EAAE,UAAU,EAAE,QAAQ,CACrC,KAAK,UAAU,MAAM,OAAO;;;;;;;;;;;;;;;;;;;AAoBjC,SAAgB,iBAAiB,SAA8B;CAE7D,MAAM,cAAcC,4BAAAA,gBAAgB;CACpC,MAAM,gBAAgB,YAAY,kBAAkB;CACpD,MAAM,aAAa,YAAY,YAAY;CAG3C,MAAM,iBAAiB,QAAQ,QAAQ;CACvC,MAAM,cAAc,MAAM,QAAQ,eAAe,GAC7C,eAAe,KACf;AACJ,KAAI,CAAC,YAAa,QAAO;CAGzB,MAAM,mBAAmB,oBAAoB,YAAY;AACzD,QAAO,WAAW,gBAAgB,iBAAiB,IAAI"}
|
|
@@ -1,36 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Parameters for the initializing GT
|
|
7
|
-
* @param {string} params.defaultLocale - The default locale to use
|
|
8
|
-
* @param {string[]} params.locales - The locales to support
|
|
9
|
-
* @param {object} params.loadTranslations - The custom translation loader to use
|
|
10
|
-
*/
|
|
11
|
-
type InitializeGTParams = GTConfig & {
|
|
12
|
-
loadTranslations?: TranslationsLoader;
|
|
13
|
-
};
|
|
1
|
+
import { n as InitializeGTParams } from "./types-Ctyskm8X.cjs";
|
|
2
|
+
import { getGT, getMessages, tx } from "gt-i18n/internal";
|
|
3
|
+
import { declareStatic, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getDefaultLocale, getLocale, getLocaleProperties, getLocales, getVersionId, msg } from "gt-i18n";
|
|
14
4
|
|
|
5
|
+
//#region src/setup/initializeGT.d.ts
|
|
15
6
|
/**
|
|
16
7
|
* Configure GT for node runtime. This must be called to setup GT for node runtime.
|
|
17
8
|
* @param {InitializeGTParams} config - The configuration for the GT instance
|
|
18
9
|
* TODO: auto detect if can find gt.config.json files
|
|
19
10
|
*/
|
|
20
11
|
declare function initializeGT(params: InitializeGTParams): void;
|
|
21
|
-
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/setup/withGT.d.ts
|
|
22
14
|
/**
|
|
23
15
|
* This function wraps entry points to provide GT context
|
|
24
16
|
*/
|
|
25
17
|
declare function withGT<T>(locale: string, fn: () => T): T;
|
|
26
|
-
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/helpers/getRequestLocale.d.ts
|
|
27
20
|
/**
|
|
28
21
|
* A request object like interface
|
|
29
22
|
* @interface RequestLike
|
|
30
23
|
* @property {Record<string, string | string[] | undefined>} headers - The request headers
|
|
31
24
|
*/
|
|
32
25
|
interface RequestLike {
|
|
33
|
-
|
|
26
|
+
headers: Record<string, string | string[] | undefined>;
|
|
34
27
|
}
|
|
35
28
|
/**
|
|
36
29
|
* Resolve the preferred locale from the request Accept-Language header, fallback to the default locale if no match is found
|
|
@@ -50,5 +43,6 @@ interface RequestLike {
|
|
|
50
43
|
* });
|
|
51
44
|
*/
|
|
52
45
|
declare function getRequestLocale(request: RequestLike): string;
|
|
53
|
-
|
|
54
|
-
export { getRequestLocale, initializeGT, withGT };
|
|
46
|
+
//#endregion
|
|
47
|
+
export { declareStatic, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getDefaultLocale, getGT, getLocale, getLocaleProperties, getLocales, getMessages, getRequestLocale, getVersionId, initializeGT, msg, tx, withGT };
|
|
48
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { n as InitializeGTParams } from "./types-BA-INDkb.mjs";
|
|
2
|
+
import { getGT, getMessages, tx } from "gt-i18n/internal";
|
|
3
|
+
import { declareStatic, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getDefaultLocale, getLocale, getLocaleProperties, getLocales, getVersionId, msg } from "gt-i18n";
|
|
4
|
+
|
|
5
|
+
//#region src/setup/initializeGT.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Configure GT for node runtime. This must be called to setup GT for node runtime.
|
|
8
|
+
* @param {InitializeGTParams} config - The configuration for the GT instance
|
|
9
|
+
* TODO: auto detect if can find gt.config.json files
|
|
10
|
+
*/
|
|
11
|
+
declare function initializeGT(params: InitializeGTParams): void;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/setup/withGT.d.ts
|
|
14
|
+
/**
|
|
15
|
+
* This function wraps entry points to provide GT context
|
|
16
|
+
*/
|
|
17
|
+
declare function withGT<T>(locale: string, fn: () => T): T;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/helpers/getRequestLocale.d.ts
|
|
20
|
+
/**
|
|
21
|
+
* A request object like interface
|
|
22
|
+
* @interface RequestLike
|
|
23
|
+
* @property {Record<string, string | string[] | undefined>} headers - The request headers
|
|
24
|
+
*/
|
|
25
|
+
interface RequestLike {
|
|
26
|
+
headers: Record<string, string | string[] | undefined>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Resolve the preferred locale from the request Accept-Language header, fallback to the default locale if no match is found
|
|
30
|
+
* @param request - The request object
|
|
31
|
+
* @returns The preferred locale
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* const locale = getRequestLocale({ headers: { 'accept-language': 'fr-FR,fr;q=0.9,en;q=0.8' } });
|
|
35
|
+
* console.log(locale); // 'fr'
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* app.get('/', (req, res) => {
|
|
39
|
+
* const locale = getRequestLocale(req);
|
|
40
|
+
* withGT(locale, () => {
|
|
41
|
+
* res.send(`Locale: ${locale}`);
|
|
42
|
+
* });
|
|
43
|
+
* });
|
|
44
|
+
*/
|
|
45
|
+
declare function getRequestLocale(request: RequestLike): string;
|
|
46
|
+
//#endregion
|
|
47
|
+
export { declareStatic, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getDefaultLocale, getGT, getLocale, getLocaleProperties, getLocales, getMessages, getRequestLocale, getVersionId, initializeGT, msg, tx, withGT };
|
|
48
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { a as setI18nManager$1, i as getI18nManager$1, n as AsyncStorageAdapter, r as AsyncStorageI18nManager } from "./AsyncStorageAdapter-D852UaVa.mjs";
|
|
2
|
+
import { getGT, getMessages, tx } from "gt-i18n/internal";
|
|
3
|
+
import { declareStatic, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getDefaultLocale, getLocale, getLocaleProperties, getLocales, getVersionId, msg } from "gt-i18n";
|
|
4
|
+
//#region src/setup/initializeGT.ts
|
|
5
|
+
/**
|
|
6
|
+
* Configure GT for node runtime. This must be called to setup GT for node runtime.
|
|
7
|
+
* @param {InitializeGTParams} config - The configuration for the GT instance
|
|
8
|
+
* TODO: auto detect if can find gt.config.json files
|
|
9
|
+
*/
|
|
10
|
+
function initializeGT(params) {
|
|
11
|
+
setI18nManager$1(new AsyncStorageI18nManager({
|
|
12
|
+
...params,
|
|
13
|
+
storeAdapter: new AsyncStorageAdapter()
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/setup/withGT.ts
|
|
18
|
+
/**
|
|
19
|
+
* This function wraps entry points to provide GT context
|
|
20
|
+
*/
|
|
21
|
+
function withGT(locale, fn) {
|
|
22
|
+
const i18nManager = getI18nManager$1();
|
|
23
|
+
if (!isAsyncStorageI18nManager(i18nManager)) throw new Error("I18nManager not initialized. Invoke configGT() to initialize.");
|
|
24
|
+
return i18nManager.run(locale, fn);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Check if the I18nManager is an AsyncStorageI18nManager
|
|
28
|
+
* @param i18nManager - The I18nManager instance
|
|
29
|
+
* @returns True if the I18nManager is an AsyncStorageI18nManager, false otherwise
|
|
30
|
+
*/
|
|
31
|
+
function isAsyncStorageI18nManager(i18nManager) {
|
|
32
|
+
return i18nManager.getAdapterType() === "async-storage-adapter" && "run" in i18nManager && typeof i18nManager.run === "function";
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/helpers/getRequestLocale.ts
|
|
36
|
+
/**
|
|
37
|
+
* Parse the Accept-Language header into an array of locales
|
|
38
|
+
* @param header - The Accept-Language header
|
|
39
|
+
* @returns An array of locales
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const locales = parseAcceptLanguage('fr-FR,fr;q=0.9,en;q=0.8');
|
|
43
|
+
* console.log(locales); // ['fr-FR', 'fr', 'en']
|
|
44
|
+
*/
|
|
45
|
+
function parseAcceptLanguage(header) {
|
|
46
|
+
return header.split(",").map((entry) => {
|
|
47
|
+
const [locale, quality] = entry.trim().split(";");
|
|
48
|
+
const qPart = quality?.split("=")[1];
|
|
49
|
+
const q = qPart !== void 0 ? parseFloat(qPart) : 1;
|
|
50
|
+
return {
|
|
51
|
+
locale: locale.trim(),
|
|
52
|
+
quality: isNaN(q) ? 1 : q
|
|
53
|
+
};
|
|
54
|
+
}).sort((a, b) => b.quality - a.quality).map((entry) => entry.locale);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Resolve the preferred locale from the request Accept-Language header, fallback to the default locale if no match is found
|
|
58
|
+
* @param request - The request object
|
|
59
|
+
* @returns The preferred locale
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* const locale = getRequestLocale({ headers: { 'accept-language': 'fr-FR,fr;q=0.9,en;q=0.8' } });
|
|
63
|
+
* console.log(locale); // 'fr'
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* app.get('/', (req, res) => {
|
|
67
|
+
* const locale = getRequestLocale(req);
|
|
68
|
+
* withGT(locale, () => {
|
|
69
|
+
* res.send(`Locale: ${locale}`);
|
|
70
|
+
* });
|
|
71
|
+
* });
|
|
72
|
+
*/
|
|
73
|
+
function getRequestLocale(request) {
|
|
74
|
+
const i18nManager = getI18nManager$1();
|
|
75
|
+
const defaultLocale = i18nManager.getDefaultLocale();
|
|
76
|
+
const gtInstance = i18nManager.getGTClass();
|
|
77
|
+
const acceptLanguage = request.headers["accept-language"];
|
|
78
|
+
const headerValue = Array.isArray(acceptLanguage) ? acceptLanguage[0] : acceptLanguage;
|
|
79
|
+
if (!headerValue) return defaultLocale;
|
|
80
|
+
const preferredLocales = parseAcceptLanguage(headerValue);
|
|
81
|
+
return gtInstance.determineLocale(preferredLocales) || defaultLocale;
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
export { declareStatic, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getDefaultLocale, getGT, getLocale, getLocaleProperties, getLocales, getMessages, getRequestLocale, getVersionId, initializeGT, msg, tx, withGT };
|
|
85
|
+
|
|
86
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["getI18nManager","getI18nManager"],"sources":["../src/setup/initializeGT.ts","../src/setup/withGT.ts","../src/helpers/getRequestLocale.ts"],"sourcesContent":["import { setI18nManager } from '../async-i18n-manager/singleton-operations';\nimport type { InitializeGTParams } from './types';\nimport { AsyncStorageI18nManager } from '../async-i18n-manager/AsyncStorageI18nManager';\nimport { AsyncStorageAdapter } from '../async-i18n-manager/AsyncStorageAdapter';\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 const i18nManager = new AsyncStorageI18nManager({\n ...params,\n storeAdapter: new AsyncStorageAdapter(),\n });\n\n setI18nManager(i18nManager);\n}\n","import { getI18nManager } from '../async-i18n-manager/singleton-operations';\nimport { ASYNC_STORAGE_ADAPTER_TYPE } from '../async-i18n-manager/AsyncStorageAdapter';\nimport { I18nManager } from 'gt-i18n/internal';\nimport { AsyncStorageI18nManager } from '../async-i18n-manager/AsyncStorageI18nManager';\n\n/**\n * This function wraps entry points to provide GT context\n */\nexport function withGT<T>(locale: string, fn: () => T): T {\n const i18nManager = getI18nManager();\n if (!isAsyncStorageI18nManager(i18nManager)) {\n throw new Error(\n 'I18nManager not initialized. Invoke configGT() to initialize.'\n );\n }\n\n return i18nManager.run<T>(locale, fn);\n}\n\n// ========== Helper functions ========== //\n\n/**\n * Check if the I18nManager is an AsyncStorageI18nManager\n * @param i18nManager - The I18nManager instance\n * @returns True if the I18nManager is an AsyncStorageI18nManager, false otherwise\n */\nfunction isAsyncStorageI18nManager(\n i18nManager: I18nManager\n): i18nManager is AsyncStorageI18nManager {\n return (\n i18nManager.getAdapterType() === ASYNC_STORAGE_ADAPTER_TYPE &&\n 'run' in i18nManager &&\n typeof i18nManager.run === 'function'\n );\n}\n","import { getI18nManager } from '../async-i18n-manager/singleton-operations';\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 * Parse the Accept-Language header into an array of locales\n * @param header - The Accept-Language header\n * @returns An array of locales\n *\n * @example\n * const locales = parseAcceptLanguage('fr-FR,fr;q=0.9,en;q=0.8');\n * console.log(locales); // ['fr-FR', 'fr', 'en']\n */\nfunction parseAcceptLanguage(header: string): string[] {\n return header\n .split(',')\n .map((entry) => {\n const [locale, quality] = entry.trim().split(';');\n const qPart = quality?.split('=')[1];\n const q = qPart !== undefined ? parseFloat(qPart) : 1;\n return {\n locale: locale.trim(),\n quality: isNaN(q) ? 1 : q,\n };\n })\n .sort((a, b) => b.quality - a.quality)\n .map((entry) => entry.locale);\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 i18nManager = getI18nManager();\n const defaultLocale = i18nManager.getDefaultLocale();\n const gtInstance = i18nManager.getGTClass();\n\n // Get the accept-language header\n const acceptLanguage = request.headers['accept-language'];\n const headerValue = Array.isArray(acceptLanguage)\n ? acceptLanguage[0]\n : acceptLanguage;\n if (!headerValue) return defaultLocale;\n\n // Parse the accept-language header\n const preferredLocales = parseAcceptLanguage(headerValue);\n return gtInstance.determineLocale(preferredLocales) || defaultLocale;\n}\n"],"mappings":";;;;;;;;;AAUA,SAAgB,aAAa,QAAkC;AAM7D,kBAAe,IALS,wBAAwB;EAC9C,GAAG;EACH,cAAc,IAAI,qBAAqB;EACxC,CAEyB,CAAC;;;;;;;ACR7B,SAAgB,OAAU,QAAgB,IAAgB;CACxD,MAAM,cAAcA,kBAAgB;AACpC,KAAI,CAAC,0BAA0B,YAAY,CACzC,OAAM,IAAI,MACR,gEACD;AAGH,QAAO,YAAY,IAAO,QAAQ,GAAG;;;;;;;AAUvC,SAAS,0BACP,aACwC;AACxC,QACE,YAAY,gBAAgB,KAAA,2BAC5B,SAAS,eACT,OAAO,YAAY,QAAQ;;;;;;;;;;;;;ACZ/B,SAAS,oBAAoB,QAA0B;AACrD,QAAO,OACJ,MAAM,IAAI,CACV,KAAK,UAAU;EACd,MAAM,CAAC,QAAQ,WAAW,MAAM,MAAM,CAAC,MAAM,IAAI;EACjD,MAAM,QAAQ,SAAS,MAAM,IAAI,CAAC;EAClC,MAAM,IAAI,UAAU,KAAA,IAAY,WAAW,MAAM,GAAG;AACpD,SAAO;GACL,QAAQ,OAAO,MAAM;GACrB,SAAS,MAAM,EAAE,GAAG,IAAI;GACzB;GACD,CACD,MAAM,GAAG,MAAM,EAAE,UAAU,EAAE,QAAQ,CACrC,KAAK,UAAU,MAAM,OAAO;;;;;;;;;;;;;;;;;;;AAoBjC,SAAgB,iBAAiB,SAA8B;CAE7D,MAAM,cAAcC,kBAAgB;CACpC,MAAM,gBAAgB,YAAY,kBAAkB;CACpD,MAAM,aAAa,YAAY,YAAY;CAG3C,MAAM,iBAAiB,QAAQ,QAAQ;CACvC,MAAM,cAAc,MAAM,QAAQ,eAAe,GAC7C,eAAe,KACf;AACJ,KAAI,CAAC,YAAa,QAAO;CAGzB,MAAM,mBAAmB,oBAAoB,YAAY;AACzD,QAAO,WAAW,gBAAgB,iBAAiB,IAAI"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_AsyncStorageAdapter = require("./AsyncStorageAdapter-xUXP4qma.cjs");
|
|
3
|
+
let gt_i18n_internal = require("gt-i18n/internal");
|
|
4
|
+
exports.AsyncStorageAdapter = require_AsyncStorageAdapter.AsyncStorageAdapter;
|
|
5
|
+
exports.AsyncStorageI18nManager = require_AsyncStorageAdapter.AsyncStorageI18nManager;
|
|
6
|
+
Object.defineProperty(exports, "I18nManager", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function() {
|
|
9
|
+
return gt_i18n_internal.I18nManager;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "StorageAdapter", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function() {
|
|
15
|
+
return gt_i18n_internal.StorageAdapter;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
exports.asyncStorageGetI18nManager = require_AsyncStorageAdapter.getI18nManager;
|
|
19
|
+
exports.asyncStorageSetI18nManager = require_AsyncStorageAdapter.setI18nManager;
|
|
20
|
+
Object.defineProperty(exports, "getI18nManager", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function() {
|
|
23
|
+
return gt_i18n_internal.getI18nManager;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(exports, "setI18nManager", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
get: function() {
|
|
29
|
+
return gt_i18n_internal.setI18nManager;
|
|
30
|
+
}
|
|
31
|
+
});
|