【问题标题】:Translate url cakephp翻译网址 cakephp
【发布时间】:2013-03-17 14:32:55
【问题描述】:

我正在 CakePHP 中创建一个页面,我正在尝试翻译 url。我想要的是翻译控制器名称所以说我有这个网址domain/da/product 然后它应该把它翻译成丹麦语所以它变成了domain/da/produkt

我在 AppHelper 中编写了自己的 url 方法,但在访问模型进行翻译时遇到问题。当我从助手搜索访问模型时,人们反对这样做。

那么什么是正确的方法来做到这一点。当用户使用该链接时,我需要进行查找并将其翻译回domain/da/product

这是 Apphelper 代码

App::uses('Helper', 'View');
App::import("Model", "ControllerTranslation");

class AppHelper extends Helper {

public function url($url = null, $full = false) {

      $Model = new ControllerTranslation();           
      $lang = Configure::read('Config.language');
      $controller = $Model->find("first",array("conditions"=> array("ControllerTranslation.translation = "=> $url['controller'],"ControllerTranslation.language" => $lang)));
      if (count($controller))
      {
            $url['controller'] = $controller["ControllerTranslation"]["translation"];
      }        
    return parent::url($url, $full);
}   

【问题讨论】:

  • 您好,我已经用代码更新了问题,目前的代码工作正常。我刚刚读到从帮助类调用模型是不好的 MVC 风格。
  • 我必须在路由器中再次创建反向查找,使用 __() 方法可以吗?
  • 是的,问题出在我需要非翻译值的路由器上。所以在我的例子中,它应该将 produkt 翻译回 product,所以它会自动找到 productController
  • 我认为您需要退后一步,看看您正在尝试解决的问题,而不是查看您选择的解决方案的缺陷

标签: cakephp localization internationalization


【解决方案1】:

如果您想要具有 translation = $url['controller'] 和 language = $lang 的 url,那么您应该使用语法:

$controller = $Model->find("first",
  array("conditions"=>   
    array(
           "ControllerTranslation.translation" => $url['controller'],                 
           "ControllerTranslation.language" => $lang
         )
       )
  );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 2012-10-10
    • 1970-01-01
    • 2016-03-09
    • 2013-07-09
    • 2016-05-10
    • 2019-02-01
    相关资源
    最近更新 更多