@sveltekit-i18n/base 3.0.0-next.0 → 3.0.0-next.1

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.
@@ -15,18 +15,15 @@ declare class I18nCore<ParserParams extends Parser.Params = any, ParserOutput =
15
15
  locales: Config.LocaleInput<LocaleUnion>[];
16
16
  initialized: boolean;
17
17
  /**
18
- * Translates `key` for the active locale. Reactive wherever reads are
19
- * tracked: the call reads the translation table and locale, so a component
20
- * using `{i18n.t('key')}` re-renders when either changes.
18
+ * Translates `key` for the active locale. Reactive on two levels: the
19
+ * returned function reads the tables at CALL time, so a destructured `t`
20
+ * keeps translating against live state, and its identity is refreshed
21
+ * whenever the config, the tables or the locale change, so merely holding
22
+ * the reference is tracked too.
21
23
  */
22
24
  t: Translations.TranslationFunction<ParserParams, ParserOutput, TranslationSchema>;
23
25
  /** Like `t`, for an explicit locale. */
24
26
  l: Translations.LocalTranslationFunction<ParserParams, ParserOutput, TranslationSchema, LocaleUnion>;
25
- /**
26
- * Applies a config. Overridable extension seam — `sveltekit-i18n` wires its
27
- * default parser by extending this method.
28
- */
29
- configLoader(config: Config.T<ParserParams, ParserOutput>): Promise<void>;
30
27
  /**
31
28
  * Public entry for (re)configuration. The failure is reported here and the
32
29
  * promise marked handled, so a fire-and-forget call cannot become an
@@ -30,7 +30,7 @@ class I18nCore {
30
30
  // A constructor may return a substitute object: the instance is folded
31
31
  // through `config.extensions` left to right, so `new I18n(config)`
32
32
  // evaluates to the last extension's output. The extensions run after the
33
- // synchronous part of `configLoader` — they receive a configured instance.
33
+ // synchronous part of the config load — they receive a configured instance.
34
34
  return (config?.extensions ?? []).reduce((acc, extension) => extension(acc), this);
35
35
  }
36
36
  // -- reactive reads ---------------------------------------------------------
@@ -66,42 +66,30 @@ class I18nCore {
66
66
  });
67
67
  initialized = $derived(this.#locale !== undefined && this.#route !== undefined && Object.keys(this.#translations).length > 0);
68
68
  /**
69
- * Translates `key` for the active locale. Reactive wherever reads are
70
- * tracked: the call reads the translation table and locale, so a component
71
- * using `{i18n.t('key')}` re-renders when either changes.
69
+ * Translates `key` for the active locale. Reactive on two levels: the
70
+ * returned function reads the tables at CALL time, so a destructured `t`
71
+ * keeps translating against live state, and its identity is refreshed
72
+ * whenever the config, the tables or the locale change, so merely holding
73
+ * the reference is tracked too.
72
74
  */
73
- t = (key, ...params) => {
74
- const { parser, fallbackLocale, ...rest } = this.#config ?? {};
75
- return translate({
76
- parser,
77
- key,
78
- params,
79
- translations: this.#translations,
80
- locale: this.#locale,
81
- fallbackLocale,
82
- ...(hasOwn(rest, 'fallbackValue') ? { fallbackValue: rest.fallbackValue } : {}),
83
- });
84
- };
75
+ t = $derived.by(() => {
76
+ void this.#config;
77
+ void this.#translations;
78
+ void this.#locale;
79
+ return (key, ...params) => this.#translate(this.#locale, key, params);
80
+ });
85
81
  /** Like `t`, for an explicit locale. */
86
- l = (locale, key, ...params) => {
87
- const { parser, fallbackLocale, ...rest } = this.#config ?? {};
88
- const [sanitizedLocale = locale] = this.#sanitize(locale);
89
- return translate({
90
- parser,
91
- key,
92
- params,
93
- translations: this.#translations,
94
- locale: sanitizedLocale,
95
- fallbackLocale,
96
- ...(hasOwn(rest, 'fallbackValue') ? { fallbackValue: rest.fallbackValue } : {}),
97
- });
98
- };
82
+ l = $derived.by(() => {
83
+ void this.#config;
84
+ void this.#translations;
85
+ return (locale, key, ...params) => {
86
+ const [sanitizedLocale = locale] = this.#sanitize(locale);
87
+ return this.#translate(sanitizedLocale, key, params);
88
+ };
89
+ });
99
90
  // -- configuration ----------------------------------------------------------
100
- /**
101
- * Applies a config. Overridable extension seam — `sveltekit-i18n` wires its
102
- * default parser by extending this method.
103
- */
104
- async configLoader(config) {
91
+ /** Applies a config. The public entry is `loadConfig`. */
92
+ async #configLoader(config) {
105
93
  if (!config) {
106
94
  logger.error('No config provided!');
107
95
  return;
@@ -149,7 +137,7 @@ class I18nCore {
149
137
  loadConfig = (config) => {
150
138
  if (this.#inert('loadConfig'))
151
139
  return Promise.resolve();
152
- const promise = this.configLoader(config);
140
+ const promise = this.#configLoader(config);
153
141
  promise.catch((error) => logError('Failed to load the i18n config.', error));
154
142
  return promise;
155
143
  };
@@ -268,6 +256,18 @@ class I18nCore {
268
256
  this.#pending = new Set();
269
257
  };
270
258
  // -- internals --------------------------------------------------------------
259
+ #translate(locale, key, params) {
260
+ const { parser, fallbackLocale, ...rest } = this.#config ?? {};
261
+ return translate({
262
+ parser,
263
+ key,
264
+ params,
265
+ translations: this.#translations,
266
+ locale,
267
+ fallbackLocale,
268
+ ...(hasOwn(rest, 'fallbackValue') ? { fallbackValue: rest.fallbackValue } : {}),
269
+ });
270
+ }
271
271
  /**
272
272
  * Resolves loader data for a locale and route WITHOUT applying it. Returns
273
273
  * `[]` when there is nothing to load. The `cache` expiry is evaluated by
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltekit-i18n/base",
3
- "version": "3.0.0-next.0",
3
+ "version": "3.0.0-next.1",
4
4
  "description": "Base functionality of sveltekit-i18n library with a support for external message parsers.",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",