0

I am trying to add translate code <?= __('This field is required') ?> in the Magento file using JavaScript but it is not working plz help me.

if(response.length == 0) {
    document.getElementById('test').innerHTML = '<span style="color:red;">'<?php __('This field is required') ?>'</span>';
    return false;
}

Are any quotes missing or not calling PHP correctly inside JavaScript quotes?

2
  • is this is pure js file or phtml? Oct 8, 2021 at 10:58
  • @RizwanKhan phml file
    – User0434
    Oct 8, 2021 at 12:11

1 Answer 1

0

You need to use something like this.

<script type="text/javascript">
        require(['mage/translate'], function($t)
        {
             
             $t("Your Message Which Need to translate.");
    });
</script>
5
  • is it not possible to add in my code phtml file.........without adding seperate function
    – User0434
    Oct 8, 2021 at 12:10
  • Yes you can added this code into your phtml file it self. but yes "require" code is necessary. Oct 8, 2021 at 12:22
  • my code is working fine without adding php....after adding php only not working.... why require is necessary it is normal php insertion in javascript right ..this is phtml file
    – User0434
    Oct 8, 2021 at 12:44
  • the javascript is required to added translation object over there, then and then it will translate your string. Oct 8, 2021 at 13:26
  • anyhow my code is working after changing single quotes to double quotes in php tag
    – User0434
    Oct 8, 2021 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.