8

Is there a method to know which translation CSV file(s) are used in the current phtml template file?

3

I assume you mean translations by $this->__('...') where no explict helper is used. Then it depends on the block's module scope

This depends on multiple factors:

  1. The block class being used to render the template. You should be able to determine it from the layout XML files, but some blocks are created programmatically, in this case you'll have to search in the code. If you are lucky, the block class is documented in the template itself, like this:

    /** @var $this Mage_Catalog_Block_Product_List */
    

    or like this:

    /** @see Mage_Catalog_Block_Product_List */
    

    The scope is the module this class belongs to (like Mage_Catalog). But be careful with class rewrites. If the block class has been rewritten by an extension, the scope changes to this extension.

  2. If the property module_name has been set (for example via layout XML), this takes precedence.
  3. Finally, if the block overrides the getModuleName() method, the scope returned from this method is used.

Are we there yet?

Now that you know the scope, you can look up the according CSV file, that is the one that is defined in this module's config.xml. It could also be more than one, but the generally followed convention is to have one file per module in the form Module_Name.csv.

  1. If this file does contains the string to be translated, you found it, except if you have a translate.csv in your theme, that overrides the translation for this exact scope (like: "Module_Name::Foo","Foo").

  2. If it's not in both, look for the translation without module scope prefix in translate.csv (if it exists).

  3. If it's not there, search in all other module CSV files. The one from the module that is loaded first, wins (i.e. core modules first, then alphabetically, respecting dependencies). Note that this fallback only is used when not in developer mode.


If you want to be sure, from which files a specific translation on the page is coming from, I can recommend you the free extension TranslationHints (DISCLAIMER: I wrote it)

Get it here: https://github.com/schmengler/TranslationHints

Screenshot: Translation Hints

2

Unless you're unit testing, you usually don't really care which translation csv is being used because they all get merged before Magento begins translation of anything.

However if you still want to figure it out, first use the layout xml files to figure out what block class is being used. If you find the block class, then it will use the config.xml of that module. If no block class is specified in the layouts you might need to go looking in the parent block classes and controllers.

The main thing to know is that the templates use the __ translation function of their associated block class, and the block class uses its module Data.php helper class's __ function.

  • 1
    "Usually you don't really care which translation csv is being used because they all get merged before Magento begins translation of anything." -- Unless unit testing for an existing translation package. My modules usually fail if English, Spanish, or French are missing before making it the QA team. – Luke A. Leber Aug 22 '16 at 0:46
1

It sounds like you want to stay organized when you add your translations. With so many CSV files for translation, why choose a random file, why not add it to the right CSV file?

Here's an example I was working on: Suppose you are editing the customer account page, and you have changed Address book to My Addresses. Now there's no French translation showing for My Addresses.

The way to find the right CSV file is to search for an existing translation in the same part of the site. The item My Orders / Mes Commandes is in the same part of the site, so I can run a search:

$ cd magento/app/
$ find . -iname '*.csv' | xargs grep -in --color 'Mes Commandes'
./locale/fr_FR/Mage_Sales.csv:73:"Back to My Orders","Retour à mes commandes"
./locale/fr_FR/Mage_Sales.csv:308:"My Orders","Mes commandes"
./locale/fr_FR/Mage_Sales.csv:309:"My Orders - Details","Mes commandes - Détails"
./locale/fr_FR/Mage_Oscommerce.csv:7:"Back to My Orders","Retour à mes commande "
./locale/fr_FR/Mage_Oscommerce.csv:51:"My Previous Orders","Mes commandes précédentes"
./locale/fr_FR/Mage_Customer.csv:215:"My Orders","Mes commandes"

So, looking at this with a little bit of guessing, the best file to modify is locale/fr_FR/Mage_Sales.csv. After making changes, don't forget to refresh Translations under System > Cache Management!

Aside: note the case-insensitive options; -iname for find, and -i for grep. These reduce the effort you need to spend hunting. The --color option doesn't reflect here, but the output will be much more readable compared to what you see here.

0

The __() translation method used in the templates is in the file /app/code/core/Mage/Core/Block/Abstract.php and takes the module name to find the correct translation. It builds an array in the following format to be used for translation:

array(1) {
    [0]=> object(Mage_Core_Model_Translate_Expr)#161 (2) {
        ["_text":protected]=> string(18) "Orders and Returns"
        ["_module":protected]=> string(10) "Mage_Sales"
    }
}

If you want to see what module name your template is using you are able to call a function:

 $this->getModuleName();

There should then be a translations file matching this string. For example for me on the category view page this function will return *Mage_Catalog* and then from this I can find the translation file Mage_Catalog.csv

  • Which line in this big Abstract.php file ? – Vicky Dev Aug 29 '16 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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