Mage_Core_Model_Translate Class Reference

List of all members.

Public Member Functions

 __construct ()
 init ($area, $forceReload=false)
 getModulesConfig ()
 setConfig ($config)
 getConfig ($key)
 getData ()
 getLocale ()
 setLocale ($locale)
 getResource ()
 getTranslate ()
 translate ($args)
 setTranslateInline ($flag=null)
 getTranslateInline ()
 getTemplateFile ($file, $type, $localeCode=null)
 getCacheId ()

Public Attributes

const CSV_SEPARATOR = ','
const SCOPE_SEPARATOR = '::'
const CACHE_TAG = 'translate'
const CONFIG_KEY_AREA = 'area'
const CONFIG_KEY_LOCALE = 'locale'
const CONFIG_KEY_STORE = 'store'
const CONFIG_KEY_DESIGN_PACKAGE = 'package'
const CONFIG_KEY_DESIGN_THEME = 'theme'

Protected Member Functions

 _loadModuleTranslation ($moduleName, $files, $forceReload=false)
 _addData ($data, $scope, $forceReload=false)
 _prepareDataString ($string)
 _loadThemeTranslation ($forceReload=false)
 _loadDbTranslation ($forceReload=false)
 _getModuleFilePath ($module, $fileName)
 _getFileData ($file)
 _loadCache ()
 _saveCache ()
 _canUseCache ()
 _getTranslatedString ($text, $code)

Protected Attributes

 $_locale
 $_translate
 $_config
 $_useCache = true
 $_cacheId
 $_data = array()
 $_dataScope
 $_translateInline
 $_canUseInline = true


Detailed Description

Translate model

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 32 of file Translate.php.


Constructor & Destructor Documentation

__construct (  ) 

Definition at line 102 of file Translate.php.

00103     {
00104     }


Member Function Documentation

_addData ( data,
scope,
forceReload = false 
) [protected]

Adding translation data

Parameters:
array $data
string $scope
Returns:
Mage_Core_Model_Translate

Checking previos value

Not allow use translation not related to module

Definition at line 225 of file Translate.php.

00226     {
00227         foreach ($data as $key => $value) {
00228             if ($key === $value) {
00229                 continue;
00230             }
00231             $key    = $this->_prepareDataString($key);
00232             $value  = $this->_prepareDataString($value);
00233             if ($scope && isset($this->_dataScope[$key]) && !$forceReload ) {
00234                 /**
00235                  * Checking previos value
00236                  */
00237                 $scopeKey = $this->_dataScope[$key] . self::SCOPE_SEPARATOR . $key;
00238                 if (!isset($this->_data[$scopeKey])) {
00239                     if (isset($this->_data[$key])) {
00240                         $this->_data[$scopeKey] = $this->_data[$key];
00241                         /**
00242                          * Not allow use translation not related to module
00243                          */
00244                         if (Mage::getIsDeveloperMode()) {
00245                             unset($this->_data[$key]);
00246                         }
00247                     }
00248                 }
00249                 $scopeKey = $scope . self::SCOPE_SEPARATOR . $key;
00250                 $this->_data[$scopeKey] = $value;
00251             }
00252             else {
00253                 $this->_data[$key]     = $value;
00254                 $this->_dataScope[$key]= $scope;
00255             }
00256         }
00257         return $this;
00258     }

_canUseCache (  )  [protected]

Check cache usage availability

Returns:
bool

Definition at line 550 of file Translate.php.

00551     {
00552         return Mage::app()->useCache('translate');
00553     }

_getFileData ( file  )  [protected]

Retrieve data from file

Parameters:
string $file
Returns:
array

Definition at line 309 of file Translate.php.

00310     {
00311         $data = array();
00312         if (file_exists($file)) {
00313             $parser = new Varien_File_Csv();
00314             $parser->setDelimiter(self::CSV_SEPARATOR);
00315             $data = $parser->getDataPairs($file);
00316         }
00317         return $data;
00318     }

_getModuleFilePath ( module,
fileName 
) [protected]

Retrieve translation file for module

Parameters:
string $module
Returns:
string

Definition at line 295 of file Translate.php.

00296     {
00297         //$file = Mage::getConfig()->getModuleDir('locale', $module);
00298         $file = Mage::getBaseDir('locale');
00299         $file.= DS.$this->getLocale().DS.$fileName;
00300         return $file;
00301     }

_getTranslatedString ( text,
code 
) [protected]

Return translated string from text.

Parameters:
string $text
string $code
Returns:
string

