How to translate product names in Magento through .csv files

Featured Image

Let’s add to cart one item from our Magento community edition. If you have MCE with sample data you can visit something like this: http://loc.magento.com/sony-vaio-vgn-txn27n-b-11-1-notebook-pc.html?___store=german&___from_store=default
Add item to your cart and you’ll see success message “Sony VAIO VGN-TXN27N/B 11.1″ Notebook PC was added to your shopping cart.”. Now if we add in our .csv file for de_DE translations something like this:

"%s was added to your shopping cart.","%s wurde zum Warenkorb hinzugefügt."
"Sony VAIO VGN-TXN27N/B 11.1" Notebook PC","Sony Deutsch VAIO VGN-TXN27N/B"

If you have already setup all required things for “German” translations, then try again buy same product and you will not be able to see our translated “success message”. It’s not only because of “"” in product name.

Let’s find and change (not recommended) in app/code/core/Mage/Checkout/controllers/CartController.php “public function addAction()”, lines

$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->htmlEscape($product->getName()));
$this->_getSession()->addSuccess($message);

to

$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->__(Mage::helper('core')->htmlEscape($product->getName())));
$this->_getSession()->addSuccess($message);

And now if you buy same item you will get your translations for products for success message.
Of course, Magento has it’s own way how you can translate products through admin area (Catalog/Manage products/Edit and you can change from drop-down menu “Choose Store View:” to something and change name of product), but I like have everything on one place. If you are new to Magento let’s do step by step instructions for setting up translations for products through .csv file

1) create a file in app/code/local/Inchoo/Translations/etc/config.xml with the following content:

<?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <Inchoo_Translations>
            <version>1.0.0</version>
        </Inchoo_Translations>
    </modules>
    <frontend>
        <translate>
            <modules>
                <Inchoo_Translations>
                    <files>
                        <default>InchooTranslations.csv</default>
                    </files>
                </Inchoo_Translations>
            </modules>
        </translate>
    </frontend>
</config>

2) tell system about module. You can do that by creating a file in app/etc/modules/Inchoo_Translations.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Inchoo_Translations>
            <active>true</active>
            <codePool>local</codePool>
        </Inchoo_Translations>
    </modules>
</config>

3) Let’s create our translations for German in .csv file. Create file in app/locale/de_DE/InchooTranslations.csv with the following content:

"%s was added to your shopping cart.","%s wurde zum Warenkorb hinzugefügt"
"Sony VAIO VGN-TXN27N/B 11.1" Notebook PC","Sony Deutsch VAIO VGN-TXN27N/B"

4-a) Let’s rewrite addAction() method. Copy file from app/code/core/Mage/Checkout/controllers/CartController.php to app/code/local/Inchoo/Checkout/controllers/CartController.php
and delete everything accept addAction() method. Add this lines on the top of your CartController.php file:

<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Inchoo_Checkout_CartController extends Mage_Checkout_CartController
{

and at the end of the file you need close curly bracket (}). Now we have our addAction() method. We need change:

$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->htmlEscape($product->getName()));
$this->_getSession()->addSuccess($message);

to

$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->__(Mage::helper('core')->htmlEscape($product->getName())));
$this->_getSession()->addSuccess($message);

4-b) tell system about rewrite. Create file in app/code/local/Inchoo/Checkout/etc/config.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<modules>
		<Inchoo_Checkout>
			<version>1.0.0</version>
		</Inchoo_Checkout>
	</modules>
	<frontend>
		<routers>
			<checkout>
				<args>
					<modules>
						<sintax before="Mage_Checkout">Inchoo_Checkout</sintax>
					</modules>
				</args>
			</checkout>
		</routers>
	</frontend>
</config>

We are almost done! This is Magento so we need tell system to load our module.
4-c) tell system about module. You can do that by creating a file in app/etc/modules/Inchoo_Checkout.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Inchoo_Checkout>
            <active>true</active>
            <codePool>local</codePool>
        </Inchoo_Checkout>
    </modules>
</config>

5) go to admin/system/configuration and from “Current Configuration Scope:” chose german. In “Locale options” / “Locale” and uncheck “Use Website” [STORE VIEW]

That’s it. You can now translate your products name from .csv files.

One Note.
If you have some product with eng. name “abcd” and if you already through admin area (Catalog/Manage products/Edit /”Choose Store View:”) change name of product for German to “defg”.
And in .csv file you have something like this “defg”,”GerDEFG”, as resoult you will get GerDEFG as name of product.
This line $message = $this->__(‘%s was added to your shopping cart.’, Mage::helper(‘core’)->__(Mage::helper(‘core’)->htmlEscape($product->getName()))); will basically do next:
a) look for product name for chosen store view (if exist, else take default product name)
b) result from previous try to translate from .csv file
c) Interpolate translation of “was added to your shopping cart” from .csv file with b)


10 comments

  1. Uupps… again

    <?php echo $this->__('<a href="%s">%s Item(s)</a>', $this->getUrl('checkout/cart'), $_cartQty) ?>
    "<a href=""%s"">%s Items</a>","<a href=""%s"">%s artikal(a)</a>"
  2. Hello Ivan,
    This is very good article, helped me a lot…
    But still I have one problem with translation. I tried to find solution on the Net, but without success.
    I have small cart box in upper right corner.
    In the .phtml file expression is:
    __(‘%s Item(s)‘, $this->getUrl(‘checkout/cart’), $_cartQty) ?>

    and i tried many diferenr solutions, none worked:
    for examlpe this should work, but it doesn’t:
    (translation.csv)
    %s Items“,”%s artikal(a)

    Do you have any ideas?
    I did the ‘clear cache’.. 😉

  3. Hi Cheaong…

    It’s sometimes in Magento impossible to change only one model and expect that everything will work fine. And never expect that changes should be in .phtml files.
    You can easily do next:
    In your phtml file you can write:

    echo get_class($this);
    exit;

    at the top of your phtml file. By this you’ll get on screen class name of Block, something like: Mage_Core_Block…_Something.

    Then you can find that block and see which model it calls. Then try to modify model, and rewrite method getTitle() if exist to public function getTitle() { return $this->__(parent::getTitle());}…. I hope you see now how to find your answer.

    GL

  4. Is it possible to apply the similar way to custom options?

    I tried to modify select.phtml in template:

    htmlEscape($_option->getTitle()) ?>

    to

    htmlEscape($this->__($_option->getTitle())) ?>

    But that way will make it works for the dropdown in product detail page only. Which model should be extended in order for the translation to work for entire site?

    Thanks!
    Cheong ES

  5. Sry for taking me so long…

    Try to add in app/locale/en_US/Mage_Catalog.csv at first line “Furniture”, “Yeap” and save csv file (Magento Sample Data).

    Then open app/code/core/Mage/Catalog/Block/Navigation.php and around line 287. change line:
    from:

    $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';

    to

    $html[] = '<span>' . $this->escapeHtml($this->__($category->getName())) . '</span>';

    in protected function _renderCategoryMenuItemHtml(…) method.

    Disable caches, refresh home page and you should see “Yeap” instead of “Furniture” at the top of your Magento with sample data.

  6. Hi Ivan,
    thanks for your explanation. Very helpful.
    I’ve a question, could be possible use the same strategy, and translate category names using a .csv file?
    In this case, which model should I extend? Could you give me a short example?

    Many thanks,
    Carles

  7. Once again you helped me on another soon to-do Magento project! I love your blog and it has now moved to the top of my pulse reader for my droid. Keep up the amazing work! It is much appreciated!
    -BigJon

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.