@owlmeans/i18n 0.1.3 → 0.1.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/build/consts.d.ts +6 -4
- package/build/consts.d.ts.map +1 -1
- package/build/consts.js +8 -7
- package/build/consts.js.map +1 -1
- package/build/helper.d.ts +0 -1
- package/build/helper.d.ts.map +1 -1
- package/build/helper.js +12 -16
- package/build/helper.js.map +1 -1
- package/build/types.d.ts +6 -4
- package/build/types.d.ts.map +1 -1
- package/build/utils/consts.d.ts +1 -2
- package/build/utils/consts.d.ts.map +1 -1
- package/build/utils/consts.js +4 -5
- package/build/utils/consts.js.map +1 -1
- package/package.json +2 -1
- package/src/consts.ts +7 -3
- package/src/helper.ts +12 -17
- package/src/types.ts +6 -4
- package/src/utils/consts.ts +4 -5
- package/tests/context.d.ts +3 -0
- package/tests/context.d.ts.map +1 -0
- package/tests/context.js +6 -0
- package/tests/context.js.map +1 -0
- package/tests/context.ts +6 -0
- package/tests/registration.spec.d.ts +2 -0
- package/tests/registration.spec.d.ts.map +1 -0
- package/tests/registration.spec.js +69 -0
- package/tests/registration.spec.js.map +1 -0
- package/tests/registration.spec.ts +81 -0
- package/tsconfig.json +1 -1
package/build/consts.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export declare const DEFAULT_NAMESPACE = "translation";
|
|
2
2
|
export declare const LIB_NAMESPACE = "lib";
|
|
3
3
|
export declare const DEFAULT_LNG = "en";
|
|
4
|
-
export declare const
|
|
4
|
+
export declare const SUPPORTED_LNGS: readonly ["en", "pl", "ru", "be", "uk", "es", "de"];
|
|
5
|
+
export type SupportedLng = (typeof SUPPORTED_LNGS)[number];
|
|
5
6
|
export declare const MAX_PRIORITY: number;
|
|
6
|
-
export declare enum
|
|
7
|
+
export declare enum I18nTier {
|
|
7
8
|
Library = "library",
|
|
8
|
-
App = "app"
|
|
9
|
-
Service = "service"
|
|
9
|
+
App = "app"
|
|
10
10
|
}
|
|
11
|
+
/** @deprecated use I18nTier */
|
|
12
|
+
export declare const I18nLevel: typeof I18nTier;
|
|
11
13
|
//# sourceMappingURL=consts.d.ts.map
|
package/build/consts.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,iBAAiB,gBAAgB,CAAA;AAE9C,eAAO,MAAM,aAAa,QAAQ,CAAA;AAElC,eAAO,MAAM,WAAW,OAAO,CAAA;AAE/B,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,iBAAiB,gBAAgB,CAAA;AAE9C,eAAO,MAAM,aAAa,QAAQ,CAAA;AAElC,eAAO,MAAM,WAAW,OAAO,CAAA;AAE/B,eAAO,MAAM,cAAc,qDAAsD,CAAA;AAEjF,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAA;AAE1D,eAAO,MAAM,YAAY,QAA0B,CAAA;AAEnD,oBAAY,QAAQ;IAClB,OAAO,YAAY;IACnB,GAAG,QAAQ;CACZ;AAED,+BAA+B;AAC/B,eAAO,MAAM,SAAS,iBAAW,CAAA"}
|
package/build/consts.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
export const DEFAULT_NAMESPACE = 'translation';
|
|
2
2
|
export const LIB_NAMESPACE = 'lib';
|
|
3
3
|
export const DEFAULT_LNG = 'en';
|
|
4
|
-
export const
|
|
4
|
+
export const SUPPORTED_LNGS = ['en', 'pl', 'ru', 'be', 'uk', 'es', 'de'];
|
|
5
5
|
export const MAX_PRIORITY = Number.MAX_SAFE_INTEGER;
|
|
6
|
-
export var
|
|
7
|
-
(function (
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
export var I18nTier;
|
|
7
|
+
(function (I18nTier) {
|
|
8
|
+
I18nTier["Library"] = "library";
|
|
9
|
+
I18nTier["App"] = "app";
|
|
10
|
+
})(I18nTier || (I18nTier = {}));
|
|
11
|
+
/** @deprecated use I18nTier */
|
|
12
|
+
export const I18nLevel = I18nTier;
|
|
12
13
|
//# sourceMappingURL=consts.js.map
|
package/build/consts.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,iBAAiB,GAAG,aAAa,CAAA;AAE9C,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,CAAA;AAElC,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,CAAA;AAE/B,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,iBAAiB,GAAG,aAAa,CAAA;AAE9C,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,CAAA;AAElC,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,CAAA;AAE/B,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAU,CAAA;AAIjF,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,gBAAgB,CAAA;AAEnD,MAAM,CAAN,IAAY,QAGX;AAHD,WAAY,QAAQ;IAClB,+BAAmB,CAAA;IACnB,uBAAW,CAAA;AACb,CAAC,EAHW,QAAQ,KAAR,QAAQ,QAGnB;AAED,+BAA+B;AAC/B,MAAM,CAAC,MAAM,SAAS,GAAG,QAAQ,CAAA"}
|
package/build/helper.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { I18nLeveledResourceSignature, I18nResource } from './types.js';
|
|
2
2
|
export declare const addI18nLib: I18nLeveledResourceSignature;
|
|
3
3
|
export declare const addI18nApp: I18nLeveledResourceSignature;
|
|
4
|
-
export declare const addCommonI18n: I18nLeveledResourceSignature;
|
|
5
4
|
export declare const initI18nResource: (lng: string, resource: string, ns?: string) => null | I18nResource[];
|
|
6
5
|
//# sourceMappingURL=helper.d.ts.map
|
package/build/helper.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,4BAA4B,EAAE,YAAY,EAA8C,MAAM,YAAY,CAAA;AA6BxH,eAAO,MAAM,UAAU,EAAE,4BAGxB,CAAA;AAED,eAAO,MAAM,UAAU,EAAE,4BAGxB,CAAA;AAED,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,4BAA4B,EAAE,YAAY,EAA8C,MAAM,YAAY,CAAA;AA6BxH,eAAO,MAAM,UAAU,EAAE,4BAGxB,CAAA;AAED,eAAO,MAAM,UAAU,EAAE,4BAGxB,CAAA;AAED,eAAO,MAAM,gBAAgB,GAAI,KAAK,MAAM,EAAE,UAAU,MAAM,EAAE,KAAK,MAAM,KAAG,IAAI,GAAG,YAAY,EAoBhG,CAAA"}
|
package/build/helper.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DEFAULT_NAMESPACE,
|
|
2
|
-
import {
|
|
1
|
+
import { DEFAULT_NAMESPACE, I18nTier, LIB_NAMESPACE, MAX_PRIORITY } from './consts.js';
|
|
2
|
+
import { tierCost } from './utils/consts.js';
|
|
3
3
|
import { ensureStructure } from './utils/storage.js';
|
|
4
4
|
const prepareOptions = (opts, ns) => {
|
|
5
5
|
if (typeof opts === 'string') {
|
|
@@ -13,26 +13,22 @@ const prepareOptions = (opts, ns) => {
|
|
|
13
13
|
}
|
|
14
14
|
return opts;
|
|
15
15
|
};
|
|
16
|
-
const _addI18n = (
|
|
16
|
+
const _addI18n = (tier, lng, resource, data, opts) => {
|
|
17
17
|
opts = prepareOptions(opts);
|
|
18
18
|
const storage = ensureStructure(lng, resource, opts.ns);
|
|
19
19
|
const ns = opts.ns ?? DEFAULT_NAMESPACE;
|
|
20
|
-
const
|
|
21
|
-
ns, lng,
|
|
20
|
+
const entry = {
|
|
21
|
+
ns, lng, tier, resource, data, priority: opts.priority
|
|
22
22
|
};
|
|
23
|
-
storage.resources.push(
|
|
23
|
+
storage.resources.push(entry);
|
|
24
24
|
};
|
|
25
25
|
export const addI18nLib = (lng, resource, data, opts) => {
|
|
26
26
|
opts = prepareOptions(opts, LIB_NAMESPACE);
|
|
27
|
-
_addI18n(
|
|
27
|
+
_addI18n(I18nTier.Library, lng, resource, data, opts);
|
|
28
28
|
};
|
|
29
29
|
export const addI18nApp = (lng, resource, data, opts) => {
|
|
30
30
|
opts = prepareOptions(opts, resource);
|
|
31
|
-
_addI18n(
|
|
32
|
-
};
|
|
33
|
-
export const addCommonI18n = (lng, resource, data, opts) => {
|
|
34
|
-
opts = prepareOptions(opts);
|
|
35
|
-
_addI18n(I18nLevel.Service, lng, resource, data, opts);
|
|
31
|
+
_addI18n(I18nTier.App, lng, resource, data, opts);
|
|
36
32
|
};
|
|
37
33
|
export const initI18nResource = (lng, resource, ns) => {
|
|
38
34
|
ns = ns ?? DEFAULT_NAMESPACE;
|
|
@@ -41,10 +37,10 @@ export const initI18nResource = (lng, resource, ns) => {
|
|
|
41
37
|
return null;
|
|
42
38
|
}
|
|
43
39
|
const result = [...translation.resources].sort((a, b) => {
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
if (
|
|
47
|
-
return
|
|
40
|
+
const aTier = tierCost[a.tier];
|
|
41
|
+
const bTier = tierCost[b.tier];
|
|
42
|
+
if (aTier !== bTier) {
|
|
43
|
+
return aTier - bTier;
|
|
48
44
|
}
|
|
49
45
|
return (a.priority ?? MAX_PRIORITY) - (b.priority ?? MAX_PRIORITY);
|
|
50
46
|
});
|
package/build/helper.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAEtF,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAEpD,MAAM,cAAc,GAAyF,CAAC,IAAI,EAAE,EAAE,EAAE,EAAE;IACxH,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,IAAI,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,CAAA;IACrB,CAAC;SAAM,IAAI,OAAO,IAAI,KAAK,WAAW,EAAE,CAAC;QACvC,IAAI,GAAG,EAAE,CAAA;IACX,CAAC;IACD,IAAI,IAAI,CAAC,EAAE,IAAI,IAAI,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC;QAClC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAA;IACd,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC,CAAA;AAED,MAAM,QAAQ,GAA0B,CAAC,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE;IAC1E,IAAI,GAAG,cAAc,CAAC,IAAI,CAAC,CAAA;IAC3B,MAAM,OAAO,GAAG,eAAe,CAAC,GAAG,EAAE,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAC,CAAA;IAEvD,MAAM,EAAE,GAAG,IAAI,CAAC,EAAE,IAAI,iBAAiB,CAAA;IAEvC,MAAM,KAAK,GAAiB;QAC1B,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C,CAAA;QAC9B,MAAM,KAAK,GAAG,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAA;QAC9B,IAAI,KAAK,KAAK,KAAK,EAAE,CAAC;YACpB,OAAO,KAAK,GAAG,KAAK,CAAA;QACtB,CAAC;QAED,OAAO,CAAC,CAAC,CAAC,QAAQ,IAAI,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,IAAI,YAAY,CAAC,CAAA;IACpE,CAAC,CAAC,CAAA;IAEF,WAAW,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IAEpC,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
|
package/build/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { I18nTier } from './consts.js';
|
|
2
2
|
export interface I18nStorage {
|
|
3
3
|
data: I18nNamespaces;
|
|
4
4
|
}
|
|
@@ -14,17 +14,17 @@ export interface I18nLanguages extends Record<string, {
|
|
|
14
14
|
export interface I18nResource {
|
|
15
15
|
ns?: string;
|
|
16
16
|
lng?: string;
|
|
17
|
-
|
|
17
|
+
tier: I18nTier;
|
|
18
18
|
resource: string;
|
|
19
19
|
priority?: number;
|
|
20
20
|
data: Record<string, any>;
|
|
21
21
|
}
|
|
22
22
|
export interface I18nResourceOptions {
|
|
23
|
-
|
|
23
|
+
priority?: number;
|
|
24
24
|
ns?: string;
|
|
25
25
|
}
|
|
26
26
|
export interface I18nResourceSignature {
|
|
27
|
-
(
|
|
27
|
+
(tier: I18nTier, lng: string, resource: string, data: Record<string, any>, opts?: I18nResourceOptions | string): void;
|
|
28
28
|
}
|
|
29
29
|
export interface I18nLeveledResourceSignature {
|
|
30
30
|
(lng: string, resource: string, data: Record<string, any>, opts?: I18nResourceOptions | string): void;
|
|
@@ -32,5 +32,7 @@ export interface I18nLeveledResourceSignature {
|
|
|
32
32
|
export interface I18nConfig {
|
|
33
33
|
defaultLng?: string;
|
|
34
34
|
defaultNs?: string;
|
|
35
|
+
fallbackLng?: string;
|
|
36
|
+
supportedLngs?: string[];
|
|
35
37
|
}
|
|
36
38
|
//# sourceMappingURL=types.d.ts.map
|
package/build/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE3C,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,cAAc,CAAA;CACrB;AAED,MAAM,WAAW,cAAe,SAAQ,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC;CAAI;AAEzE,MAAM,WAAW,aAAc,SAAQ,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC;CAAI;AAExE,MAAM,WAAW,aAAc,SAAQ,MAAM,CAAC,MAAM,EAAE;IACpD,SAAS,EAAE,YAAY,EAAE,CAAC;IAC1B,cAAc,EAAE,MAAM,EAAE,CAAA;CACzB,CAAC;CAAI;AAEN,MAAM,WAAW,YAAY;IAC3B,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,QAAQ,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC1B;AAED,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,EAAE,CAAC,EAAE,MAAM,CAAA;CACZ;AAED,MAAM,WAAW,qBAAqB;IACpC,CAAC,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,EAAE,mBAAmB,GAAG,MAAM,GAAG,IAAI,CAAA;CACtH;AAED,MAAM,WAAW,4BAA4B;IAC3C,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,EAAE,mBAAmB,GAAG,MAAM,GAAG,IAAI,CAAA;CACtG;AAED,MAAM,WAAW,UAAU;IACzB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,aAAa,CAAC,EAAE,MAAM,EAAE,CAAA;CACzB"}
|
package/build/utils/consts.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../../src/utils/consts.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../../src/utils/consts.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,QAAQ;;;CAGpB,CAAA"}
|
package/build/utils/consts.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export const
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[I18nLevel.Service]: 2
|
|
1
|
+
import { I18nTier } from '../consts.js';
|
|
2
|
+
export const tierCost = {
|
|
3
|
+
[I18nTier.Library]: 0,
|
|
4
|
+
[I18nTier.App]: 1,
|
|
6
5
|
};
|
|
7
6
|
//# sourceMappingURL=consts.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consts.js","sourceRoot":"","sources":["../../src/utils/consts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"consts.js","sourceRoot":"","sources":["../../src/utils/consts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEvC,MAAM,CAAC,MAAM,QAAQ,GAAG;IACtB,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;IACrB,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;CAClB,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owlmeans/i18n",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "tsc -b",
|
|
8
|
+
"test": "bun test ./tests",
|
|
8
9
|
"dev": "sleep 168 && nodemon -e ts,tsx,json --watch src --exec \"tsc -p ./tsconfig.json\"",
|
|
9
10
|
"watch": "tsc -b -w --preserveWatchOutput --pretty"
|
|
10
11
|
},
|
package/src/consts.ts
CHANGED
|
@@ -5,12 +5,16 @@ export const LIB_NAMESPACE = 'lib'
|
|
|
5
5
|
|
|
6
6
|
export const DEFAULT_LNG = 'en'
|
|
7
7
|
|
|
8
|
-
export const
|
|
8
|
+
export const SUPPORTED_LNGS = ['en', 'pl', 'ru', 'be', 'uk', 'es', 'de'] as const
|
|
9
|
+
|
|
10
|
+
export type SupportedLng = (typeof SUPPORTED_LNGS)[number]
|
|
9
11
|
|
|
10
12
|
export const MAX_PRIORITY = Number.MAX_SAFE_INTEGER
|
|
11
13
|
|
|
12
|
-
export enum
|
|
14
|
+
export enum I18nTier {
|
|
13
15
|
Library = 'library',
|
|
14
16
|
App = 'app',
|
|
15
|
-
Service = 'service'
|
|
16
17
|
}
|
|
18
|
+
|
|
19
|
+
/** @deprecated use I18nTier */
|
|
20
|
+
export const I18nLevel = I18nTier
|
package/src/helper.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { DEFAULT_NAMESPACE,
|
|
1
|
+
import { DEFAULT_NAMESPACE, I18nTier, LIB_NAMESPACE, MAX_PRIORITY } from './consts.js'
|
|
2
2
|
import type { I18nLeveledResourceSignature, I18nResource, I18nResourceOptions, I18nResourceSignature } from './types.js'
|
|
3
|
-
import {
|
|
3
|
+
import { tierCost } from './utils/consts.js'
|
|
4
4
|
import { ensureStructure } from './utils/storage.js'
|
|
5
5
|
|
|
6
6
|
const prepareOptions: (opts: I18nResourceOptions | string | undefined, ns?: string) => I18nResourceOptions = (opts, ns) => {
|
|
@@ -15,32 +15,27 @@ const prepareOptions: (opts: I18nResourceOptions | string | undefined, ns?: stri
|
|
|
15
15
|
return opts
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
const _addI18n: I18nResourceSignature = (
|
|
18
|
+
const _addI18n: I18nResourceSignature = (tier, lng, resource, data, opts) => {
|
|
19
19
|
opts = prepareOptions(opts)
|
|
20
20
|
const storage = ensureStructure(lng, resource, opts.ns)
|
|
21
21
|
|
|
22
22
|
const ns = opts.ns ?? DEFAULT_NAMESPACE
|
|
23
23
|
|
|
24
|
-
const
|
|
25
|
-
ns, lng,
|
|
24
|
+
const entry: I18nResource = {
|
|
25
|
+
ns, lng, tier, resource, data, priority: opts.priority
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
storage.resources.push(
|
|
28
|
+
storage.resources.push(entry)
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
export const addI18nLib: I18nLeveledResourceSignature = (lng, resource, data, opts) => {
|
|
32
32
|
opts = prepareOptions(opts, LIB_NAMESPACE)
|
|
33
|
-
_addI18n(
|
|
33
|
+
_addI18n(I18nTier.Library, lng, resource, data, opts)
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
export const addI18nApp: I18nLeveledResourceSignature = (lng, resource, data, opts) => {
|
|
37
37
|
opts = prepareOptions(opts, resource)
|
|
38
|
-
_addI18n(
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export const addCommonI18n: I18nLeveledResourceSignature = (lng, resource, data, opts) => {
|
|
42
|
-
opts = prepareOptions(opts)
|
|
43
|
-
_addI18n(I18nLevel.Service, lng, resource, data, opts)
|
|
38
|
+
_addI18n(I18nTier.App, lng, resource, data, opts)
|
|
44
39
|
}
|
|
45
40
|
|
|
46
41
|
export const initI18nResource = (lng: string, resource: string, ns?: string): null | I18nResource[] => {
|
|
@@ -51,10 +46,10 @@ export const initI18nResource = (lng: string, resource: string, ns?: string): nu
|
|
|
51
46
|
}
|
|
52
47
|
|
|
53
48
|
const result = [...translation.resources].sort((a, b) => {
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
if (
|
|
57
|
-
return
|
|
49
|
+
const aTier = tierCost[a.tier]
|
|
50
|
+
const bTier = tierCost[b.tier]
|
|
51
|
+
if (aTier !== bTier) {
|
|
52
|
+
return aTier - bTier
|
|
58
53
|
}
|
|
59
54
|
|
|
60
55
|
return (a.priority ?? MAX_PRIORITY) - (b.priority ?? MAX_PRIORITY)
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { I18nTier } from './consts.js'
|
|
2
2
|
|
|
3
3
|
export interface I18nStorage {
|
|
4
4
|
data: I18nNamespaces
|
|
@@ -16,19 +16,19 @@ export interface I18nLanguages extends Record<string, {
|
|
|
16
16
|
export interface I18nResource {
|
|
17
17
|
ns?: string
|
|
18
18
|
lng?: string
|
|
19
|
-
|
|
19
|
+
tier: I18nTier
|
|
20
20
|
resource: string
|
|
21
21
|
priority?: number
|
|
22
22
|
data: Record<string, any>
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export interface I18nResourceOptions {
|
|
26
|
-
|
|
26
|
+
priority?: number
|
|
27
27
|
ns?: string
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
export interface I18nResourceSignature {
|
|
31
|
-
(
|
|
31
|
+
(tier: I18nTier, lng: string, resource: string, data: Record<string, any>, opts?: I18nResourceOptions | string): void
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
export interface I18nLeveledResourceSignature {
|
|
@@ -38,4 +38,6 @@ export interface I18nLeveledResourceSignature {
|
|
|
38
38
|
export interface I18nConfig {
|
|
39
39
|
defaultLng?: string
|
|
40
40
|
defaultNs?: string
|
|
41
|
+
fallbackLng?: string
|
|
42
|
+
supportedLngs?: string[]
|
|
41
43
|
}
|
package/src/utils/consts.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { I18nTier } from '../consts.js'
|
|
2
2
|
|
|
3
|
-
export const
|
|
4
|
-
[
|
|
5
|
-
[
|
|
6
|
-
[I18nLevel.Service]: 2
|
|
3
|
+
export const tierCost = {
|
|
4
|
+
[I18nTier.Library]: 0,
|
|
5
|
+
[I18nTier.App]: 1,
|
|
7
6
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["context.ts"],"names":[],"mappings":"AAEA,mDAAmD;AACnD,eAAO,MAAM,YAAY,YAExB,CAAA"}
|
package/tests/context.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAE3D,mDAAmD;AACnD,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,EAAE;IAC/B,oBAAoB,CAAC,IAAI,GAAG,EAAE,CAAA;AAChC,CAAC,CAAA"}
|
package/tests/context.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registration.spec.d.ts","sourceRoot":"","sources":["registration.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, expect, test, beforeEach } from 'bun:test';
|
|
2
|
+
import { addI18nLib, addI18nApp, initI18nResource, DEFAULT_LNG, LIB_NAMESPACE, SUPPORTED_LNGS, I18nTier } from '@owlmeans/i18n';
|
|
3
|
+
import { resetStorage } from './context.js';
|
|
4
|
+
beforeEach(resetStorage);
|
|
5
|
+
describe('@owlmeans/i18n — registration', () => {
|
|
6
|
+
test('addI18nLib registers under lib namespace', () => {
|
|
7
|
+
addI18nLib('en', 'errors', { minLength: 'Too short' });
|
|
8
|
+
const result = initI18nResource('en', 'errors', LIB_NAMESPACE);
|
|
9
|
+
expect(result).not.toBeNull();
|
|
10
|
+
expect(result.length).toBe(1);
|
|
11
|
+
expect(result[0].tier).toBe(I18nTier.Library);
|
|
12
|
+
expect(result[0].ns).toBe(LIB_NAMESPACE);
|
|
13
|
+
expect(result[0].data).toMatchObject({ minLength: 'Too short' });
|
|
14
|
+
});
|
|
15
|
+
test('addI18nApp registers under resource-name namespace', () => {
|
|
16
|
+
addI18nApp('en', 'my-app', { title: 'My App' });
|
|
17
|
+
const result = initI18nResource('en', 'my-app', 'my-app');
|
|
18
|
+
expect(result).not.toBeNull();
|
|
19
|
+
expect(result[0].tier).toBe(I18nTier.App);
|
|
20
|
+
expect(result[0].ns).toBe('my-app');
|
|
21
|
+
});
|
|
22
|
+
test('addI18nApp accepts explicit ns override', () => {
|
|
23
|
+
addI18nApp('en', 'my-app', { title: 'My App' }, { ns: 'custom-ns' });
|
|
24
|
+
const result = initI18nResource('en', 'my-app', 'custom-ns');
|
|
25
|
+
expect(result).not.toBeNull();
|
|
26
|
+
expect(result[0].ns).toBe('custom-ns');
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
describe('@owlmeans/i18n — tier ordering (Library < App)', () => {
|
|
30
|
+
test('App tier entry sorts after Library tier', () => {
|
|
31
|
+
addI18nLib('en', 'labels', { submit: 'Submit (lib)' });
|
|
32
|
+
addI18nApp('en', 'labels', { submit: 'Submit (app)' }, { ns: LIB_NAMESPACE });
|
|
33
|
+
const result = initI18nResource('en', 'labels', LIB_NAMESPACE);
|
|
34
|
+
expect(result).not.toBeNull();
|
|
35
|
+
expect(result.length).toBe(2);
|
|
36
|
+
expect(result[0].tier).toBe(I18nTier.Library);
|
|
37
|
+
expect(result[1].tier).toBe(I18nTier.App);
|
|
38
|
+
});
|
|
39
|
+
test('in-tier priority tiebreak: lower priority number sorts first', () => {
|
|
40
|
+
addI18nLib('en', 'buttons', { ok: 'OK (low)' }, { priority: 10 });
|
|
41
|
+
addI18nLib('en', 'buttons', { ok: 'OK (high)' }, { priority: 1 });
|
|
42
|
+
const result = initI18nResource('en', 'buttons', LIB_NAMESPACE);
|
|
43
|
+
expect(result[0].priority).toBe(1);
|
|
44
|
+
expect(result[1].priority).toBe(10);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
describe('@owlmeans/i18n — multi-language', () => {
|
|
48
|
+
test('each language is stored independently', () => {
|
|
49
|
+
addI18nLib('en', 'errors', { minLength: 'Too short' });
|
|
50
|
+
addI18nLib('pl', 'errors', { minLength: 'Zbyt krótkie' });
|
|
51
|
+
const en = initI18nResource('en', 'errors', LIB_NAMESPACE);
|
|
52
|
+
const pl = initI18nResource('pl', 'errors', LIB_NAMESPACE);
|
|
53
|
+
expect(en[0].data.minLength).toBe('Too short');
|
|
54
|
+
expect(pl[0].data.minLength).toBe('Zbyt krótkie');
|
|
55
|
+
});
|
|
56
|
+
test('initI18nResource returns null on second call for the same language (already initialized)', () => {
|
|
57
|
+
addI18nLib('en', 'errors', { minLength: 'Too short' });
|
|
58
|
+
initI18nResource('en', 'errors', LIB_NAMESPACE);
|
|
59
|
+
const second = initI18nResource('en', 'errors', LIB_NAMESPACE);
|
|
60
|
+
expect(second).toBeNull();
|
|
61
|
+
});
|
|
62
|
+
test('SUPPORTED_LNGS contains the seven canonical languages', () => {
|
|
63
|
+
expect(SUPPORTED_LNGS).toEqual(['en', 'pl', 'ru', 'be', 'uk', 'es', 'de']);
|
|
64
|
+
});
|
|
65
|
+
test('DEFAULT_LNG is English', () => {
|
|
66
|
+
expect(DEFAULT_LNG).toBe('en');
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
//# source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ACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { describe, expect, test, beforeEach } from 'bun:test'
|
|
2
|
+
import {
|
|
3
|
+
addI18nLib, addI18nApp, initI18nResource,
|
|
4
|
+
DEFAULT_LNG, LIB_NAMESPACE, SUPPORTED_LNGS, I18nTier
|
|
5
|
+
} from '@owlmeans/i18n'
|
|
6
|
+
import { resetStorage } from './context.js'
|
|
7
|
+
|
|
8
|
+
beforeEach(resetStorage)
|
|
9
|
+
|
|
10
|
+
describe('@owlmeans/i18n — registration', () => {
|
|
11
|
+
test('addI18nLib registers under lib namespace', () => {
|
|
12
|
+
addI18nLib('en', 'errors', { minLength: 'Too short' })
|
|
13
|
+
const result = initI18nResource('en', 'errors', LIB_NAMESPACE)
|
|
14
|
+
expect(result).not.toBeNull()
|
|
15
|
+
expect(result!.length).toBe(1)
|
|
16
|
+
expect(result![0].tier).toBe(I18nTier.Library)
|
|
17
|
+
expect(result![0].ns).toBe(LIB_NAMESPACE)
|
|
18
|
+
expect(result![0].data).toMatchObject({ minLength: 'Too short' })
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('addI18nApp registers under resource-name namespace', () => {
|
|
22
|
+
addI18nApp('en', 'my-app', { title: 'My App' })
|
|
23
|
+
const result = initI18nResource('en', 'my-app', 'my-app')
|
|
24
|
+
expect(result).not.toBeNull()
|
|
25
|
+
expect(result![0].tier).toBe(I18nTier.App)
|
|
26
|
+
expect(result![0].ns).toBe('my-app')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('addI18nApp accepts explicit ns override', () => {
|
|
30
|
+
addI18nApp('en', 'my-app', { title: 'My App' }, { ns: 'custom-ns' })
|
|
31
|
+
const result = initI18nResource('en', 'my-app', 'custom-ns')
|
|
32
|
+
expect(result).not.toBeNull()
|
|
33
|
+
expect(result![0].ns).toBe('custom-ns')
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
describe('@owlmeans/i18n — tier ordering (Library < App)', () => {
|
|
38
|
+
test('App tier entry sorts after Library tier', () => {
|
|
39
|
+
addI18nLib('en', 'labels', { submit: 'Submit (lib)' })
|
|
40
|
+
addI18nApp('en', 'labels', { submit: 'Submit (app)' }, { ns: LIB_NAMESPACE })
|
|
41
|
+
const result = initI18nResource('en', 'labels', LIB_NAMESPACE)
|
|
42
|
+
expect(result).not.toBeNull()
|
|
43
|
+
expect(result!.length).toBe(2)
|
|
44
|
+
expect(result![0].tier).toBe(I18nTier.Library)
|
|
45
|
+
expect(result![1].tier).toBe(I18nTier.App)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('in-tier priority tiebreak: lower priority number sorts first', () => {
|
|
49
|
+
addI18nLib('en', 'buttons', { ok: 'OK (low)' }, { priority: 10 })
|
|
50
|
+
addI18nLib('en', 'buttons', { ok: 'OK (high)' }, { priority: 1 })
|
|
51
|
+
const result = initI18nResource('en', 'buttons', LIB_NAMESPACE)
|
|
52
|
+
expect(result![0].priority).toBe(1)
|
|
53
|
+
expect(result![1].priority).toBe(10)
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
describe('@owlmeans/i18n — multi-language', () => {
|
|
58
|
+
test('each language is stored independently', () => {
|
|
59
|
+
addI18nLib('en', 'errors', { minLength: 'Too short' })
|
|
60
|
+
addI18nLib('pl', 'errors', { minLength: 'Zbyt krótkie' })
|
|
61
|
+
const en = initI18nResource('en', 'errors', LIB_NAMESPACE)
|
|
62
|
+
const pl = initI18nResource('pl', 'errors', LIB_NAMESPACE)
|
|
63
|
+
expect(en![0].data.minLength).toBe('Too short')
|
|
64
|
+
expect(pl![0].data.minLength).toBe('Zbyt krótkie')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('initI18nResource returns null on second call for the same language (already initialized)', () => {
|
|
68
|
+
addI18nLib('en', 'errors', { minLength: 'Too short' })
|
|
69
|
+
initI18nResource('en', 'errors', LIB_NAMESPACE)
|
|
70
|
+
const second = initI18nResource('en', 'errors', LIB_NAMESPACE)
|
|
71
|
+
expect(second).toBeNull()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('SUPPORTED_LNGS contains the seven canonical languages', () => {
|
|
75
|
+
expect(SUPPORTED_LNGS).toEqual(['en', 'pl', 'ru', 'be', 'uk', 'es', 'de'])
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('DEFAULT_LNG is English', () => {
|
|
79
|
+
expect(DEFAULT_LNG).toBe('en')
|
|
80
|
+
})
|
|
81
|
+
})
|
package/tsconfig.json
CHANGED