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?

Dhiren Vasoya
9,70914 gold badges37 silver badges61 bronze badges
asked Oct 8, 2021 at 10:44
2
  • is this is pure js file or phtml? Commented Oct 8, 2021 at 10:58
  • @RizwanKhan phml file Commented 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>
answered Oct 8, 2021 at 10:58
5
  • is it not possible to add in my code phtml file.........without adding seperate function Commented Oct 8, 2021 at 12:10
  • Yes you can added this code into your phtml file it self. but yes "require" code is necessary. Commented 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 Commented Oct 8, 2021 at 12:44
  • the javascript is required to added translation object over there, then and then it will translate your string. Commented Oct 8, 2021 at 13:26
  • anyhow my code is working after changing single quotes to double quotes in php tag Commented Oct 8, 2021 at 14:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.