Last active
September 29, 2015 23:47
-
-
Save ArnaudLigny/1688003 to your computer and use it in GitHub Desktop.
Use Gettext to translate Magento modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Narno Gettext Translate model | |
* (overlap of Mage_Core_Model_Translate) | |
* | |
* Support Gettext file (binary). | |
*/ | |
class Narno_Gettext_Model_Translate extends Mage_Core_Model_Translate | |
{ | |
[...] | |
/** | |
* Config file: translate files types | |
*/ | |
const TYPE_DEFAULT = 'csv'; // xml path: translate/modules/{My_Module}/files/default | |
const TYPE_CSV = 'csv'; // xml path: translate/modules/{My_Module}/files/csv | |
const TYPE_MO = 'mo'; // xml path: translate/modules/{My_Module}/files/mo | |
[...] | |
[...] | |
/** | |
* Loading data from module translation files | |
* | |
* @param string $moduleName | |
* @param string $files | |
* @return Mage_Core_Model_Translate | |
*/ | |
protected function _loadModuleTranslation($moduleName, $files, $forceReload=false) | |
{ | |
foreach ($files as $type=>$file) { | |
$file = $this->_getModuleFilePath($moduleName, $file); | |
$this->_addData($this->_getFileData($file, $type), $moduleName, $forceReload); | |
} | |
return $this; | |
} | |
[...] | |
[...] | |
/** | |
* Retrieve data from file | |
* | |
* @param string $file | |
* @param string $type ("default", "csv" or "mo") | |
* @return array | |
*/ | |
protected function _getFileData($file, $type) | |
{ | |
$data = array(); | |
if (file_exists($file)) { | |
switch ($type) { | |
case self::TYPE_MO: | |
$getText = new Zend_Translate_Adapter_Gettext($file, $this->getLocale()); | |
$data = $getText->getMessages(); | |
break; | |
case self::TYPE_CSV: | |
case self::TYPE_DEFAULT: | |
default: | |
$parser = new Varien_File_Csv(); | |
$parser->setDelimiter(self::CSV_SEPARATOR); | |
$data = $parser->getDataPairs($file); | |
break; | |
} | |
} | |
return $data; | |
} | |
[...] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<config> | |
<frontend> | |
<translate> | |
<modules> | |
<{My_Module}> | |
<files> | |
<mo>{My_Module}.mo</mo> | |
</files> | |
</{My_Module}> | |
</modules> | |
</translate> | |
</frontend> | |
</config> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment