How to create custom language translation CSV file for your module in Magento?

If you are a Magento Developer then this article shows how to you create and use custom language translation CSV file for your module. Since Magento has built in functionality for translations, you can internationalize your module with different languages.

Note: You need to keep in mind that to make translation work in Magento, you should never write bare strings anywhere in your module. Seen example below.

Wrong method!

<?php

echo "Lorem ipsum dolor sit amet";

?>

 

Magento built in translation method.

<?php

echo $this->__('Lorem ipsum dolor sit amet');

?>

This method will look into the translation CSV file of you module and will replace the string to its complementary value. We will provide one small example for this article and realize on simple module, seen slideshow module free. https://magenmarket.com/ma2-slideshow-widget-module-free.html Look Slideshow module files via directory.

app
  + code
    + local
      + Ma2
        + Slideshow
          + Block
            + Helper
            + Model
            + controller
            + etc
              + config.xml
              ...
            + sql
  + design
    + frontend
      + your_package_name  (package)
        + your_theme_name  (theme)
          + template
            + ma2slideshow
              + slideshow.phtml
  + etc
    + modules
      + Ma2_Slideshow.xml
skin
  + frontend
    + your_package_name
      + your_theme_name
        + css
          + ma2slideshow
        + js
          + ma2slideshow

1. Create the translation CSV file

Go to directory app/locale/LANGUAGE_CODE. By default, the LANGUAGE_CODE is en_US. You have to create a new CSV file with name Ma2_Slideshow.csv. The CSV file should contain the translation strings in the key-value pair format as image below.

csv file csv file

2. Define translation CSV file for this module

Open config.xml file via directory

app
  + code
    + local
      + Ma2
        + Slideshow
          ...
          + etc
            ...
            + config.xml

Write the following code specifying the language translation CSV file.

<?xml version="1.0"?>
<config>
    <frontend>
        ...
        <translate>
            <modules>
                <Ma2_Slideshow>
                    <files>
                        <default>Ma2_Slideshow.csv</default>
                    </files>
                </Ma2_Slideshow>
            </modules>
        </translate>
    </frontend>
    <adminhtml>
        ...
        <translate>
            <modules>
                <Ma2_Slideshow>
                    <files>
                        <default>Ma2_Slideshow.csv</default>
                    </files>
                </Ma2_Slideshow>
            </modules>
        </translate>
    </adminhtml>
        ...
</config>

 

Look image below.

Define translation CSV file Define translation CSV file

I have translate to Español (España) language for this module. Seen more details.

Backend translations

The image is English version

Backend English version Backend English version

The create Ma2_Slideshow.csv file via directory app->locale->es_ES for Spannish vesion. Seen image below

Spanish csv file The create Spanish translations csv file

Please come back Admin section and choose Interface Locale  for Spanish language translations. Look image below

Choose language interface locale Choose language interface locale

result

Backend demo for Spanish language translations Backend demo for Spanish language translations

 Frontend translations

First open template .phtml file and select a string what you want to translate. Seen image below I'm add a string to template .phtml file for test translation.

Add a string in template .phtml file to test frontend translations Add a string in template .phtml file to test frontend translations
Frontend demo string to translations Frontend demo string to translations

Come back Admin section, go to System -> Manage Stores and click Create Store View button. After insert requirement inputs and click Save Store View button. Look image below.

Create store Create store

Go to System -> Configuration -> General -> General -> Locale Options and choose Spanish(Spain) on Locale selection field and click Save Config button.

Current configuration scope Current configuration scope

Go to CMS -> Widgets tab and click Add New Widget Instance button to create a new module Ma2 Slideshow and Assign to Store Views with store is Spanish. Come back step create Ma2_Slideshow.csv file and look first line, you can see string to test translation on frontend ("Ma2 slideshow language","Ma2 slideshow language spanish"). Back to frontend and refresh your page.

Frontend translation Frontend translation