symfony模板不同于语言

时间:2014-01-15 14:27:39

标签: php symfony twig

我正在开发一个项目,我想要添加语言 - 我不想在任何控制器中更改 - 为其他语言切换模板 - 找到了这个解决方案:

Symfony 2 load different template depending on user agent properties

我设置了它:

<?php
/**
 * Created by PhpStorm.
 * User: grek
 * Date: 15.01.14
 * Time: 15:41
 */

namespace ed\siteBundle\Service;

use Symfony\Bundle\FrameworkBundle\Templating\Loader\FilesystemLoader;
use Symfony\Component\Templating\Storage\FileStorage;

class templateLangLoader extends FilesystemLoader{

    protected $container;

    public function __construct($templatePathPatterns, $container)
    {
        parent::__construct($templatePathPatterns);
        $this->container = $container;
    }

    public function load(\Symfony\Component\Templating\TemplateReferenceInterface $template)
    {

        $translatedBundles = ['eddiscoveryBundle','edpartnerBundle','edregisterBundle','siteBundle','userBundle'];
        if(in_array($template->get('bundle'),$translatedBundles))
        {
            //$request = $this->container->get('request');

        }

        try {
            $file = $this->locator->locate($template);

        echo "<BR>$file";
           /*
            print_r([
                $template->getLogicalName()
                ,$template->getPath()
                , var_dump($template,1)
                , $file
            ]);
*/

        } catch (\InvalidArgumentException $e) {
            return false;
        }

        return new FileStorage($file);
    }



} 

我来到现场:

/home/grek/public_html/edpartner/src/ed/discoveryBundle/Resources/views/Default/pl_listItem.html.twig
/home/grek/public_html/edpartner/src/ed/discoveryBundle/Resources/views/Default/pl_header.html.twig
/home/grek/public_html/edpartner/src/ed/discoveryBundle/Resources/views/Default/pl_list.html.twig
/home/grek/public_html/edpartner/src/ed/discoveryBundle/Resources/views/Default/pl_fullPage.html.twig
/home/grek/public_html/edpartner/src/ed/discoveryBundle/Resources/views/Default/pl_listItemDemo.html.twig
/home/grek/public_html/edpartner/app/Resources/views/pl_ajax-layout.html.twig
/home/grek/public_html/edpartner/app/Resources/views/pl_clean.html.twig
/home/grek/public_html/edpartner/app/Resources/views/pl_base.html.twig 

不允许序列化'Closure' 500内部服务器错误 - 例外

非常奇怪 - 模板名称 - 我重命名所有模板添加pl_surfix但是在代码上我没有改变但是synfony自我查找模板?

我有代码: $这 - &GT;呈现( 'eddiscoveryBundle:默认:list.html.twig')

symfony看看 - pl_list.html.twig怎么追求呢?我想要它,但我想我必须改变 - 这是自我改变和错误'封闭'的序列化是不允许的

新: 我重命名模板 /home/grek/public_html/edpartner/src/ed/discoveryBundle/Resources/views/Default/pl_listItem.html.twig 至 /home/grek/public_html/edpartner/src/ed/discoveryBundle/Resources/views/Default/asdasdasdasdasasd_listItem.html.twig 我在echo“
$ file”上看到这个名字;没有任何改变?怎么可能?

那么如何纠正现在在代码上重命名呢?

更新2:

是更改加载程序:

  public function load(\Symfony\Component\Templating\TemplateReferenceInterface $template)
    {

        $translatedBundles = ['eddiscoveryBundle','edpartnerBundle','edregisterBundle','siteBundle','userBundle'];



        if(in_array($template->get('bundle'),$translatedBundles))
        {

            var_dump($template);
            $request = $this->container->get('request');

        }

        $file = $this->locator->locate($template);

        return new FileStorage($file);

    }

并将所有模板重命名为原始模板而不使用pl _

首先获得(网站工作):

object(Symfony\Bundle\FrameworkBundle\Templating\TemplateReference)[1268]
  protected 'parameters' => 
    array (size=5)
      'bundle' => string 'eddiscoveryBundle' (length=17)
      'controller' => string 'Default' (length=7)
      'name' => string 'fullPage' (length=8)
      'format' => string 'html' (length=4)
      'engine' => string 'twig' (length=4)

重命名模板添加pl_ - 无需更改代码:

和(网站不工作)得到:

    object(Symfony\Bundle\FrameworkBundle\Templating\TemplateReference)[335]
  protected 'parameters' => 
    array (size=5)
      'bundle' => string 'eddiscoveryBundle' (length=17)
      'controller' => string 'Default' (length=7)
      'name' => string 'pl_fullPage' (length=11)
      'format' => string 'html' (length=4)
      'engine' => string 'twig' (length=4)

和网站崩溃 - 不允许序列化'关闭' 500内部服务器错误 - 例外

所以如何将文件名重命名为pl_ - 它是自我重命名和崩溃:)

这个例子使用FilesystemLoader - 也许可以使用 - Twig_LoaderInterface我在doc中发现了一些想法,但是没有看到这个类或例子如何在symfony中覆盖它......

1 个答案:

答案 0 :(得分:0)

您无需更改任何内容。只要你做得对,你就可以为所有语言配备1个模板/控制器......

阅读文档以获取有关翻译here

的更多信息
相关问题