I've just run into an annoying problem which I haven't found any answers for (either in SO or the whole Internet).

I am trying to develop a multi-language site, and I have been following CakePHP's Internalization & Localization tutorial. I am now trying to place language folders inside the Views folder, so I could simply redirect the user to the view file according to their desired language. Problem is, the piece of code that was supposed to do that simply doesn't work in my application:

// App Controller Code.
public function beforeFilter() {
    $locale = Configure::read('Config.language');
    if ($locale && file_exists(VIEWS . $locale . DS . $this->viewPath)) {
        // e.g. use /app/View/fra/Pages/tos.ctp instead of /app/View/Pages/tos.ctp
        $this->viewPath = $locale . DS . $this->viewPath;
    }
}

The error I get states that there's no VIEWS constant, which is indeed true. But the tutorial says nothing about it so I am a bit lost here. I could actually remove the second condition from the if statement and the code will work, but I know that someday I may want to add view files which are the same for both languages (views with only images for example) and there should be no redirect to language folders in that case, which is exactly what this code would do.

I know that I might be missing something quite obvious since even the tutorial doesn`t mention nothing about it, but I would be so glad if anybody could share some help.

有帮助吗?

解决方案

I have found a dirty solution for this problem, and I'm still hopeful that better solutions will show up. Anyway, to the solution:

I've simply replaced the condition:

file_exists(VIEWS . $locale . DS . $this->viewPath))

for

file_exists(APP . DS . 'View' . DS . $locale . DS . $this->viewPath)

Now my app finds the path correctly.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top