Skip to content

Instantly share code, notes, and snippets.

@paales
Last active August 29, 2015 14:09
Show Gist options
  • Save paales/560823837cefac9742e5 to your computer and use it in GitHub Desktop.
Save paales/560823837cefac9742e5 to your computer and use it in GitHub Desktop.
Add translations automatically to your module's CSV.
<?php
class Ho_SimpleBundle_Helper_Data extends Mage_Core_Helper_Abstract
{
public function __()
{
$args = func_get_args();
$string = array_shift($args);
$expr = new Mage_Core_Model_Translate_Expr($string, $this->_getModuleName());
array_unshift($args, $expr);
$result = Mage::app()->getTranslator()->translate($args);
if ($result == $string) {
$this->_addTranslateStringToLog($result);
}
return $result;
}
/**
* Write unfound strings to the modules en_US/filename.csv
* @param string $text
* @internal param string $locale
*/
protected function _addTranslateStringToLog($text)
{
$translatecsv = Mage::getBaseDir('locale') . DS . 'en_US' . DS . $this->_getTranslateFilename();
if (!file_exists($translatecsv)) { // new translation file
mkdir(dirname($translatecsv), 0777, true);
if ($fp = fopen($translatecsv, "wb")) {
fwrite($fp, "\"{$text}\", \"\"\n");
fclose($fp);
}
} else { // file exists, only write string if not exists
$execgrep = exec('grep -- \''.$text.'\' '.$translatecsv);
if (empty($execgrep)) {
if ($fp = fopen($translatecsv, "a")) {
fwrite($fp, "\"{$text}\", \"{$text}\"\n");
fclose($fp);
}
}
}
}
protected $_translateFilename;
protected function _getTranslateFilename()
{
$nodeName = strtolower(str_replace('_Helper_Data', '', __CLASS__));
if (is_null($this->_translateFilename)) {
foreach (Mage::app()->getTranslator()->getModulesConfig() as $moduleName=>$info) {
/** @var Mage_Core_Model_Config_Element $info */
if ($moduleName == $nodeName) {
$this->_translateFilename = (string) $info->files->default;
break;
}
}
}
return $this->_translateFilename;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment