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>

Develop Your Magento Store in Multiple Languages With Magento Translate System

Embed Size (px)

DESCRIPTION

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.

Citation preview

Develop your Magento Store in Multiple Languages with Magento Translate SystemIf 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/websiteHere 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

Mage_Catalog.csv

You will need to define a translate file to the adminhtml area. Translate files are also located in the following pathapp/design/frontend/default/{theme}/locale/en_US/translate.csvYou can enable the inline translate feature along the following path System -> Configuration -> Advanced -> Developer -> Translate Inline

Translate System with Custom Modules and ThemesIf 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

Getting DeeperHow does Magento work on translations? Here are two files that it uses for translationsMage_Core_Model_TranslateMage_Core_Model_LocaleHeres how function _() can be definedpublicfunction__(){$args= func_get_args();$expr=newMage_Core_Model_Translate_Expr(array_shift($args),$this->getModuleName());array_unshift($args,$expr);return$this->_getApp()->getTranslator()->translate($args);}

The definition is located withinMage_Core_Block_Abstract,Mage_Adminhtml_Controller_ActionMage_Core_Controller_Front_ActionMage_Core_Helper_Abstract With this the translate function is called uponwithin the Mage_Core_Model_TranslateThe init() function initializes the translations

Step 1: _LoadModuleTranslationforeach($this->getModulesConfig()as$moduleName=>$info) {$info=$info->asArray();$this->_loadModuleTranslation($moduleName,$info['files'],$forceReload);}protectedfunction_loadModuleTranslation($moduleName,$files,$forceReload=false){foreach($filesas$file) {$file=$this->_getModuleFilePath($moduleName,$file);$this->_addData($this->_getFileData($file),$moduleName,$forceReload);}return$this;}protectedfunction_getModuleFilePath($module,$fileName){$file= Mage::getBaseDir('locale');$file.= DS.$this->getLocale().DS.$fileName;return$file;}

Step 2: _LoadThemeTranslation

protectedfunction_loadThemeTranslation($forceReload= false){$file= Mage::getDesign()->getLocaleFileName('translate.csv');$this->_addData($this->_getFileData($file), false,$forceReload);return$this;}

Step 3: LoadDBTranslation();

protectedfunction_loadDbTranslation($forceReload= false){$arr=$this->getResource()->getTranslationArray(null,$this->getLocale());$this->_addData($arr,$this->getConfig(self::CONFIG_KEY_STORE),$forceReload);return$this;}

Step 4: addData()protectedfunction_addData($data,$scope,$forceReload=false){foreach($dataas$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 Stringprotectedfunction_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 classEmulate and revert are the two important functions of this classEmulate Functionpublicfunctionemulate($storeId){if($storeId) {$this->_emulatedLocales[] =clone$this->getLocale();$this->_locale =newZend_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 Functionpublicfunctionrevert(){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.ConclusionWhile 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.