langsys-js-typescript 0.4.0 → 0.4.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
@@ -263,6 +263,9 @@ function isICU(template) {
263
263
  return ICU_PATTERN.test(template);
264
264
  }
265
265
  var ICU_PATTERN = /\{[^{}]+,\s*(plural|select|selectordinal|number|date|time)\s*[,}]/;
266
+ function normalizeMarkupPlaceholders(text) {
267
+ return text.replace(/%([A-Za-z_][A-Za-z0-9_]*)%/g, "{$1}");
268
+ }
266
269
  function interpolate(template, params, locale) {
267
270
  if (isICU(template)) {
268
271
  try {
@@ -1139,7 +1142,7 @@ function tokenizeElement(element) {
1139
1142
  const tokens = [];
1140
1143
  const clone = element.cloneNode(true);
1141
1144
  _walkForTokens(element, Array.from(clone.childNodes), tokens, []);
1142
- return { tokens, content: clone.outerHTML };
1145
+ return { tokens, content: normalizeMarkupPlaceholders(clone.outerHTML) };
1143
1146
  }
1144
1147
  function _writeKnownContentBlockToCache(category, customId) {
1145
1148
  const lookupCat = category || "__uncategorized__";
@@ -1169,7 +1172,7 @@ function _walkForTokens(liveRoot, cloneNodes, tokens, indices) {
1169
1172
  }
1170
1173
  const contentToken = node.nodeValue?.replace(/\s+/g, " ").trim();
1171
1174
  if (node.nodeType === Node.TEXT_NODE && contentToken) {
1172
- tokens.push(contentToken);
1175
+ tokens.push(normalizeMarkupPlaceholders(contentToken));
1173
1176
  return;
1174
1177
  }
1175
1178
  if (!node.hasChildNodes()) return;
@@ -1184,24 +1187,24 @@ function _tokenizeAttributes(element, tokens) {
1184
1187
  }
1185
1188
  for (const attr of TRANSLATABLE_ATTRIBUTES) {
1186
1189
  const value = element.getAttribute(attr)?.trim();
1187
- if (value) tokens.push(value);
1190
+ if (value) tokens.push(normalizeMarkupPlaceholders(value));
1188
1191
  }
1189
1192
  if (VALUE_TRANSLATABLE_ELEMENTS.includes(tagName)) {
1190
1193
  const value = element.getAttribute("value")?.trim();
1191
- if (value) tokens.push(value);
1194
+ if (value) tokens.push(normalizeMarkupPlaceholders(value));
1192
1195
  }
1193
1196
  if (tagName === "input") {
1194
1197
  const inputType = element.getAttribute("type")?.toLowerCase();
1195
1198
  if (inputType && VALUE_TRANSLATABLE_INPUT_TYPES.includes(inputType)) {
1196
1199
  const value = element.getAttribute("value")?.trim();
1197
- if (value) tokens.push(value);
1200
+ if (value) tokens.push(normalizeMarkupPlaceholders(value));
1198
1201
  }
1199
1202
  }
1200
1203
  if (tagName === "select") {
1201
1204
  const options = element.querySelectorAll("option");
1202
1205
  options.forEach((option) => {
1203
1206
  const optionText = option.textContent?.trim();
1204
- if (optionText) tokens.push(optionText);
1207
+ if (optionText) tokens.push(normalizeMarkupPlaceholders(optionText));
1205
1208
  });
1206
1209
  }
1207
1210
  }
@@ -1342,7 +1345,7 @@ var Translate = class {
1342
1345
  }
1343
1346
  if (isEmpty(node.nodeValue?.trim())) return;
1344
1347
  if (isEmpty(node.originalNodeValue)) {
1345
- node.originalNodeValue = node.nodeValue;
1348
+ node.originalNodeValue = normalizeMarkupPlaceholders(node.nodeValue);
1346
1349
  }
1347
1350
  const contentToken = node.originalNodeValue?.replace(/\s+/g, " ").trim();
1348
1351
  if (!contentToken) return;
@@ -1391,7 +1394,7 @@ var Translate = class {
1391
1394
  const optionEl = option;
1392
1395
  if (!optionEl.originalAttributes) optionEl.originalAttributes = {};
1393
1396
  if (optionEl.originalAttributes["textContent"] === void 0) {
1394
- optionEl.originalAttributes["textContent"] = option.textContent || "";
1397
+ optionEl.originalAttributes["textContent"] = normalizeMarkupPlaceholders(option.textContent || "");
1395
1398
  }
1396
1399
  const originalText = optionEl.originalAttributes["textContent"].trim();
1397
1400
  if (originalText) {
@@ -1405,7 +1408,7 @@ var Translate = class {
1405
1408
  const currentValue = element.getAttribute(attr);
1406
1409
  if (!currentValue) return;
1407
1410
  if (element.originalAttributes[attr] === void 0) {
1408
- element.originalAttributes[attr] = currentValue;
1411
+ element.originalAttributes[attr] = normalizeMarkupPlaceholders(currentValue);
1409
1412
  }
1410
1413
  const originalValue = element.originalAttributes[attr].trim();
1411
1414
  if (!originalValue) return;
@@ -1424,7 +1427,7 @@ var SENT_SCAN = /\uE000(\d+)\uE001|\uE002(\d+)\uE003/g;
1424
1427
  function encodeRichText(root) {
1425
1428
  const slots = [];
1426
1429
  const phrase = _encodeNodes(Array.from(root.childNodes), slots);
1427
- return { phrase: phrase.replace(/\s+/g, " ").trim(), slots };
1430
+ return { phrase: normalizeMarkupPlaceholders(phrase.replace(/\s+/g, " ").trim()), slots };
1428
1431
  }
1429
1432
  function _encodeNodes(nodes, slots) {
1430
1433
  let out = "";
@@ -1543,6 +1546,6 @@ var t = ((phrase, ...rest) => {
1543
1546
  });
1544
1547
  var tSignal = LangsysApp.Translations.tSignal;
1545
1548
 
1546
- export { LangsysApp, LangsysAppAPI, Logger, PHRASE_MARKER_ATTR, Phrase, Translate, Translations, canonicalizeLocale, createSignal, currentlyLoadedLocale, encodeRichText, generateCustomId, getValue, interpolate, isContentBlockKnown, isEmpty, isICU, logger, markupTokenValues, md5, persist, reconstitute, registerContentBlock, sTranslations, stripSentinels, t, tSignal, tokenizeElement };
1549
+ export { LangsysApp, LangsysAppAPI, Logger, PHRASE_MARKER_ATTR, Phrase, Translate, Translations, canonicalizeLocale, createSignal, currentlyLoadedLocale, encodeRichText, generateCustomId, getValue, interpolate, isContentBlockKnown, isEmpty, isICU, logger, markupTokenValues, md5, normalizeMarkupPlaceholders, persist, reconstitute, registerContentBlock, sTranslations, stripSentinels, t, tSignal, tokenizeElement };
1547
1550
  //# sourceMappingURL=index.mjs.map
1548
1551
  //# sourceMappingURL=index.mjs.map