0

I want to use my local theme's locale csv file to translate a status message that comes from a plugin and is called in the theme via AJAX. I'm not sure how or where though.

My plugin creates 'packages' of several existing products and shows them as "Joint deals" on a product page. In the CartController.php this response is made:

        $response = $this->prepareResponse([
            'status' => 'SUCCESS',
            'message' => 'Your package was added to cart',
        ]);

In my product view.phtml the response is called via an AJAX call like this:

        try {
            $j.ajax({
                 ...
                success: function(data){
                    $j('product-a2c-popup popup-content span.content').text(data.message);

So in other words the message is taken from the response straight from the extension. But how/when would I wrap that message in the echo $this->__ format? If I do it in the extension itself, how would the mark-up be seeing as it is written in an array?

1
  • Can you please check answer from below for cleaner solution?
    – sv3n
    Commented Jul 22, 2017 at 2:39

2 Answers 2

2

You can try below code.

Option 1:

$response = $this->prepareResponse([
    'status' => 'SUCCESS',
    'message' => $this->__('Your package was added to cart'),
]);

Option 2

If above code is not working then you can use below. Add below in your view.phtml before your javascript code

<script type="text/javascript">
Translator.add('Your package was added to cart','<?php echo $this->__("Your package was added to cart")?>');
</script>

Now in your Ajax code.

try {
    $j.ajax({
         ...
        success: function(data){
            $j('product-a2c-popup popup-content span.content').text(Translator.translate(data.message));

You can also make sure by echoing <?php echo $this->__("Your package was added to cart")?> in phtml if the translation is working.

6
  • Ok, this doesn't break the extension (that's something), but when I put this string in my locale.csv, it doesn't pick up the translation. Shouldn't it be bound to a specific textdomain or something?
    – Asitis
    Commented May 22, 2017 at 13:37
  • Can you add details about how you are adding your csv file. Commented May 22, 2017 at 13:59
  • It's in my theme, in the app/design/frontend/theme/package/locale/nl_NL/translate.csv < this is not the issue, all other translations in there do work.
    – Asitis
    Commented May 23, 2017 at 6:46
  • Are you sure you are using same string? Please make sure there are no blank spaces, dot or special characters is there for the string "Your package was added to cart" Commented May 23, 2017 at 6:47
  • I know how the translations work ;) Yes, it is the exact same.
    – Asitis
    Commented May 23, 2017 at 7:20
1

Try below code

$response = $this->prepareResponse([
    'status' => 'SUCCESS',
    'message' => Mage::helper('checkout')->__('Your package was added to cart'),
]);
1
  • Ok, this doesn't break the extension (that's something), but when I put this string in my locale.csv, it (still) doesn't pick up the translation?
    – Asitis
    Commented May 22, 2017 at 13:38

Your Answer

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

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