Definition at line 562 of file Translate.php.

00563     {
00564         $translated = '';
00565         if (array_key_exists($code, $this->getData())) {
00566             $translated = $this->_data[$code];
00567         }
00568         elseif (array_key_exists($text, $this->getData())) {
00569             $translated = $this->_data[$text];
00570         }
00571         else {
00572             $translated = $text;
00573         }
00574         return $translated;
00575     }

_loadCache (  )  [protected]

Loading data cache

Parameters:
string $area
Returns:
array | false

Definition at line 520 of file Translate.php.

00521     {
00522         if (!$this->_canUseCache()) {
00523             return false;
00524         }
00525         $data = Mage::app()->loadCache($this->getCacheId());
00526         $data = unserialize($data);
00527         return $data;
00528     }

_loadDbTranslation ( forceReload = false  )  [protected]

Loading current store translation from DB

Returns:
Mage_Core_Model_Translate

Definition at line 282 of file Translate.php.

00283     {
00284         $arr = $this->getResource()->getTranslationArray();
00285         $this->_addData($arr, $this->getConfig(self::CONFIG_KEY_STORE), $forceReload);
00286         return $this;
00287     }

_loadModuleTranslation ( moduleName,
files,
forceReload = false 
) [protected]

Loading data from module translation files

Parameters:
string $moduleName
string $files
Returns:
Mage_Core_Model_Translate

Definition at line 209 of file Translate.php.

00210     {
00211         foreach ($files as $file) {
00212             $file = $this->_getModuleFilePath($moduleName, $file);
00213             $this->_addData($this->_getFileData($file), $moduleName, $forceReload);
00214         }
00215         return $this;
00216     }

_loadThemeTranslation ( forceReload = false  )  [protected]

Loading current theme translation

Returns:
Mage_Core_Model_Translate

Definition at line 270 of file Translate.php.

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

_prepareDataString ( string  )  [protected]

Definition at line 260 of file Translate.php.

00261     {
00262         return str_replace('""', '"', $string);
00263     }

_saveCache (  )  [protected]

Saving data cache

Parameters:
string $area
Returns:
Mage_Core_Model_Translate

Definition at line 536 of file Translate.php.

00537     {
00538         if (!$this->_canUseCache()) {
00539             return $this;
00540         }
00541         Mage::app()->saveCache(serialize($this->getData()), $this->getCacheId(), array(self::CACHE_TAG), null);
00542         return $this;
00543     }

getCacheId (  ) 

Retrieve cache identifier

Returns:
string

Definition at line 491 of file Translate.php.

00492     {
00493         if (is_null($this->_cacheId)) {
00494             $this->_cacheId = 'translate';
00495             if (isset($this->_config[self::CONFIG_KEY_LOCALE])) {
00496                 $this->_cacheId.= '_'.$this->_config[self::CONFIG_KEY_LOCALE];
00497             }
00498             if (isset($this->_config[self::CONFIG_KEY_AREA])) {
00499                 $this->_cacheId.= '_'.$this->_config[self::CONFIG_KEY_AREA];
00500             }
00501             if (isset($this->_config[self::CONFIG_KEY_STORE])) {
00502                 $this->_cacheId.= '_'.$this->_config[self::CONFIG_KEY_STORE];
00503             }
00504             if (isset($this->_config[self::CONFIG_KEY_DESIGN_PACKAGE])) {
00505                 $this->_cacheId.= '_'.$this->_config[self::CONFIG_KEY_DESIGN_PACKAGE];
00506             }
00507             if (isset($this->_config[self::CONFIG_KEY_DESIGN_THEME])) {
00508                 $this->_cacheId.= '_'.$this->_config[self::CONFIG_KEY_DESIGN_THEME];
00509             }
00510         }
00511         return $this->_cacheId;
00512     }

getConfig ( key  ) 

Retrieve config value by key

Parameters:
string $key
Returns:
mixed

Definition at line 194 of file Translate.php.

00195     {
00196         if (isset($this->_config[$key])) {
00197             return $this->_config[$key];
00198         }
00199         return null;
00200     }

getData (  ) 

Retrieve translation data

Returns:
array

Definition at line 325 of file Translate.php.

00326     {
00327         if (is_null($this->_data)) {
00328             return array();
00329             //Mage::throwException('Translation data is not initialized. Please contact developers.');
00330         }
00331         return $this->_data;
00332     }

getLocale (  ) 

Retrieve locale

Returns:
string

Definition at line 339 of file Translate.php.

