1
0
Fork 0
mirror of https://github.com/Oreolek/kohana-multilang.git synced 2024-06-14 22:00:46 +03:00

Adding files

v1.0
This commit is contained in:
Sebastien Guibert 2011-03-06 19:52:31 +01:00
parent 7b64fc93c5
commit a09e3e5659
10 changed files with 549 additions and 0 deletions

9
classes/multilang.php Normal file
View file

@ -0,0 +1,9 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Multilang class
*/
class Multilang extends Multilang_Core {
}

114
classes/multilang/core.php Normal file
View file

@ -0,0 +1,114 @@
<?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 best default language available and returns it.
* A language cookie and HTTP Accept-Language headers are taken into account.
*
* @return string language key, e.g. "en", "fr", "nl", etc.
*/
static public function find_default()
{
// Get the list of supported languages
$langs = (array) Kohana::config('multilang.languages');
$cookie = Kohana::config('multilang.cookie');
// Look for language cookie first
if($lang = Cookie::get($cookie))
{
// Valid language found in cookie
if(isset($langs[$lang]))
{
return $lang;
}
// Delete cookie with unset language
Cookie::delete($cookie);
}
// Parse HTTP Accept-Language headers
foreach(Request::accept_lang() as $lang => $quality)
{
// Return the first language found (the language with the highest quality)
if(isset($langs[$lang]))
{
return $lang;
}
}
// Return the hard-coded default language as final fallback
return Kohana::config('multilang.default');
}
/**
* Initialize the config and cookies
*/
static public function init()
{
// Get the list of supported languages
$langs = (array) Kohana::config('multilang.languages');
// Set the language in I18n
I18n::lang($langs[Request::$lang]['i18n']);
// Set locale
setlocale(LC_ALL, $langs[Request::$lang]['locale']);
$cookie = Kohana::config('multilang.cookie');
// 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)
{
$languages = (array) Kohana::config('multilang.languages');
// get the current route name
$current_route = Route::name(Request::initial()->route());
$default_language = Kohana::config('multilang.default');
$params = Request::initial()->param();
if(strpos($current_route, '.') !== FALSE)
{
// Split the route path
list($lang, $name) = explode('.', $current_route, 2);
} else {
$name = $current_route;
}
// Create uris for each language
foreach($languages as $code => &$language)
{
if($code == Request::$lang)
{
if($current)
{
$language['uri'] = FALSE;
} else {
unset($languages[$code]);
}
} else {
$language['uri'] = Route::get($name, $code)->uri($params);
}
}
return View::factory('multilang/selector')
->bind('languages', $languages);
}
}

View file

@ -0,0 +1,80 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Multilang module request class
* From the module https://github.com/GeertDD/kohana-lang
*/
class Multilang_Request extends Kohana_Request {
/**
* @var string request language code
*/
static public $lang = '';
/**
*
* Extension of the request factory method. If none given, the URI will
* be automatically detected. If the URI contains no language segment, the user
* will be redirected to the same URI with the default language prepended.
* If the URI does contain a language segment, I18n and locale will be set.
* Also, a cookie with the current language will be set. Finally, the language
* segment is chopped off the URI and normal request processing continues.
*
* @param string URI of the request
* @param Kohana_Cache cache object
* @return Request
*/
public static function factory($uri = TRUE, Cache $cache = NULL)
{
if(!Kohana::$is_cli)
{
// Get the list of supported languages
$langs = (array) Kohana::config('multilang.languages');
if($uri === TRUE)
{
// We need the current URI
$uri = Request::detect_uri();
}
// Normalize URI
$uri = ltrim($uri, '/');
// Look for a supported language in the first URI segment
if(!preg_match('~^(?:'.implode('|', array_keys($langs)).')(?=/|$)~i', $uri, $matches))
{
// If we don't have any, we look whether it's normal (is it in the no lang routes ?) or we need to append it
if(Request::process_uri($uri, Route::nolang_routes()))
{
return parent::factory($uri, $cache);
}
// We can't find a language, we're gonna need to look deeper
$lang = Multilang::find_default();
// Use the default server protocol
$protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
// Redirect to the same URI, but with language prepended
header($protocol.' 302 Found');
header('Location: '.URL::base(TRUE, TRUE).$lang.'/'.$uri);
// Stop execution
exit;
}
// Language found in the URI
Request::$lang = strtolower($matches[0]);
Multilang::init();
// Remove language from URI
$uri = (string) substr($uri, strlen(Request::$lang));
}
// Continue normal request processing with the URI without language*/
return parent::factory($uri, $cache);
}
}

