@dabalabs/lang 0.0.6-beta → 0.0.7-beta

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/dist/dabalang.js CHANGED
@@ -143,6 +143,35 @@ async function submitCorrection(gatewayUrl, projectId, apiKey, targetLang, sourc
143
143
  await parseOrThrow(response);
144
144
  }
145
145
 
146
+ // src/translation-cache.ts
147
+ var PREFIX = "dabalang:tcache:";
148
+ var VERSION = 1;
149
+ var MAX_ENTRY_BYTES = 512 * 1024;
150
+ function key(projectId, lang) {
151
+ return `${PREFIX}${projectId}:${lang}`;
152
+ }
153
+ function readCachedTranslations(projectId, lang) {
154
+ try {
155
+ const raw = window.localStorage.getItem(key(projectId, lang));
156
+ if (!raw) return null;
157
+ const parsed = JSON.parse(raw);
158
+ if (!parsed || parsed.v !== VERSION || typeof parsed.t !== "object") return null;
159
+ return new Map(Object.entries(parsed.t));
160
+ } catch {
161
+ return null;
162
+ }
163
+ }
164
+ function writeCachedTranslations(projectId, lang, translations) {
165
+ try {
166
+ if (translations.size === 0) return;
167
+ const payload = { v: VERSION, t: Object.fromEntries(translations) };
168
+ const serialised = JSON.stringify(payload);
169
+ if (serialised.length > MAX_ENTRY_BYTES) return;
170
+ window.localStorage.setItem(key(projectId, lang), serialised);
171
+ } catch {
172
+ }
173
+ }
174
+
146
175
  // src/dom/ignore.ts
147
176
  var IGNORE_ATTR = "data-dabalang-ignore";
148
177
  var SENSITIVE_ELEMENT_SELECTOR = `[${IGNORE_ATTR}], input[type="password"], input[type="hidden"], [autocomplete="current-password"], [autocomplete="new-password"], [autocomplete^="cc-"]`;
@@ -273,8 +302,28 @@ var TranslationApplier = class {
273
302
  }
274
303
  return null;
275
304
  }
305
+ /** Writes every translation this map covers into the DOM. */
306
+ paint(translations) {
307
+ let painted = 0;
308
+ for (const [sourceText, nodes] of this.groups) {
309
+ const translated = translations.get(sourceText);
310
+ if (translated === void 0) continue;
311
+ for (const node of nodes) node.textContent = translated;
312
+ painted += 1;
313
+ }
314
+ return painted;
315
+ }
276
316
  async applyLanguage(targetLang) {
277
- const cached = this.fetchedLanguages.get(targetLang);
317
+ let cached = this.fetchedLanguages.get(targetLang);
318
+ if (!cached) {
319
+ const stored = readCachedTranslations(this.projectId, targetLang);
320
+ if (stored && stored.size > 0) {
321
+ cached = stored;
322
+ this.fetchedLanguages.set(targetLang, stored);
323
+ this.paint(stored);
324
+ this.activeLang = targetLang;
325
+ }
326
+ }
278
327
  const allTexts = this.sourceTexts();
279
328
  const missing = cached ? allTexts.filter((t) => !cached.has(t)) : allTexts;
280
329
  if (missing.length > 0) {
@@ -282,13 +331,9 @@ var TranslationApplier = class {
282
331
  const map = cached ?? /* @__PURE__ */ new Map();
283
332
  for (const r of results) map.set(r.sourceText, r.translatedText);
284
333
  this.fetchedLanguages.set(targetLang, map);
334
+ writeCachedTranslations(this.projectId, targetLang, map);
285
335
  }
286
- const translations = this.fetchedLanguages.get(targetLang);
287
- for (const [sourceText, nodes] of this.groups) {
288
- const translated = translations.get(sourceText);
289
- if (translated === void 0) continue;
290
- for (const node of nodes) node.textContent = translated;
291
- }
336
+ this.paint(this.fetchedLanguages.get(targetLang));
292
337
  this.activeLang = targetLang;
293
338
  }
294
339
  restoreOriginal() {
@@ -600,10 +645,10 @@ var PathRouter = class {
600
645
  };
601
646
 
602
647
  // src/preference.ts
603
- var PREFIX = "dabalang:lang:";
648
+ var PREFIX2 = "dabalang:lang:";
604
649
  function readPreferredLanguage(projectId) {
605
650
  try {
606
- return window.localStorage.getItem(PREFIX + projectId);
651
+ return window.localStorage.getItem(PREFIX2 + projectId);
607
652
  } catch {
608
653
  return null;
609
654
  }
@@ -611,9 +656,9 @@ function readPreferredLanguage(projectId) {
611
656
  function writePreferredLanguage(projectId, langCode) {
612
657
  try {
613
658
  if (langCode === null) {
614
- window.localStorage.removeItem(PREFIX + projectId);
659
+ window.localStorage.removeItem(PREFIX2 + projectId);
615
660
  } else {
616
- window.localStorage.setItem(PREFIX + projectId, langCode);
661
+ window.localStorage.setItem(PREFIX2 + projectId, langCode);
617
662
  }
618
663
  } catch {
619
664
  }