00340     {
00341         if (is_null($this->_locale)) {
00342             $this->_locale = Mage::app()->getLocale()->getLocaleCode();
00343         }
00344         return $this->_locale;
00345     }

getModulesConfig (  ) 

Retrieve modules configuration by translation

Returns:
Mage_Core_Model_Config_Element

Definition at line 151 of file Translate.php.

00152     {
00153         if (!Mage::getConfig()->getNode($this->getConfig(self::CONFIG_KEY_AREA).'/translate/modules')) {
00154             return array();
00155         }
00156 
00157         $config = Mage::getConfig()->getNode($this->getConfig(self::CONFIG_KEY_AREA).'/translate/modules')->children();
00158         if (!$config) {
00159             return array();
00160         }
00161         return $config;
00162     }

getResource (  ) 

Retrieve DB resource model

Returns:
unknown

Definition at line 358 of file Translate.php.

00359     {
00360         return Mage::getResourceSingleton('core/translate');
00361     }

getTemplateFile ( file,
type,
localeCode = null 
)

Retrive translated template file

Parameters:
string $file
string $type
string $localeCode
Returns:
string

Definition at line 459 of file Translate.php.

00460     {
00461         if (is_null($localeCode) || preg_match('/[^a-zA-Z_]/', $localeCode)) {
00462             $localeCode = $this->getLocale();
00463         }
00464 
00465         $filePath = Mage::getBaseDir('locale')  . DS
00466                   . $localeCode . DS . 'template' . DS . $type . DS . $file;
00467 
00468         if (!file_exists($filePath)) { // If no template specified for this locale, use store default
00469             $filePath = Mage::getBaseDir('locale') . DS
00470                       . Mage::app()->getLocale()->getDefaultLocale()
00471                       . DS . 'template' . DS . $type . DS . $file;
00472         }
00473 
00474         if (!file_exists($filePath)) {  // If no template specified as  store default locale, use en_US
00475             $filePath = Mage::getBaseDir('locale') . DS
00476                       . Mage_Core_Model_Locale::DEFAULT_LOCALE
00477                       . DS . 'template' . DS . $type . DS . $file;
00478         }
00479 
00480         $ioAdapter = new Varien_Io_File();
00481         $ioAdapter->open(array('path' => Mage::getBaseDir('locale')));
00482 
00483         return (string) $ioAdapter->read($filePath);
00484     }

getTranslate (  ) 

Retrieve translation object

Returns:
Zend_Translate_Adapter

Definition at line 368 of file Translate.php.

00369     {
00370         if (is_null($this->_translate)) {
00371             $this->_translate = new Zend_Translate('array', $this->getData(), $this->getLocale());
00372         }
00373         return $this->_translate;
00374     }

getTranslateInline (  ) 

Retrieve active translate mode

Returns:
bool

Definition at line 446 of file Translate.php.

00447     {
00448         return $this->_canUseInline;
00449     }

init ( area,
forceReload = false 
)

Initialization translation data

Parameters:
string $area
Returns:
Mage_Core_Model_Translate

Definition at line 112 of file Translate.php.

00113     {
00114         $this->setConfig(array(self::CONFIG_KEY_AREA=>$area));
00115 
00116         $this->_translateInline = Mage::getSingleton('core/translate_inline')
00117             ->isAllowed($area=='adminhtml' ? 'admin' : null);
00118 
00119         if (!$forceReload) {
00120             if ($this->_canUseCache()) {
00121                 $this->_data = $this->_loadCache();
00122                 if ($this->_data !== false) {
00123                     return $this;
00124                 }
00125             }
00126             Mage::app()->removeCache($this->getCacheId());
00127         }
00128 
00129         $this->_data = array();
00130 
00131         foreach ($this->getModulesConfig() as $moduleName=>$info) {
00132             $info = $info->asArray();
00133             $this->_loadModuleTranslation($moduleName, $info['files'], $forceReload);
00134         }
00135 
00136         $this->_loadThemeTranslation($forceReload);
00137         $this->_loadDbTranslation($forceReload);
00138 
00139         if (!$forceReload && $this->_canUseCache()) {
00140             $this->_saveCache();
00141         }
00142 
00143         return $this;
00144     }

setConfig ( config  ) 

Initialize configuration

Parameters:
array $config
Returns:
Mage_Core_Model_Translate

Definition at line 170 of file Translate.php.

