Code snippets for symfony 1.x

Navigation

Snippets by user Tamcy

Culture-aware templates

The following view class tries to load the template under directory name of the current culture, then fall back to the default template if such template cannot be found.

So if you access to "index" action of module "foo" and if current user culture is "zh_HK", the class will try to load: "module/foo/templates/zh_HK/indexSuccess.php"

If no such file exist, it will fall back to the default behaviour by loading "module/foo/templates/indexSuccess.php"

class sfCulturePHPView extends sfPHPView
{
  protected
    $cultureDirectory = '';
 
  public function initialize($context, $moduleName, $actionName, $viewName)
  {
    parent::initialize($context, $moduleName, $actionName, $viewName);
  }
 
  public function getDirectory()
  {
    if ($this->cultureDirectory)
      return $this->directory.'/'.$this->cultureDirectory;
    return $this->directory;
  }
 
  protected function preRenderCheck()
  {
    parent::preRenderCheck();
 
    // determine whether the culture-aware template is usable
    $currentCulture = $this->context->getUser()->getCulture();
 
    $template = $this->directory.'/'.$currentCulture.'/'.$this->template;
 
    if (is_readable($template))
      $this->cultureDirectory = $currentCulture;
    else
      $this->cultureDirectory = '';
  }
}
 
by Tamcy on 2008-05-27, tagged culture  view 
(1 comment)