1
0
Fork 0
mirror of https://github.com/Oreolek/kohana-multilang.git synced 2024-04-28 07:09:26 +03:00
kohana-multilang/classes/multilang/core.php

159 lines
3.7 KiB
PHP
Raw Normal View History

2011-03-06 20:52:31 +02:00
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Multilang core class
* From the module https://github.com/GeertDD/kohana-lang
*/
class Multilang_Core {
static public $lang = '';
/**
* Looks for the user language.
2011-03-06 20:52:31 +02:00
* A language cookie and HTTP Accept-Language headers are taken into account.
*
* If the auto detection is disabled, we return the default one
2011-03-06 20:52:31 +02:00
*
* @return string language key, e.g. "en", "fr", "nl", "en_US", "en-us", etc.
2011-03-06 20:52:31 +02:00
*/
static public function find_user_language()
{
$config = Kohana::$config->load('multilang');
if($config->auto_detect)
2011-03-06 20:52:31 +02:00
{
// Get the list of supported languages
$languages = $config->languages;
$cookie = $config->cookie;
// Look for language cookie first
if($lang = Cookie::get($cookie))
2011-03-06 20:52:31 +02:00
{
// Valid language found in cookie
if(isset($languages[$lang]))
{
return $lang;
}
2011-03-06 20:52:31 +02:00
// Delete cookie with unset language
Cookie::delete($cookie);
}
2011-03-06 20:52:31 +02:00
// Parse HTTP Accept-Language headers
foreach(Request::accept_lang() as $lang => $quality)
2011-03-06 20:52:31 +02:00
{
// Return the first language found (the language with the highest quality)
if(isset($languages[$lang]))
{
return $lang;
}
2011-03-06 20:52:31 +02:00
}
}
// Return the hard-coded default language as final fallback
return $config->default;
2011-03-06 20:52:31 +02:00
}
/**
* Initialize the config and cookies
*/
static public function init()
{
$config = Kohana::$config->load('multilang');
2011-03-06 20:52:31 +02:00
// Get the list of supported languages
$langs = $config->languages;
2011-03-06 20:52:31 +02:00
// Set the language in I18n
I18n::lang($langs[Request::$lang]['i18n']);
// Set locale
setlocale(LC_ALL, $langs[Request::$lang]['locale']);
$cookie = $config->cookie;
2011-03-06 20:52:31 +02:00
// Update language cookie if needed
if(Cookie::get($cookie) !== Request::$lang)
{
Cookie::set($cookie, Request::$lang);
}
}
/**
* Return a language selector menu
* @param boolean $current Display the current language or not
* @return View
*/
static public function selector($current = TRUE)
{
$config = Kohana::$config->load('multilang');
$languages = $config->languages;
2011-03-06 20:52:31 +02:00
// Get the current route name
$current_route = Route::name(Request::initial()->route());
2011-03-06 20:52:31 +02:00
$params = Request::initial()->param();
if($current_route !== 'default' && strpos($current_route, '.') !== FALSE)
2011-03-06 20:52:31 +02:00
{
// Split the route path
list($lang, $name) = explode('.', $current_route, 2);
}
else
{
2011-03-06 20:52:31 +02:00
$name = $current_route;
}
// Create uris for each language
2011-06-26 20:09:00 +03:00
foreach($languages as $lang => &$language)
{
// If it's the current language
2011-06-26 20:09:00 +03:00
if($lang === Request::$lang)
2011-03-06 20:52:31 +02:00
{
// We only display it when required
2011-03-06 20:52:31 +02:00
if($current)
{
2011-06-26 20:09:00 +03:00
$selectors[$lang] = '<span class="multilang-selected multilang-'.$lang.'">'.$languages[$lang]['label'].'</span>';
}
}
else
{
2011-06-26 20:09:00 +03:00
$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
2011-06-26 20:09:00 +03:00
$route = Request::initial()->route();
2011-06-26 20:54:27 +03:00
$params['lang'] = NULL;
if(!$config->hide_default || $config->default !== $lang)
2011-06-26 20:09:00 +03:00
{
$params['lang'] = $lang;
}
2011-06-26 20:54:27 +03:00
2011-03-06 20:52:31 +02:00
}
else
2011-06-26 20:09:00 +03:00
{
if(Arr::get(Route::all(), $lang.'.'.$name))
{
$route = Route::get($name, $lang);
}
}
2011-06-26 20:09:00 +03:00
if($route !== NULL)
{
$selectors[$lang] = HTML::anchor($route->uri($params), $languages[$lang]['label'], array('class' => 'multilang-selectable multilang-'.$lang, 'title' => $languages[$lang]['label']));
}
2011-03-06 20:52:31 +02:00
}
}
2011-06-26 20:09:00 +03:00
// 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);
2011-06-26 20:09:00 +03:00
}
return '';
2011-03-06 20:52:31 +02:00
}
}