langsys-js-typescript 0.5.0 → 0.6.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.
package/dist/index.mjs CHANGED
@@ -935,8 +935,23 @@ var isEmpty = (data) => {
935
935
  if (typeof data === "object") return Object.keys(data).length === 0;
936
936
  return false;
937
937
  };
938
+ function toUtf8ByteString(input) {
939
+ const bytes = new TextEncoder().encode(input);
940
+ let out = "";
941
+ for (let i = 0; i < bytes.length; i += 8192) {
942
+ out += String.fromCharCode(...bytes.subarray(i, i + 8192));
943
+ }
944
+ return out;
945
+ }
938
946
  function md5(inputString) {
939
947
  if (typeof inputString !== "string") inputString = JSON.stringify(inputString);
948
+ return md5Core(toUtf8ByteString(inputString));
949
+ }
950
+ function md5Legacy(inputString) {
951
+ if (typeof inputString !== "string") inputString = JSON.stringify(inputString);
952
+ return md5Core(inputString);
953
+ }
954
+ function md5Core(inputString) {
940
955
  const hc = "0123456789abcdef";
941
956
  function rh(n) {
942
957
  let j, s = "";
@@ -1068,6 +1083,14 @@ var TRANSLATABLE_ATTRIBUTES = [
1068
1083
  "data-required-message",
1069
1084
  "data-pattern-message"
1070
1085
  ];
1086
+ var PHRASE_MARKER_ATTRS = ["data-ls-phrase", "data-langsys-phrase"];
1087
+ function isPhraseMarked(element) {
1088
+ return PHRASE_MARKER_ATTRS.some((attr) => {
1089
+ if (!element.hasAttribute(attr)) return false;
1090
+ const value = (element.getAttribute(attr) ?? "").toLowerCase();
1091
+ return value !== "false" && value !== "0";
1092
+ });
1093
+ }
1071
1094
  var VALUE_TRANSLATABLE_ELEMENTS = ["button"];
1072
1095
  var VALUE_TRANSLATABLE_INPUT_TYPES = ["submit", "button"];
1073
1096
  var SEMANTIC_STYLE_PROPERTIES = [
@@ -1122,6 +1145,9 @@ var SEMANTIC_STYLE_PROPERTIES = [
1122
1145
  function generateCustomId(category, tokens) {
1123
1146
  return md5(JSON.stringify([category, tokens]));
1124
1147
  }
1148
+ function generateLegacyCustomId(category, tokens) {
1149
+ return md5Legacy(JSON.stringify([category, tokens]));
1150
+ }
1125
1151
  function isContentBlockKnown(category, customId) {
1126
1152
  const cats = sTranslations.get();
1127
1153
  const lookupCat = category || "__uncategorized__";
@@ -1181,7 +1207,7 @@ function _walkForTokens(liveRoot, cloneNodes, tokens, indices) {
1181
1207
  if (node.nodeType === Node.ELEMENT_NODE) {
1182
1208
  const el = node;
1183
1209
  if (el.getAttribute("translate") === "no") return;
1184
- if (el.hasAttribute("data-ls-phrase")) return;
1210
+ if (isPhraseMarked(el)) return;
1185
1211
  }
1186
1212
  if (node.hasChildNodes()) {
1187
1213
  _applyStylesToClone(liveRoot, node, [...indices, index]);
@@ -1331,7 +1357,8 @@ var Translate = class {
1331
1357
  return true;
1332
1358
  }
1333
1359
  async handleContentBlock(contentBlock) {
1334
- if (isEmpty(this.custom_id)) {
1360
+ const derivedId = isEmpty(this.custom_id);
1361
+ if (derivedId) {
1335
1362
  this.custom_id = generateCustomId(contentBlock.category, contentBlock.tokens);
1336
1363
  }
1337
1364
  contentBlock.custom_id = this.custom_id;
@@ -1343,6 +1370,17 @@ var Translate = class {
1343
1370
  }
1344
1371
  return;
1345
1372
  }
1373
+ if (derivedId) {
1374
+ const legacyId = generateLegacyCustomId(contentBlock.category, contentBlock.tokens);
1375
+ if (legacyId !== this.custom_id && isContentBlockKnown(contentBlock.category, legacyId)) {
1376
+ this.custom_id = legacyId;
1377
+ if (this.tokens.length > 1 && this.element) {
1378
+ this.translate(Array.from(this.element.childNodes));
1379
+ this.lastTranslatedLocale = currentlyLoadedLocale.get();
1380
+ }
1381
+ return;
1382
+ }
1383
+ }
1346
1384
  void registerContentBlock(contentBlock);
1347
1385
  if (this.tokens.length > 1 && this.element) {
1348
1386
  this.translate(Array.from(this.element.childNodes));
@@ -1358,7 +1396,7 @@ var Translate = class {
1358
1396
  if (node?.nodeType === Node.ELEMENT_NODE) {
1359
1397
  const element = node;
1360
1398
  if (element.getAttribute("translate") === "no") return;
1361
- if (element.hasAttribute("data-ls-phrase")) return;
1399
+ if (isPhraseMarked(element)) return;
1362
1400
  this.translateAttributes(node);
1363
1401
  }
1364
1402
  if (node?.nodeType !== Node.TEXT_NODE) {
@@ -1599,6 +1637,6 @@ var t = ((phrase, ...rest) => {
1599
1637
  });
1600
1638
  var tSignal = LangsysApp.Translations.tSignal;
1601
1639
 
1602
- export { LangsysApp, LangsysAppAPI, Logger, PHRASE_MARKER_ATTR, Phrase, Translate, Translations, canonicalizeLocale, createSignal, currentlyLoadedLocale, encodeRichText, findUnusedParamKeys, generateCustomId, getValue, interpolate, isContentBlockKnown, isEmpty, isICU, logger, markupTokenValues, md5, normalizeMarkupPlaceholders, persist, reconstitute, registerContentBlock, sTranslations, stripSentinels, t, tSignal, tokenizeElement };
1640
+ export { LangsysApp, LangsysAppAPI, Logger, PHRASE_MARKER_ATTR, PHRASE_MARKER_ATTRS, Phrase, Translate, Translations, canonicalizeLocale, createSignal, currentlyLoadedLocale, encodeRichText, findUnusedParamKeys, generateCustomId, generateLegacyCustomId, getValue, interpolate, isContentBlockKnown, isEmpty, isICU, isPhraseMarked, logger, markupTokenValues, md5, md5Legacy, normalizeMarkupPlaceholders, persist, reconstitute, registerContentBlock, sTranslations, stripSentinels, t, tSignal, tokenizeElement };
1603
1641
  //# sourceMappingURL=index.mjs.map
1604
1642
  //# sourceMappingURL=index.mjs.map