Automating Magento translations with BingTranslate

The Yireo BingTranslate 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.

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.

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.

<?php
require_once '../app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

 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.

Translation-model

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

<?php
$translator = Mage::getSingleton('bingtranslate/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: https://gist.github.com/yireo/5524504

Product-model

To translate products, the additional model bingtranslate/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('bingtranslate/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('bingtranslate/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.

$productTranslator->getCharCount();

A full example for using this model can be found on the following Gist: https://gist.github.com/yireo/5524123