Vous êtes sur la page 1sur 6

Develop your Magento Store in

Multiple Languages with Magento


Translate System
If you want a single website (read store) to appear in multiple languages while using
Magento, Magento translate system is your answer. You can have a new store view
or setup a new store to incorporate this system.

Go to System>Manage Stores>Create new store/store view/website


Here go to system>configuration>general>locale and then set the language you
want to have the store in.
You can setup translations either by using translation csv files or with inline
translate feature. If you want to use the translation csv files you will need to visit
the path app/locale/en_US. You can introduce translational files for the respective
languages. You can add the translational files for a particular language. You will
need to define the translational file you need to use for a particular module in the
config.xml file
<frontend>
<translate>
<modules>
<Mage_Catalog>
<files>
<default>Mage_Catalog.csv</default>
</files>
</Mage_Catalog>
</modules>
</translate>
</frontend>

You will need to define a translate file to the adminhtml area. Translate files are also
located in the following path
app/design/frontend/default/{theme}/locale/en_US/translate.csv
You can enable the inline translate feature along the following path System ->
Configuration -> Advanced -> Developer -> Translate Inline
Translate System with Custom Modules and Themes
If you want to enable the translate files for custom modules and themes, you will
need to use the function _(..);
echo __('Having Fun?');
echo __('Having Fun? %s','Ya');
echo __('Having Fun? %s %d','Ya',date('h'));
This function is made available in .phtml files, block files, controllers as well as
helper files.
You can even use translation in configuration files
<checkout translate="label" module="checkout">
<label>Checkout</label>
Getting Deeper
How does Magento work on translations? Here are two files that it uses for
translations
Mage_Core_Model_Translate
Mage_Core_Model_Locale
Heres how function _() can be defined
public function __()
{
$args = func_get_args();
$expr = new Mage_Core_Model_Translate_Expr(array_shift($args), $this>getModuleName());
array_unshift($args, $expr);
return $this->_getApp()->getTranslator()->translate($args);
}
The definition is located within
Mage_Core_Block_Abstract,Mage_Adminhtml_Controller_Action
Mage_Core_Controller_Front_Action
Mage_Core_Helper_Abstract
With this the translate function is called upon within the Mage_Core_Model_Translate

The init() function initializes the translations


Step 1: _LoadModuleTranslation
foreach ($this->getModulesConfig() as $moduleName=>$info) {
$info = $info->asArray();
$this->_loadModuleTranslation($moduleName, $info['files'], $forceReload);
}
protected function _loadModuleTranslation($moduleName, $files, $forceReloa
d=false)
{
foreach ($files as $file) {
$file = $this->_getModuleFilePath($moduleName, $file);
$this->_addData($this->_getFileData($file), $moduleName, $forceReloa
d);
}
return $this;
}
protected function _getModuleFilePath($module, $fileName)
{
$file = Mage::getBaseDir('locale');
$file.= DS.$this->getLocale().DS.$fileName;
return $file;
}
Step 2: _LoadThemeTranslation
protected function _loadThemeTranslation($forceReload = false)
{
$file = Mage::getDesign()->getLocaleFileName('translate.csv');
$this->_addData($this->_getFileData($file), false, $forceReload);
return $this;
}
Step 3: LoadDBTranslation();
protected function _loadDbTranslation($forceReload = false)
{
$arr = $this->getResource()->getTranslationArray(null, $this>getLocale());
$this->_addData($arr, $this->getConfig(self::CONFIG_KEY_STORE), $forceR
eload);
return $this;
}
Step 4: addData()
protected function _addData($data, $scope, $forceReload=false)

{
foreach ($data as $key => $value) {
if ($key === $value) {
continue;
}
$key = $this->_prepareDataString($key);
$value = $this->_prepareDataString($value);
if ($scope && isset($this->_dataScope[$key]) && !$forceReload ) {
/**
* Checking previos value
*/
$scopeKey = $this->_dataScope[$key] .
self::SCOPE_SEPARATOR . $key;
if (!isset($this->_data[$scopeKey])) {
if (isset($this->_data[$key])) {
$this->_data[$scopeKey] = $this->_data[$key];
/**
* Not allow use translation not related to module
*/
if (Mage::getIsDeveloperMode()) {
unset($this->_data[$key]);
}
}
}
$scopeKey = $scope . self::SCOPE_SEPARATOR . $key;
$this->_data[$scopeKey] = $value;
}
else {
$this->_data[$key]
= $value;
$this->_dataScope[$key]= $scope;
}
}
return $this;
}
Step 5: Translate the String
protected function _getTranslatedString($text, $code)
{
$translated = '';
if (array_key_exists($code, $this->getData())) {
$translated = $this->_data[$code];
}
elseif (array_key_exists($text, $this->getData())) {
$translated = $this->_data[$text];
}
else {
$translated = $text;
}
return $translated;
}

Working with Magento


Mage_Core_Model_Locale is the class that you will need to concern with. You will find
all the locale and currency within this class
Emulate and revert are the two important functions of this class
Emulate Function
public function emulate($storeId)
{
if ($storeId) {
$this->_emulatedLocales[] = clone $this->getLocale();
$this->_locale
= new Zend_Locale(Mage::getStoreConfig(self::XML_PATH_DEFAULT_LOCALE,
$storeId));
$this->_localeCode = $this->_locale->toString();
Mage::getSingleton('core/translate')->setLocale($this->_locale)>init('frontend', true);
}
else {
$this->_emulatedLocales[] = false;
}
}
Revert Function
public function revert()
{
if ($locale = array_pop($this->_emulatedLocales)) {
$this->_locale = $locale;
$this->_localeCode = $this->_locale->toString();
Mage::getSingleton('core/translate')->setLocale($this->_locale)>init('adminhtml', true);
}
}
You can undo the effect of emulate function with the revert function. While emulate
function is responsible for loading the frontend, revert function is responsible for
loading admin html area.
Conclusion
While this system has a minor bug that you will need to ensure is taken into account
while development or testing of the site, this system helps get a multi language
website on a single website. You can seek translations whenever it is needed, and
help the user view the website in their language.
Note: A backup is essential before you proceed with this coding and modification.

Deepa is a passionate blogger associated with Silver Touch Technologies., a leading


Magento

Development

UK.

She

recommends

checking

out

Magento

Development Services at https://www.silvertouchtech.co.uk. If you are looking


to hire Magento Developer UK then just get in touch with her.