【问题标题】:symfony template different for languagesymfony 模板因语言而异
【发布时间】:2014-01-15 14:27:39
【问题描述】:

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

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 

“闭包”的序列化是不允许的 500 内部服务器错误 - 异常

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

我有代码: $this->render('eddiscoveryBundle:Default:list.html.twig')

symfony 看看-pl_list.html.twig 它是怎么查询的呢?我想要它,但我认为我必须改变 - 这是自我改变和错误 'Closure' 的序列化是不允许的

新: 我重命名模板从 /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)

和网站崩溃 - 'Closure' 的序列化是不允许的 500 内部服务器错误 - 异常

那么如何将文件名重命名为 pl_ - 它是自我重命名并崩溃 :)

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

【问题讨论】:

    标签: php symfony twig


    【解决方案1】:

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

    阅读文档以获取有关翻译的更多信息here

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多