0

I am using Kohana 3.3

Goal

I would like to have my application select the proper view file based on the language in the URL. So for example:

mydomain.com/es/foobar would load the Spanish view file in my Foobar controller. This is working for anything but the base URL. Right now if you go to mydomain.com/es/ or mydomain.com/en/ I am getting a 404 returned. I would like for it to route to the Index controller at /classes/Controller/Index.php I am not sure what I am missing here. Any pointers would be appreciated.

NOTE:

mydomain.com correctly gets directed to the english language page.

I can post some Controller code if necessary but I am fairly certain this is just a routing problem.

Current Routes

/*I don't think this one is even getting fired even though it's first */
Route::set('mydomain_home', '(<lang>/)',
array(
    'lang' => '(en|es)'
))
->filter(function($route, $params, $request)
{   
    $lang = is_empty($params['lang']) ? 'en' : $params['lang'];
    /*This debug statement is not printing*/
    echo Debug::vars('Language in route filter: '.$lang);
    $params['controller'] = $lang.'_Index';
    return $params; // Returning an array will replace the parameters
})
->defaults(array(
    'controller' => 'Index',
    'action' => 'index',
    'lang' => 'en',
));;

/*This one works for the non-base URL e.g. mydomain.com/es/page1 */
Route::set('mydomain_default', '(<lang>/)(<controller>(/<action>(/<subfolder>)))',
array(
    'lang' => '(en|es)',
))
->filter(function($route, $params, $request) {
        // Replacing the hyphens for underscores.
        $params['action'] = str_replace('-', '_', $params['action']);
        return $params; // Returning an array will replace the parameters.
})
->defaults(array(
    'controller' => 'Index',
    'action' => 'index',
    'lang' => 'en',
));
thatidiotguy
  • 8,701
  • 13
  • 60
  • 105
  • Can't you use kohanas build in I18n module? Using different view files for different languages would be impossible to maintain. http://kohanaframework.org/3.3/guide-api/I18n – AmazingDreams Jul 23 '13 at 09:23
  • Are you using different controllers for different languages??? If I were you I'd seriously consider something else. There should not even be any output in the controllers, so those should NEVER have to be localized. – AmazingDreams Jul 23 '13 at 09:36
  • @AmazingDreams Using separate view files is no different than using the I18N. Whenever anything changes I have to go hunt down that particular bunch of text and change it. I also plan on the HTML structure diverging between language-specific versions of the website, so it makes sense. Do you have anything to add about my actual problem? – thatidiotguy Jul 23 '13 at 13:41

1 Answers1

1

I replicated your problem and used your routes. After modifying them I came to the conclusion two routes would be easier. One for the normal URLs and one for the language routes.

Below are the routes I made:

Route::set('language_default', '(<lang>(/<controller>(/<action>(/<subfolder>))))',
array(
    'lang' => '(en|es)',
))
->filter(function($route, $params, $request) {
    // Replacing the hyphens for underscores.
    $params['action'] = str_replace('-', '_', $params['action']);
    return $params; // Returning an array will replace the parameters.
})
->defaults(array(
    'controller' => 'Index',
    'action' => 'index',
    'lang' => 'en',
));


Route::set('default', '(<controller>(/<action>(/<subfolder>)))')
->filter(function($route, $params, $request) {
    // Replacing the hyphens for underscores.
    $params['action'] = str_replace('-', '_', $params['action']);
    return $params; // Returning an array will replace the parameters.
})
->defaults(array(
    'controller' => 'Index',
    'action' => 'index',
    'lang' => 'en',
));
Manuras
  • 506
  • 2
  • 10