@primate/core 0.7.4 → 0.8.0
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/lib/private/App.d.ts +128 -65
- package/lib/private/Bag.d.ts +11 -0
- package/lib/private/Bag.js +31 -0
- package/lib/private/Flags.d.ts +5 -5
- package/lib/private/Flags.js +2 -1
- package/lib/private/app/Facade.client.d.ts +20 -0
- package/lib/private/app/Facade.client.js +30 -0
- package/lib/private/app/{Facade.d.ts → Facade.server.d.ts} +141 -72
- package/lib/private/app/{Facade.js → Facade.server.js} +12 -4
- package/lib/private/build/App.d.ts +0 -2
- package/lib/private/build/App.js +0 -7
- package/lib/private/build/client/index.js +29 -27
- package/lib/private/build/client/plugin/frontend.js +1 -1
- package/lib/private/build/hook.js +0 -3
- package/lib/private/build/server/index.js +5 -18
- package/lib/private/build/server/plugin/native-addons.d.ts +1 -1
- package/lib/private/build/server/plugin/native-addons.js +1 -1
- package/lib/private/client/create-form.d.ts +13 -9
- package/lib/private/client/create-form.js +21 -4
- package/lib/private/client/transport.js +0 -2
- package/lib/private/config/index.d.ts +6 -2
- package/lib/private/config/schema.d.ts +28 -21
- package/lib/private/config/schema.js +5 -3
- package/lib/private/cookie.d.ts +5 -5
- package/lib/private/db/errors.d.ts +82 -82
- package/lib/private/db/migrate/store.d.ts +2 -2
- package/lib/private/db/primary.d.ts +1 -1
- package/lib/private/db/sql.d.ts +1 -1
- package/lib/private/errors.d.ts +80 -68
- package/lib/private/errors.js +22 -0
- package/lib/private/frontend.d.ts +7 -7
- package/lib/private/frontend.js +7 -10
- package/lib/private/i18n/API.d.ts +14 -18
- package/lib/private/i18n/config.client.d.ts +7 -0
- package/lib/private/i18n/config.client.js +130 -0
- package/lib/private/i18n/config.server.d.ts +7 -0
- package/lib/private/i18n/{index/server.js → config.server.js} +31 -23
- package/lib/private/i18n/constant/COOKIE_NAME.d.ts +1 -1
- package/lib/private/i18n/constant/DEFAULT_LOCALE.d.ts +1 -1
- package/lib/private/i18n/constant/DEFAULT_PERSIST_MODE.d.ts +1 -1
- package/lib/private/i18n/constant/PERSIST_HEADER.d.ts +1 -1
- package/lib/private/i18n/constant/PERSIST_METHOD.d.ts +1 -1
- package/lib/private/i18n/constant/PERSIST_STORAGE_KEY.d.ts +1 -1
- package/lib/private/i18n/index.d.ts +42 -0
- package/lib/private/i18n/index.js +6 -0
- package/lib/private/i18n/missing.d.ts +5 -0
- package/lib/private/i18n/missing.js +38 -0
- package/lib/private/i18n/module.js +36 -52
- package/lib/private/i18n/schema.d.ts +4 -4
- package/lib/private/loader.d.ts +5 -0
- package/lib/private/loader.js +28 -0
- package/lib/private/logger.d.ts +1 -1
- package/lib/private/request/RequestBag.d.ts +12 -13
- package/lib/private/request/RequestBag.js +5 -4
- package/lib/private/request/RequestView.d.ts +5 -5
- package/lib/private/request/RequestView.js +0 -1
- package/lib/private/response/view.d.ts +1 -0
- package/lib/private/response/view.js +0 -7
- package/lib/private/route/Handler.d.ts +1 -1
- package/lib/private/route/NarrowedRequest.d.ts +6 -1
- package/lib/private/route/Options.d.ts +2 -1
- package/lib/private/route/hook.d.ts +1 -2
- package/lib/private/route/router.d.ts +9 -9
- package/lib/private/route/router.js +9 -0
- package/lib/private/route.client.d.ts +21 -12
- package/lib/private/route.client.js +9 -4
- package/lib/private/route.d.ts +3 -0
- package/lib/private/route.js +2 -0
- package/lib/private/serve/App.d.ts +2 -13
- package/lib/private/serve/App.js +63 -46
- package/lib/private/serve/Init.d.ts +0 -3
- package/lib/private/server/TAG.d.ts +1 -1
- package/lib/private/session/config.client.d.ts +4 -0
- package/lib/private/session/config.client.js +24 -0
- package/lib/private/session/config.server.d.ts +15 -0
- package/lib/private/session/config.server.js +44 -0
- package/lib/private/session/index.d.ts +2 -14
- package/lib/private/session/index.js +2 -43
- package/lib/private/session/schema.d.ts +5 -5
- package/lib/private/store/PrimaryKey.d.ts +1 -1
- package/lib/private/store.client.d.ts +13 -0
- package/lib/private/store.client.js +9 -0
- package/lib/private/store.d.ts +2 -0
- package/lib/private/target/Manager.d.ts +2 -0
- package/lib/private/target/Manager.js +29 -5
- package/lib/private/target/Target.d.ts +2 -0
- package/lib/public/i18n.d.ts +3 -0
- package/lib/public/{i18n/config.js → i18n.js} +1 -1
- package/lib/public/loader.d.ts +2 -0
- package/lib/public/loader.js +2 -0
- package/lib/public/response.d.ts +1 -1
- package/lib/public/{session/config.d.ts → session.d.ts} +1 -1
- package/lib/public/{session/config.js → session.js} +1 -1
- package/package.json +27 -13
- package/lib/private/app/Facade.browser.d.ts +0 -11
- package/lib/private/app/Facade.browser.js +0 -19
- package/lib/private/build/server/plugin/store.d.ts +0 -4
- package/lib/private/build/server/plugin/store.js +0 -25
- package/lib/private/build/server/plugin/stores.d.ts +0 -4
- package/lib/private/build/server/plugin/stores.js +0 -28
- package/lib/private/i18n/index/client.d.ts +0 -9
- package/lib/private/i18n/index/client.js +0 -152
- package/lib/private/i18n/index/server.d.ts +0 -9
- package/lib/private/i18n/symbol/internal.d.ts +0 -3
- package/lib/private/i18n/symbol/internal.js +0 -3
- package/lib/public/i18n/API.d.ts +0 -2
- package/lib/public/i18n/API.js +0 -2
- package/lib/public/i18n/Catalogs.d.ts +0 -2
- package/lib/public/i18n/Catalogs.js +0 -2
- package/lib/public/i18n/ContextData.d.ts +0 -2
- package/lib/public/i18n/ContextData.js +0 -2
- package/lib/public/i18n/config.d.ts +0 -2
- package/lib/public/i18n/locale.d.ts +0 -2
- package/lib/public/i18n/locale.js +0 -2
- package/lib/public/i18n/sInternal.d.ts +0 -2
- package/lib/public/i18n/sInternal.js +0 -2
- package/lib/public/route/hook.d.ts +0 -2
- package/lib/public/route/hook.js +0 -2
- /package/lib/private/i18n/{index/types.d.ts → types.d.ts} +0 -0
- /package/lib/private/i18n/{index/types.js → types.js} +0 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import COOKIE_NAME from "#i18n/constant/COOKIE_NAME";
|
|
2
|
+
import DEFAULT_PERSIST_MODE from "#i18n/constant/DEFAULT_PERSIST_MODE";
|
|
3
|
+
import PERSIST_HEADER from "#i18n/constant/PERSIST_HEADER";
|
|
4
|
+
import PERSIST_METHOD from "#i18n/constant/PERSIST_METHOD";
|
|
5
|
+
import PERSIST_STORAGE_KEY from "#i18n/constant/PERSIST_STORAGE_KEY";
|
|
6
|
+
import format from "#i18n/format";
|
|
7
|
+
import Formatter from "#i18n/Formatter";
|
|
8
|
+
import resolve from "#i18n/resolve";
|
|
9
|
+
import validate from "#i18n/validate";
|
|
10
|
+
import is from "@rcompat/is";
|
|
11
|
+
export default function i18n(config) {
|
|
12
|
+
const catalogs = config.locales;
|
|
13
|
+
for (const [locale, catalog] of Object.entries(catalogs)) {
|
|
14
|
+
validate(catalog, locale);
|
|
15
|
+
}
|
|
16
|
+
const default_catalog = catalogs[config.defaultLocale];
|
|
17
|
+
const currency = config.currency ?? "USD";
|
|
18
|
+
const persist = config.persist ?? DEFAULT_PERSIST_MODE;
|
|
19
|
+
const locales = Object.keys(catalogs);
|
|
20
|
+
let active_locale = config.defaultLocale;
|
|
21
|
+
const get_locale = () => active_locale;
|
|
22
|
+
const restore_cookie = (request) => {
|
|
23
|
+
const saved = request?.cookies?.[COOKIE_NAME];
|
|
24
|
+
if (saved !== undefined && saved in catalogs) {
|
|
25
|
+
active_locale = saved;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const restore_storage = (kind) => {
|
|
29
|
+
try {
|
|
30
|
+
const storage = kind === "localStorage"
|
|
31
|
+
? localStorage
|
|
32
|
+
: sessionStorage;
|
|
33
|
+
const saved = storage.getItem(PERSIST_STORAGE_KEY);
|
|
34
|
+
if (saved !== null && saved in catalogs) {
|
|
35
|
+
active_locale = saved;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// ignore storage failures
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const restore = (request) => {
|
|
43
|
+
if (persist === false)
|
|
44
|
+
return;
|
|
45
|
+
if (persist === "cookie") {
|
|
46
|
+
restore_cookie(request);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
restore_storage(persist);
|
|
50
|
+
};
|
|
51
|
+
const persist_locale = (locale) => {
|
|
52
|
+
if (persist === false)
|
|
53
|
+
return;
|
|
54
|
+
if (persist === "cookie") {
|
|
55
|
+
void fetch("/", {
|
|
56
|
+
method: PERSIST_METHOD,
|
|
57
|
+
headers: {
|
|
58
|
+
[PERSIST_HEADER]: locale,
|
|
59
|
+
},
|
|
60
|
+
keepalive: true,
|
|
61
|
+
}).then(response => {
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
console.warn(`[i18n] persist failed: ${response.status}`);
|
|
64
|
+
}
|
|
65
|
+
}).catch(error => {
|
|
66
|
+
console.warn("[i18n] persist failed", error);
|
|
67
|
+
});
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
const storage = persist === "localStorage"
|
|
72
|
+
? localStorage
|
|
73
|
+
: sessionStorage;
|
|
74
|
+
storage.setItem(PERSIST_STORAGE_KEY, locale);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// ignore storage failures
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const set_locale = (locale) => {
|
|
81
|
+
if (!(locale in catalogs)) {
|
|
82
|
+
throw new Error(`[i18n] Unknown locale "${locale}".`);
|
|
83
|
+
}
|
|
84
|
+
active_locale = locale;
|
|
85
|
+
persist_locale(locale);
|
|
86
|
+
};
|
|
87
|
+
function t(...args) {
|
|
88
|
+
const formatter = new Formatter(active_locale);
|
|
89
|
+
const [key, params] = args;
|
|
90
|
+
const translated = resolve(catalogs[active_locale], key) ??
|
|
91
|
+
resolve(default_catalog, key) ??
|
|
92
|
+
String(key);
|
|
93
|
+
if (is.string(translated)) {
|
|
94
|
+
return format(translated, params ?? {}, currency, formatter);
|
|
95
|
+
}
|
|
96
|
+
return translated;
|
|
97
|
+
}
|
|
98
|
+
const api = t;
|
|
99
|
+
Object.defineProperties(api, {
|
|
100
|
+
defaultLocale: {
|
|
101
|
+
value: config.defaultLocale,
|
|
102
|
+
enumerable: true,
|
|
103
|
+
},
|
|
104
|
+
locales: {
|
|
105
|
+
value: locales,
|
|
106
|
+
enumerable: true,
|
|
107
|
+
},
|
|
108
|
+
catalogs: {
|
|
109
|
+
value: catalogs,
|
|
110
|
+
enumerable: true,
|
|
111
|
+
},
|
|
112
|
+
currency: {
|
|
113
|
+
value: currency,
|
|
114
|
+
enumerable: true,
|
|
115
|
+
},
|
|
116
|
+
locale: {
|
|
117
|
+
value: {
|
|
118
|
+
get: get_locale,
|
|
119
|
+
set: set_locale,
|
|
120
|
+
},
|
|
121
|
+
enumerable: true,
|
|
122
|
+
},
|
|
123
|
+
restore: {
|
|
124
|
+
value: restore,
|
|
125
|
+
enumerable: true,
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
return api;
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=config.client.js.map
|