@primate/core 0.8.1 → 0.8.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.
@@ -18,28 +18,28 @@ export default function i18n(config) {
18
18
  const persist = config.persist ?? DEFAULT_PERSIST_MODE;
19
19
  const locales = Object.keys(catalogs);
20
20
  let active_locale = config.defaultLocale;
21
- const get_locale = () => active_locale;
22
- const restore_cookie = (request) => {
21
+ function get() {
22
+ return active_locale;
23
+ }
24
+ function restore_cookie(request) {
23
25
  const saved = request?.cookies?.[COOKIE_NAME];
24
- if (saved !== undefined && saved in catalogs) {
26
+ if (saved !== undefined && saved in catalogs)
25
27
  active_locale = saved;
26
- }
27
- };
28
- const restore_storage = (kind) => {
28
+ }
29
+ ;
30
+ function restore_storage(kind) {
29
31
  try {
30
- const storage = kind === "localStorage"
31
- ? localStorage
32
- : sessionStorage;
32
+ const storage = kind === "localStorage" ? localStorage : sessionStorage;
33
33
  const saved = storage.getItem(PERSIST_STORAGE_KEY);
34
- if (saved !== null && saved in catalogs) {
34
+ if (saved !== null && saved in catalogs)
35
35
  active_locale = saved;
36
- }
37
36
  }
38
37
  catch {
39
38
  // ignore storage failures
40
39
  }
41
- };
42
- const restore = (request) => {
40
+ }
41
+ ;
42
+ function restore(request) {
43
43
  if (persist === false)
44
44
  return;
45
45
  if (persist === "cookie") {
@@ -47,16 +47,15 @@ export default function i18n(config) {
47
47
  return;
48
48
  }
49
49
  restore_storage(persist);
50
- };
51
- const persist_locale = (locale) => {
50
+ }
51
+ ;
52
+ function persist_locale(locale) {
52
53
  if (persist === false)
53
54
  return;
54
55
  if (persist === "cookie") {
55
56
  void fetch("/", {
56
57
  method: PERSIST_METHOD,
57
- headers: {
58
- [PERSIST_HEADER]: locale,
59
- },
58
+ headers: { [PERSIST_HEADER]: locale },
60
59
  keepalive: true,
61
60
  }).then(response => {
62
61
  if (!response.ok) {
@@ -76,55 +75,36 @@ export default function i18n(config) {
76
75
  catch {
77
76
  // ignore storage failures
78
77
  }
79
- };
80
- const set_locale = (locale) => {
78
+ }
79
+ ;
80
+ function set(locale) {
81
81
  if (!(locale in catalogs)) {
82
82
  throw new Error(`[i18n] Unknown locale "${locale}".`);
83
83
  }
84
84
  active_locale = locale;
85
85
  persist_locale(locale);
86
- };
86
+ }
87
+ ;
87
88
  function t(...args) {
88
89
  const formatter = new Formatter(active_locale);
89
- const [key, params] = args;
90
+ const [key, params = {}] = args;
90
91
  const translated = resolve(catalogs[active_locale], key) ??
91
92
  resolve(default_catalog, key) ??
92
93
  String(key);
93
- if (is.string(translated)) {
94
- return format(translated, params ?? {}, currency, formatter);
95
- }
96
- return translated;
94
+ return (is.string(translated)
95
+ ? format(translated, params, currency, formatter)
96
+ : translated);
97
97
  }
98
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
- });
99
+ api.defaultLocale = config.defaultLocale;
100
+ api.locales = locales;
101
+ api.catalogs = catalogs;
102
+ api.currency = currency;
103
+ api.locale = { get, set };
104
+ api.restore = restore;
105
+ api.with = (_locale) => {
106
+ throw new Error("[i18n] with is not supported on client.");
107
+ };
128
108
  return api;
129
109
  }
130
110
  //# sourceMappingURL=config.client.js.map