@pure-ds/core 0.7.36 → 0.7.37

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.
@@ -11,10 +11,22 @@ const __localizationState = {
11
11
  reconcileTimer: null,
12
12
  requestedKeys: new Set(),
13
13
  textNodeKeyMap: new WeakMap(),
14
+ attributeKeyMap: new WeakMap(),
14
15
  valueToKeys: new Map(),
15
16
  missingWarnings: new Set(),
16
17
  };
17
18
 
19
+ const __LOCALIZABLE_ATTRIBUTES = [
20
+ "title",
21
+ "placeholder",
22
+ "aria-label",
23
+ "aria-description",
24
+ "aria-placeholder",
25
+ "aria-roledescription",
26
+ "alt",
27
+ "label",
28
+ ];
29
+
18
30
  const __isStrTagged = (val) =>
19
31
  Boolean(val) && typeof val !== "string" && typeof val === "object" && "strTag" in val;
20
32
 
@@ -599,10 +611,135 @@ async function __localizeRequestedTextNodes() {
599
611
  }
600
612
  }
601
613
 
614
+ function __getElementAttributeKeyMap(element) {
615
+ let map = __localizationState.attributeKeyMap.get(element);
616
+ if (!map) {
617
+ map = new Map();
618
+ __localizationState.attributeKeyMap.set(element, map);
619
+ }
620
+ return map;
621
+ }
622
+
623
+ async function __localizeAttribute(element, attrName) {
624
+ if (!element || typeof element.getAttribute !== "function") {
625
+ return;
626
+ }
627
+
628
+ const rawValue = element.getAttribute(attrName);
629
+ if (typeof rawValue !== "string" || !rawValue.length) {
630
+ return;
631
+ }
632
+
633
+ const keyMap = __getElementAttributeKeyMap(element);
634
+ let key = keyMap.get(attrName) || null;
635
+
636
+ if (!key || !__localizationState.requestedKeys.has(key)) {
637
+ key = __findRequestedKeyForText(rawValue);
638
+ }
639
+
640
+ if (!key) {
641
+ const segmentMatch = __findRequestedSubsegmentForText(rawValue);
642
+ if (!segmentMatch) {
643
+ return;
644
+ }
645
+
646
+ const scopedLocale = __resolveContextLocale({ element });
647
+ await __loadLocaleInternal(scopedLocale, "attribute");
648
+
649
+ const translated = __resolveTranslation(segmentMatch.key, segmentMatch.values, { element }, null);
650
+ const translatedText = segmentMatch.values.length
651
+ ? __replacePlaceholders(translated, (index) => segmentMatch.values[index])
652
+ : translated;
653
+
654
+ const localizedValue =
655
+ rawValue.slice(0, segmentMatch.start) +
656
+ translatedText +
657
+ rawValue.slice(segmentMatch.end);
658
+
659
+ if (localizedValue !== rawValue) {
660
+ element.setAttribute(attrName, localizedValue);
661
+ }
662
+
663
+ keyMap.set(attrName, segmentMatch.key);
664
+ return;
665
+ }
666
+
667
+ keyMap.set(attrName, key);
668
+
669
+ const scopedLocale = __resolveContextLocale({ element });
670
+ await __loadLocaleInternal(scopedLocale, "attribute");
671
+
672
+ const values = __resolveTemplateValuesForText(key, rawValue);
673
+ const translated = __resolveTranslation(key, values, { element }, null);
674
+ const translatedText = values.length
675
+ ? __replacePlaceholders(translated, (index) => values[index])
676
+ : translated;
677
+
678
+ if (translatedText !== rawValue) {
679
+ element.setAttribute(attrName, translatedText);
680
+ }
681
+ }
682
+
683
+ async function __localizeRequestedAttributes() {
684
+ if (typeof document === "undefined" || __localizationState.requestedKeys.size === 0) {
685
+ return;
686
+ }
687
+
688
+ const root = document.body || document.documentElement;
689
+ if (!root) {
690
+ return;
691
+ }
692
+
693
+ const roots = [];
694
+ const seenRoots = new Set();
695
+
696
+ const addRoot = (candidateRoot) => {
697
+ if (!candidateRoot || seenRoots.has(candidateRoot)) {
698
+ return;
699
+ }
700
+
701
+ seenRoots.add(candidateRoot);
702
+ roots.push(candidateRoot);
703
+ };
704
+
705
+ addRoot(root);
706
+
707
+ for (let index = 0; index < roots.length; index += 1) {
708
+ const currentRoot = roots[index];
709
+ if (!currentRoot || typeof currentRoot.querySelectorAll !== "function") {
710
+ continue;
711
+ }
712
+
713
+ const elements = currentRoot.querySelectorAll("*");
714
+ for (const element of elements) {
715
+ const shadowRoot = element?.shadowRoot;
716
+ if (shadowRoot) {
717
+ addRoot(shadowRoot);
718
+ }
719
+ }
720
+ }
721
+
722
+ for (const scanRoot of roots) {
723
+ if (!scanRoot || typeof scanRoot.querySelectorAll !== "function") {
724
+ continue;
725
+ }
726
+
727
+ const elements = scanRoot.querySelectorAll("*");
728
+ for (const element of elements) {
729
+ for (const attrName of __LOCALIZABLE_ATTRIBUTES) {
730
+ if (element.hasAttribute(attrName)) {
731
+ await __localizeAttribute(element, attrName);
732
+ }
733
+ }
734
+ }
735
+ }
736
+ }
737
+
602
738
  async function __reconcileLocalization() {
603
739
  const detectedLocales = __collectDetectedLocales();
604
740
  await __ensureDetectedLocalesLoaded(detectedLocales);
605
741
  await __localizeRequestedTextNodes();
742
+ await __localizeRequestedAttributes();
606
743
  __pruneUndetectedLocales(detectedLocales);
607
744
  }
608
745
 
@@ -748,6 +885,7 @@ export function configureLocalization(config = null) {
748
885
  __localizationState.loadingByLocale.clear();
749
886
  __localizationState.requestedKeys.clear();
750
887
  __localizationState.textNodeKeyMap = new WeakMap();
888
+ __localizationState.attributeKeyMap = new WeakMap();
751
889
  __localizationState.valueToKeys.clear();
752
890
  __localizationState.missingWarnings.clear();
753
891