如何翻译控制器文件中的文本Prestashop

问题描述:

我重写了一个控制器文件(CartController)...并且我获得了产品条件(新)并且我将成为法语站点的“Nouveau”。我使用下面的代码,但它不起作用。我该如何解决这个问题?如何翻译控制器文件中的文本Prestashop

在/override/controllers/front/CartController.php

[...] 
$list_product = $cart_current->getProducts(); 
foreach ($list_product as $index => $product){ 
    $product_current = new ProductCore($product['id_product'],true); 
    $result['label'] = $this->l($product_current->condition); /* Translation? */ 
} 
[...] 

首先,我会建议,以解决您这样的代码:

[...] 
$list_product = $cart_current->getProducts(); 
foreach ($list_product as $index => $product){ 
    $product_current = new Product($product['id_product'],true); // Product not ProductCore 
    $result['label'] = $product_current->condition; // Translation not here in the 'controller' but we make in the 'view' 
} 
[...] 

然后在 '视图'(购物车.tpl/product.tpl):

[...] 
/* 
You have to comment this translations to avoid displaying. This is a workaround used also for the months. 
{l s='New'} // {l s='New' mod='mymodule'} if you are in your module tpl 
{l s='Used'} // Like above 
*/ 
{l s='%s' sprintf=[$result['label']]} 
[...] 
+0

在控制器文件中有没有其他方法可以做到这一点? –

+0

你必须使用'Translate'类,但我不建议这么做......更正确的是,将'逻辑'与'视图'分开......因为你有一个'变量'字符串要翻译,所以你必须首先翻译所有可能的值。 – sarcom

+0

非常感谢! –

It's更好的做法override类/ Cart.php,功能的getProducts 和添加查询

, p.`condition` 

你可以在任何你的模块翻译

的添加翻译文本为“新建”后尝试使用下面的翻译:: getModuleTranslation()

代替

$ this-> l()