【问题标题】:How to create a page in a prestashop module such that the output is not wrapped in the site's template html AND using translation?如何在 prestashop 模块中创建页面,以便输出不包含在站点的模板 html 中并使用翻译?
【发布时间】:2015-07-11 08:29:00
【问题描述】:

我知道该怎么做:

1) 创建一个允许翻译的模块控制器。

我可以在控制器本身或模板中声明要翻译的文本:

/modules/mymodule/controllers/front/list.php

class myModuleListModuleFrontController extends ModuleFrontController
{
    public function initContent()
    {
        parent::initContent();
        $this->l('Some text to translater');
        $this->setTemplate('list.tpl');
    }
}

/modules/mymodule/views/templates/front/list.tpl

{l s='Some other text' mod='mymodule'}

2) 我知道要创建一些未嵌入 html 的输出,例如一些 json 对象:

/modules/mymodule/json.php

include( '../../config/config.inc.php' ); 
echo json_encode(array('key' => 'Some text'));

我需要什么:

我需要能够翻译一些文本并将输出发送到浏览器而不需要周围的 html。我必须能够做到其中之一:

  • 使用独立文件并能够声明要翻译的文本,类似于此(不起作用):

    include( '../../config/config.inc.php' ); 
    echo json_encode(array('key' => l('Some text')));
    
  • 使用模块控制器并强制原始输出,类似于此(也不起作用):

    class myModuleListModuleFrontController extends ModuleFrontController
    {
        public function initContent()
        {
            parent::initContent();
            $this->noHtml = true;
            echo json_encode(array('key' => l('Some text')));
        }
    }
    

【问题讨论】:

  • 你想通过 Ajax 从你的 FrontController 获取一些 HTML 吗?
  • @gskema 与此类似。如果您给出通过 Ajax 获得 html 贿赂的答案,它很可能会回答我的问题。不过,我处于一个非常特殊的情况:我想代理下载链接以检查用户是否已登录并具有权限,然后显示本地化错误消息、发送文件或重定向到登录。

标签: module controller prestashop


【解决方案1】:

如果要在FrontController中翻译文本,只能通过两种方式进行:

翻译模板内的文本

// Inside module front controller

$template = '.../template.tpl'
$this->context->smarty->fetch($template)

// Inside template.tpl

{l s='Translateable text' mod='mymodule'}

或者使用已经在主模块文件中翻译的字符串

 // Inside module front controller
 $this->module->l('My string');

 // But it has to already exist inside mymodule.php
 $this->l('My string'); // You don't have to use it, it just has to exist to get scanned by RegEx.

如果你想在你的模块前端控制器中返回一些东西给 Ajax 请求

 public function init() {

 parent::init(); // If you need to

 // Some code here

 if (Tools::getValue('ajax'))
 {
     header('Content-Type: text/html');
     die($this->context->smarty->fetch($template));

     // Or
     header('Content-Type: application/json');
     die(Tools::jsonEncode($response_array));
 }

还有一个函数叫做

FrontControllerCore::getLayout @ Line 1209

您可以使用它来覆盖整个页面模板,但是,它应该用于为产品和其他页面创建独特的显示(如全屏产品演示等)

如果您想自己输出文件而不向用户提供完整的文件路径:

 public function init() {

    parent::init(); // If you need to

    if (ob_get_level() && ob_get_length() > 0)
        ob_end_clean();

    // Set download headers
    header('Content-Transfer-Encoding: binary');
    header('Content-Type: '.$mime_type);
    header('Content-Length: '.filesize($file));
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    // Prevents max execution timeout, when reading large files
    set_time_limit(0);
    $fp = fopen($file, 'rb');
    while (!feof($fp))
        echo fgets($fp, 16384);

   exit;

除此之外,我不知道构建您的应用可能还需要什么。始终将令牌发送到您的控制器以确保安全!

【讨论】:

  • 非常感谢。这提供了比我预期更多的细节,以及有用的背景信息。
猜你喜欢
  • 2023-04-05
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 2012-11-03
相关资源
最近更新 更多