Automating Magento translations with GoogleTranslate

The Yireo GoogleTranslate extension can be used to translate Magento products, Magento categories and Magento CMS-pages from within the backend. But the API classes also allow for scripted translations. This whitepaper discussed the various possibilities.

Basic code for a Magento script

Some Magento tasks are better not to run from within the Magento Admin Panel: Memory consumption might be too high, timeouts might occur, etcetera. An alternative is to write a simple PHP-script, let it call upon the Magento application and run that script through a shell or from the web.

The first step is to create an empty PHP script that calls upon the Magento file Mage.php, which contains the startup procedure for the Magento application. Next, the Magento application is actually started using the Mage::app() statement.

require_once '../app/Mage.php';

 Every time the Magento application is started, it starts with a specific scope (Website, Store, Store View). By default, the scope of the default Store View will be loaded. But when trying to save objects to the Magento database (which is seen as an administrative task), it is better to load the admin scope instead. The method setCurrentStore() is used to make this happen.

Warning on translating attributes

Make sure to take note that every product attribute is assigned to a specific scope. For instance, a color attribute can be assigned to the Store View scope. This means that you can manage the color value of a product on the global level, but also override this value on the Store View level. However, take for instance the sku attribute, which is assigned to the Global scope. This makes it impossible to override this value in the Store View level. While editing a product, you might be able to switch to the Store View level and modify the SKU. However the [GLOBAL] right from the input, indicates that you will be modifying the attribute globally, across all scopes, across all Store Views.

This mentioning of how attributes work with scopes is not related to our extension but to Magento itself. If this is new to you, you might be missing out on more important hints about the Magento architecture. Make sure to take the time to go through the official Magento documentation.

Translation model

By using Magento code, you can also apply GoogleTranslate for automated translations. The following PHP segment gives an example of this: The model identifier googletranslate/translator gives direct access to the translation model:

$translator = Mage::getSingleton('googletranslate/translator');
$translation = $translator->translate($text, $source, $destination);
if(!empty($translation)) {
echo $translation."\n";
} else {
echo $translator->getApiError()."\n";

A full example for using this model can be found on the following Gist:


To translate products, the additional model googletranslate/product can be used to translate a product entirely. The class contains a method translate with 3 arguments: The product object, an array of product attributes and an array of the Store Views:

$storeCodes = array('french', 'german', 'danish', 'dutch');
$productAttributes = array('name', 'short_description', 'description');
$productTranslator = Mage::getModel('googletranslate/product');
$productTranslator->translate($product, $productAttributes, $storeCodes);

To translate various products, simply load a Magento product-collection and call upon the $productTranslator within a loop:

$productCollection = Mage::getModel('catalog/product')->getCollection();
foreach($productCollection as $product) {
$productTranslator = Mage::getModel('googletranslate/product');
$productTranslator->translate($product, $productAttributes, $storeCodes);

The product translator also contains a method getCharCount() which contains the total number of characters being translated for that specific product. It basically contains an enumeration of all characters of all product values of all product attributes of the product you're translating. This value can be important if you want to keep below a specific daily limit.


A full example for using this model can be found on the following Gist: