Magento 1.9 Localisation – Translate String from Extension Controller to AJAX Call

extensionslocalisationmagento-1.9

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?

Best Answer

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.

Related Topic