Final version 2.0

This commit is contained in:
Sebastien Guibert 2011-06-26 19:09:00 +02:00
parent 0d2d75242f
commit c091dacfc2
4 changed files with 60 additions and 29 deletions

View File

@ -99,39 +99,54 @@ class Multilang_Core {
}
// Create uris for each language
foreach($languages as $code => &$language)
foreach($languages as $lang => &$language)
{
// If it's the current language
if($code === Request::$lang)
if($lang === Request::$lang)
{
// We only display it when required
if($current)
{
$selectors[$code] = '<span class="multilang-selected multilang-'.$code.'">'.$languages[$code]['label'].'</span>';
$selectors[$lang] = '<span class="multilang-selected multilang-'.$lang.'">'.$languages[$lang]['label'].'</span>';
}
}
else
{
$route = NULL;
// If it's the default route, it's unique and special (like you <3)
if($current_route === 'default')
{
// We juste need to change the language parameter
$route = Request::initial()->route();
$params = array(
'lang' => $code,
);
$route = Request::initial()->route();
if(!Kohana::config('multilang.hide_default') || Kohana::config('multilang.default') !== $lang)
{
$params['lang'] = $lang;
}
}
else
{
$route = Route::get($name, $code);
{
if(Arr::get(Route::all(), $lang.'.'.$name))
{
$route = Route::get($name, $lang);
}
}
$selectors[$code] = HTML::anchor($route->uri($params), $languages[$code]['label'], array('class' => 'multilang-selectable multilang-'.$code, 'title' => $languages[$code]['label']));
if($route !== NULL)
{
$selectors[$lang] = HTML::anchor($route->uri($params), $languages[$lang]['label'], array('class' => 'multilang-selectable multilang-'.$lang, 'title' => $languages[$lang]['label']));
}
}
}
return View::factory('multilang/selector')
// We display the menu only if we can select another language for this page
if(count($selectors) > 1)
{
return View::factory('multilang/selector')
->bind('selectors', $selectors);
}
return '';
}
}

View File

@ -31,7 +31,7 @@ class Multilang_Request extends Kohana_Request {
if(!Kohana::$is_cli)
{
// If we don't hide the default language, we must look for a language code for the root uri
if(!Kohana::config('multilang.hide_default') && $uri === TRUE && Request::detect_uri() === '')
if(Kohana::config('multilang.auto_detect') && $uri === TRUE && Request::detect_uri() === '')
{
$lang = Multilang::find_user_language();
@ -40,14 +40,23 @@ class Multilang_Request extends Kohana_Request {
// Redirect to the root URI, but with language prepended
header($protocol.' 302 Found');
header('Location: '.URL::base(TRUE, TRUE).$lang.'/');
header('Location: '.URL::base(TRUE, TRUE).$lang.'/');
exit;
}
}
$request = parent::factory($uri, $cache, $injected_routes);
Request::$lang = $request->param('lang');
// If the default language is hidden, we manually set it
if(Kohana::config('multilang.hide_default') && $request->param('lang') === NULL)
{
Request::$lang = Kohana::config('multilang.default');
}
else
{
Request::$lang = $request->param('lang');
}
Multilang::init();
return $request;
}

View File

@ -18,6 +18,20 @@ class Multilang_Route extends Kohana_Route {
*/
static public function set($name, $uri_callback = NULL, $regex = NULL, $lang = NULL)
{
if(!Kohana::config('multilang.hide_default') || Kohana::config('multilang.default') != $lang)
{
if($lang !== NULL)
{
$uri_callback = '<lang>/'.$uri_callback;
$regex['lang'] = $lang;
}
}
if($lang !== NULL)
{
$name = $lang.'.'.$name;
}
return Route::$_routes[$name] = new Route($uri_callback, $regex, $lang);
}

View File

@ -33,33 +33,26 @@ class Multilang_Routes {
$default_lang = Kohana::config('multilang.default');
$languages = Kohana::config('multilang.languages');
// We first look for the default language uri which is obviously compulsory
$default_uri = Arr::get($uris, $default_lang);
if($default_uri === NULL)
{
throw new Kohana_Exception('The default route uri is required for the language :lang', array(':lang' => $default_lang));
throw new Kohana_Exception('The default language route uri is required for the route: :route', array(':route' => $name));
}
else
{
// If we dont hide the default language in the uri
if(!Kohana::config('multilang.hide_default'))
{
$default_uri = '<lang>/'.$default_uri;
$regex['lang'] = $default_lang;
}
$routes->_routes[$default_lang.'.'.$name] = Route::set($default_lang.'.'.$name, $default_uri, $regex, $default_lang);
{
$routes->_routes[$default_lang.'.'.$name] = Route::set($name, $default_uri, $regex, $default_lang);
}
unset($languages[$default_lang]);
// Then we add the routes for all the other languages
foreach($languages as $lang => $settings)
{
$uri = '<lang>/'.(Arr::get($uris, $lang) ? $uris[$lang] : $uris[$default_lang]);
$regex['lang'] = $lang;
$uri = (Arr::get($uris, $lang) ? $uris[$lang] : $uris[$default_lang]);
// For the uri, we use the one given or the default one
$routes->_routes[$lang.'.'.$name] = Route::set($lang.'.'.$name, $uri, $regex, $lang);
$routes->_routes[$lang.'.'.$name] = Route::set($name, $uri, $regex, $lang);
}
return $routes;
}