00171     {
00172         $this->_config = $config;
00173         if (!isset($this->_config[self::CONFIG_KEY_LOCALE])) {
00174             $this->_config[self::CONFIG_KEY_LOCALE] = $this->getLocale();
00175         }
00176         if (!isset($this->_config[self::CONFIG_KEY_STORE])) {
00177             $this->_config[self::CONFIG_KEY_STORE] = Mage::app()->getStore()->getId();
00178         }
00179         if (!isset($this->_config[self::CONFIG_KEY_DESIGN_PACKAGE])) {
00180             $this->_config[self::CONFIG_KEY_DESIGN_PACKAGE] = Mage::getDesign()->getPackageName();
00181         }
00182         if (!isset($this->_config[self::CONFIG_KEY_DESIGN_THEME])) {
00183             $this->_config[self::CONFIG_KEY_DESIGN_THEME] = Mage::getDesign()->getTheme('locale');
00184         }
00185         return $this;
00186     }

setLocale ( locale  ) 

Definition at line 347 of file Translate.php.

00348     {
00349         $this->_locale = $locale;
00350         return $this;
00351     }

setTranslateInline ( flag = null  ) 

Set Translate inline mode

Parameters:
bool $flag
Returns:
Mage_Core_Model_Translate

Definition at line 435 of file Translate.php.

00436     {
00437         $this->_canUseInline = (bool) $flag;
00438         return $this;
00439     }

translate ( args  ) 

Translate

Parameters:
array $args
Returns:
string

Definition at line 382 of file Translate.php.

00383     {
00384         $text = array_shift($args);
00385 
00386         if (is_string($text) && ''==$text
00387             || is_null($text)
00388             || is_bool($text) && false===$text
00389             || is_object($text) && ''==$text->getText()) {
00390             return '';
00391         }
00392         if ($text instanceof Mage_Core_Model_Translate_Expr) {
00393             $code = $text->getCode(self::SCOPE_SEPARATOR);
00394             $module = $text->getModule();
00395             $text = $text->getText();
00396             $translated = $this->_getTranslatedString($text, $code);
00397         }
00398         else {
00399             if (!empty($_REQUEST['theme'])) {
00400                 $module = 'frontend/default/'.$_REQUEST['theme'];
00401             } else {
00402                 $module = 'frontend/default/default';
00403             }
00404             $code = $module.self::SCOPE_SEPARATOR.$text;
00405             $translated = $this->_getTranslatedString($text, $code);
00406         }
00407 
00408         //array_unshift($args, $translated);
00409         //$result = @call_user_func_array('sprintf', $args);
00410 
00411         $result = @vsprintf($translated, $args);
00412         if ($result === false) {
00413             $result = $translated;
00414         }
00415 
00416         if ($result === false){
00417             $result = $translated;
00418         }
00419 
00420         if ($this->_translateInline && $this->getTranslateInline()) {
00421             if (strpos($result, '{{{')===false || strpos($result, '}}}')===false || strpos($result, '}}{{')===false) {
00422                 $result = '{{{'.$result.'}}{{'.$translated.'}}{{'.$text.'}}{{'.$module.'}}}';
00423             }
00424         }
00425 
00426         return $result;
00427     }


Member Data Documentation

$_cacheId [protected]

Definition at line 72 of file Translate.php.

$_canUseInline = true [protected]

Definition at line 100 of file Translate.php.

$_config [protected]

Definition at line 63 of file Translate.php.

$_data = array() [protected]

Definition at line 79 of file Translate.php.

$_dataScope [protected]

Definition at line 86 of file Translate.php.

$_locale [protected]

Definition at line 49 of file Translate.php.

$_translate [protected]

Definition at line 56 of file Translate.php.

$_translateInline [protected]

Definition at line 93 of file Translate.php.

$_useCache = true [protected]

Definition at line 65 of file Translate.php.

const CACHE_TAG = 'translate'

Definition at line 36 of file Translate.php.

const CONFIG_KEY_AREA = 'area'

Definition at line 38 of file Translate.php.

const CONFIG_KEY_DESIGN_PACKAGE = 'package'

Definition at line 41 of file Translate.php.

const CONFIG_KEY_DESIGN_THEME = 'theme'

Definition at line 42 of file Translate.php.

const CONFIG_KEY_LOCALE = 'locale'

Definition at line 39 of file Translate.php.

const CONFIG_KEY_STORE = 'store'

Definition at line 40 of file Translate.php.

const CSV_SEPARATOR = ','

Definition at line 34 of file Translate.php.

const SCOPE_SEPARATOR = '::'

Definition at line 35 of file Translate.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:01 2009 for Magento by  doxygen 1.5.8