222
classes/multilang/route.php Normal file
View file

@ -0,0 +1,222 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Multilang module route class
*/
class Multilang_Route extends Kohana_Route {
protected $_lang = '';
static protected $_nolang_routes = array();
/**
* Altered method to allow multiple routes for i18n.
* You can pass an array with the language code as the key and the uri as the value.
*
* Route::set('homepage', array(
* 'en' => 'home',
* 'fr' => 'accueil',
* ))->defaults(array(
* 'controller' => 'homepage',
* 'action' => 'index',
* ));
*
* Stores a named route and returns it. The "action" will always be set to
* "index" if it is not defined.
*
* Route::set('default', '(<controller>(/<action>(/<id>)))')
* ->defaults(array(
* 'controller' => 'welcome',
* ));
*
* @param string route name
* @param mixed URI pattern or Array of URI patterns or a lambda/callback function
* @param array regex patterns for route keys
* @param mixed route lang code OR FALSE if you wanna prevent the language code from being added
* @return Route
*/
static public function set($name, $uri_callback = NULL, $regex = NULL, $lang = NULL)
{
if($lang)
{
$name = $lang.'.'.$name;
}
Route::$_routes[$name] = new Route($uri_callback, $regex, $lang);
if($lang === FALSE)
{
Route::$_nolang_routes[$name] = Route::$_routes[$name];
}
return Route::$_routes[$name];
}
/**
* Retrieves a named route.
*
* $route = Route::get('default');
*
* @param string route name
* @return Route
* @throws Kohana_Exception
*/
static public function get($name, $lang = NULL)
{
// We use the current language if none given
if(is_null($lang))
{
$lang = Request::$lang;
}
// We first look for a "given_language.name" route.
if(isset(Route::$_routes[$lang.'.'.$name]))
{
$name = $lang.'.'.$name;
// then the default language
} elseif(isset(Route::$_routes[Kohana::config('multilang.default').'.'.$name])) {
$name = Kohana::config('multilang.default').'.'.$name;
}
$route = parent::get($name);
if(!is_null($route))
{
$route->_lang = $lang;
}
return $route;
}
/**
* Altered constructor to handle multilingual routes
*
* Creates a new route. Sets the URI and regular expressions for keys.
* Routes should always be created with [Route::set] or they will not
* be properly stored.
*
* $route = new Route($uri, $regex);
*
* The $uri parameter can either be a string for basic regex matching or it
* can be a valid callback or anonymous function (php 5.3+). If you use a
* callback or anonymous function, your method should return an array
* containing the proper keys for the route. If you want the route to be
* "reversable", you need to return a 'uri' key in the standard syntax.
*
* $route = new Route(function($uri)
* {
* if (list($controller, $action, $param) = explode('/', $uri) AND $controller == 'foo' AND $action == 'bar')
* {
* return array(
* 'controller' => 'foobar',
* 'action' => $action,
* 'id' => $param,
* 'uri' => 'foo/bar/<id>.html
* );
* }
* });
*
* @param mixed route URI pattern or lambda/callback function
* @param array key patterns
* @param
* @return void
* @uses Route::_compile
*/
public function __construct($uri = NULL, array $regex = NULL, $lang = NULL)
{
$this->_lang = $lang;
return parent::__construct($uri, $regex);
}
/**
* Altered method to handle multilingual uris.
*
* Generates a URI for the current route based on the parameters given.
*
* // Using the "default" route: "users/profile/10"
* $route->uri(array(
* 'controller' => 'users',
* 'action' => 'profile',
* 'id' => '10'
* ));
*
* @param array URI parameters
* @return string
* @throws Kohana_Exception
* @uses Route::REGEX_Key
*/
public function uri(array $params = NULL)
{
$uri = parent::uri($params);
// We add the language code if required
if($this->_lang)
{
return $this->_lang.'/'.$uri;
}
return $uri;
}
/**
* Altered method to handle multilingual parameter
*
* Create a URL from a route name. This is a shortcut for:
*
* echo URL::site(Route::get($name)->uri($params), $protocol);
*
* @param string route name
* @param array URI parameters
* @param mixed protocol string or boolean, adds protocol and domain
* @return string
* @since 3.0.7
* @uses URL::site
*/
static public function url($name, array $params = NULL, $protocol = NULL, $lang = NULL)
{
// Create a URI with the route and convert it to a URL
return URL::site(Route::get($name, $lang)->uri($params), $protocol);
}
/**
* Get all the routes without any language code
* @return array
*/
static public function nolang_routes()
{
return Route::$_nolang_routes;
}
/**
* Saves or loads the route cache. If your routes will remain the same for
* a long period of time, use this to reload the routes from the cache
* rather than redefining them on every page load.
*
* It remakes the nolang_routes array too for language less routes
*
* if ( ! Route::cache())
* {
* // Set routes here (or include a file for example)
* Route::cache(TRUE);
* }
*
* @param boolean cache the current routes
* @return void when saving routes
* @return boolean when loading routes
* @uses Kohana::cache
*/
static public function cache($save = FALSE)
{
$return = parent::cache($save);
if($save !== TRUE && Route::$_routes)
{
Route::$_nolang_routes = array();
foreach(Route::$_routes as $name => $route)
{
if($route->_lang === FALSE)
{
Route::$_nolang_routes[$name] = Route::$_routes[$name];
}
}
}
return $return;
}
}

