langsys-js-typescript 0.4.3 → 0.6.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/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,10 @@ 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) => element.hasAttribute(attr));
1089
+ }
1071
1090
  var VALUE_TRANSLATABLE_ELEMENTS = ["button"];
1072
1091
  var VALUE_TRANSLATABLE_INPUT_TYPES = ["submit", "button"];
1073
1092
  var SEMANTIC_STYLE_PROPERTIES = [
@@ -1122,6 +1141,9 @@ var SEMANTIC_STYLE_PROPERTIES = [
1122
1141
  function generateCustomId(category, tokens) {
1123
1142
  return md5(JSON.stringify([category, tokens]));
1124
1143
  }
1144
+ function generateLegacyCustomId(category, tokens) {
1145
+ return md5Legacy(JSON.stringify([category, tokens]));
1146
+ }
1125
1147
  function isContentBlockKnown(category, customId) {
1126
1148
  const cats = sTranslations.get();
1127
1149
  const lookupCat = category || "__uncategorized__";
@@ -1181,7 +1203,7 @@ function _walkForTokens(liveRoot, cloneNodes, tokens, indices) {
1181
1203
  if (node.nodeType === Node.ELEMENT_NODE) {
1182
1204
  const el = node;
1183
1205
  if (el.getAttribute("translate") === "no") return;
1184
- if (el.hasAttribute("data-ls-phrase")) return;
1206
+ if (isPhraseMarked(el)) return;
1185
1207
  }
1186
1208
  if (node.hasChildNodes()) {
1187
1209
  _applyStylesToClone(liveRoot, node, [...indices, index]);
@@ -1331,7 +1353,8 @@ var Translate = class {
1331
1353
  return true;
1332
1354
  }
1333
1355
  async handleContentBlock(contentBlock) {
1334
- if (isEmpty(this.custom_id)) {
1356
+ const derivedId = isEmpty(this.custom_id);
1357
+ if (derivedId) {
1335
1358
  this.custom_id = generateCustomId(contentBlock.category, contentBlock.tokens);
1336
1359
  }
1337
1360
  contentBlock.custom_id = this.custom_id;
@@ -1343,6 +1366,17 @@ var Translate = class {
1343
1366
  }
1344
1367
  return;
1345
1368
  }
1369
+ if (derivedId) {
1370
+ const legacyId = generateLegacyCustomId(contentBlock.category, contentBlock.tokens);
1371
+ if (legacyId !== this.custom_id && isContentBlockKnown(contentBlock.category, legacyId)) {
1372
+ this.custom_id = legacyId;
1373
+ if (this.tokens.length > 1 && this.element) {
1374
+ this.translate(Array.from(this.element.childNodes));
1375
+ this.lastTranslatedLocale = currentlyLoadedLocale.get();
1376
+ }
1377
+ return;
1378
+ }
1379
+ }
1346
1380
  void registerContentBlock(contentBlock);
1347
1381
  if (this.tokens.length > 1 && this.element) {
1348
1382
  this.translate(Array.from(this.element.childNodes));
@@ -1358,7 +1392,7 @@ var Translate = class {
1358
1392
  if (node?.nodeType === Node.ELEMENT_NODE) {
1359
1393
  const element = node;
1360
1394
  if (element.getAttribute("translate") === "no") return;
1361
- if (element.hasAttribute("data-ls-phrase")) return;
1395
+ if (isPhraseMarked(element)) return;
1362
1396
  this.translateAttributes(node);
1363
1397
  }
1364
1398
  if (node?.nodeType !== Node.TEXT_NODE) {
@@ -1599,6 +1633,6 @@ var t = ((phrase, ...rest) => {
1599
1633
  });
1600
1634
  var tSignal = LangsysApp.Translations.tSignal;
1601
1635
 
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 };
1636
+ 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
1637
  //# sourceMappingURL=index.mjs.map
1604
1638
  //# sourceMappingURL=index.mjs.map