cap icon indicating copy to clipboard operation
cap copied to clipboard

Interpolation between CapJS and Angular translate

Open jcugmas opened this issue 1 year ago • 5 comments

Hi,

Hope you're well and thank you again for this project

I'm coming back for another trouble I'm facing with CapJS standalone and more precisely the widget in the frontend side. I have integrated the cap widget tag on my Angular application using with TranslateService for handling multiple languages. So I have something like this :

<cap-widget data-cap-api-endpoint="{{captchaUrl}}"
                        data-cap-i18n-verifying-label="{{'common_form_labels.captchaVerify'|translate}}"
                        data-cap-i18n-initial-state="{{'common_form_labels.captchaState'|translate}}"
                        data-cap-i18n-solved-label="{{'common_form_labels.captchaState'|translate}}"
                        data-cap-i18n-error-label="{{'common_buttons.error'|translate}}">
                        </cap-widget>

All the attributes are working except data-cap-i18n-initial-state, so when I arrive to the concerning page I have the foloiwing :

Image

What I suspect is that createUI method into the widget is called before that Angular interpolate translate values while it's working for the other attributes like data-cap-i18n-solved-label.

My suggestion is maybe to add data-cap-i18n-initial-state in the static observedAttributes method and in attributeChangedCallback to update html text of the CapWidget html element. Some thing like this (it's not refactored)

if (name == "data-cap-i18n-initial-state") {
        this.#div.innerHTML = `<div class="checkbox"></div><p>${this.getI18nText(
          "initial-state",
          "I'm a human"
        )}</p>`;
      }

What do you think about that ?

Thanks and have a good day

Best regards

jcugmas avatar Jun 02 '25 14:06 jcugmas

Is a new tag of standalone is required or not ? I'm using standalone with docker

Thanks

jcugmas avatar Jun 02 '25 21:06 jcugmas

Hi,

Sorry I was in vacation the last few days. I update my image but I get an error in my browser when I navigate to my page containing the captcha :

Uncaught TypeError: this[#s] is undefined
    attributeChangedCallback widget.js:1

It seems it's was thrown by this line :

 this.#div.querySelector("p").innerText = this.getI18nText(
          "initial-state",
          "I'm a human"
        );

What's the problem here you think ?

Thanks

jcugmas avatar Jun 06 '25 07:06 jcugmas

Weird, I have updated the my standalone image but I have still the error. Maybe something from the bundle webpack but the script is included by a script tag outside of npm modules.

I keep looking into it this week

Best regards

jcugmas avatar Jun 10 '25 07:06 jcugmas

I think it's coming from a timing conflict between the DOM generation of the div element of the capatcha and the initialization of the widget script. After debug the this[#s] is corresponding to the #div and it's thrown in the new block I have suggested to add :

      if (name === "data-cap-i18n-initial-state") {
        this.#div.querySelector("p").innerText = this.getI18nText(
          "initial-state",
          "I'm a human"
        );
      }

Maybe it will be safe to add a check before like

      if (name === "data-cap-i18n-initial-state") {

        if(!this.#div)
          return;
        
        this.#div.querySelector("p").innerText = this.getI18nText(
          "initial-state",
          "I'm a human"
        );
      }

Or a await somewhere, don't you think ?

Thanks

jcugmas avatar Jun 10 '25 10:06 jcugmas

Thanks for the add, it's working for me now

jcugmas avatar Jun 10 '25 12:06 jcugmas