View file

@ -0,0 +1,54 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Multilang module routes class
*/
class Multilang_Routes {
protected $_routes = array();
/**
* Set routes for each language
* You can pass an array with the language code as the key and the uri as the value.
*
* Routes::set('homepage', array(
* 'en' => 'home',
* 'fr' => 'accueil',
* ))->defaults(array(
* 'controller' => 'homepage',
* 'action' => 'index',
* ));
*
* @param string route name
* @param array URI patterns (array of "language code" => "uri")
* @param array regex patterns for route keys
* @return Routes
*/
static public function set($name, $uris = array(), $regex = NULL)
{
$routes = new Routes();
// we add all the routes setting the name to code.name (en.homepage for example).
foreach($uris as $code => $uri)
{
$routes->_routes[$code.'.'.$name] = Route::set($name, $uri, $regex, $code);
}
return $routes;
}
/**
* Set the defaults values for each route
* @param array $defaults
* @return Multilang_Routes
*/
public function defaults(array $defaults = NULL)
{
foreach($this->_routes as $route)
{
$route->defaults($defaults);
}
return $this;
}
}

8
classes/request.php Normal file
View file

@ -0,0 +1,8 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Multilang module request class
*/
class Request extends Multilang_Request {
}

8
classes/route.php Normal file
View file

@ -0,0 +1,8 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Multilang module route class
*/
class Route extends Multilang_Route {
}

8
classes/routes.php Normal file
View file

@ -0,0 +1,8 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Multilang module routes class
*/
class Routes extends Multilang_Routes {
}

32
config/multilang.php Normal file
View file

@ -0,0 +1,32 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Multilang module config file
*/
/*
* List of available languages
*/
return array(
'default' => 'en',
'cookie' => 'lang',
'languages' => array(
/*
'en' => array(
'i18n' => 'en_US',
'locale' => array('en_US.utf-8'),
'label' => 'english',
),
'fr' => array(
'i18n' => 'fr_FR',
'locale' => array('fr_FR.utf-8'),
'label' => 'français',
),
'de' => array(
'i18n' => 'de_DE',
'locale' => array('de_DE.utf-8'),
'label' => 'deutsch',
),
*/
),
);

View file

@ -0,0 +1,14 @@
<ul class="multilang-selector">
<?php foreach($languages as $code => $language): ?>
<?php if($language['uri']): ?>
<li class="multilang-selectable multilang-<?php echo $code; ?>">
<?php echo HTML::anchor($language['uri'], $language['label'], array('title' => $language['label'])); ?>
</li>
<?php else: ?>
<li class="multilang-selected multilang-<?php echo $code; ?>">
<span><?php echo $language['label']; ?></span>
</li>
<?php endif; ?>
<li cllass="clearer"></li>
<?php endforeach; ?>
</ul>