Skip to content

Instantly share code, notes, and snippets.

@vitalyrotari
Created February 16, 2012 11:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vitalyrotari/1844219 to your computer and use it in GitHub Desktop.
Save vitalyrotari/1844219 to your computer and use it in GitHub Desktop.
Language support for FuelPHP
/**
* app/bootstrap.php
*/
Autoloader::add_classes(array(
// Add classes you want to override here
// Example: 'View' => APPPATH.'classes/view.php',
'Uri' => APPPATH.'classes/uri.php',
'LayoutHelper' => APPPATH.'classes/layouthelper.php',
));
/**
* Localization & internationalization settings
*
* app/config/config.php
*/
'language' => 'en', // Default language
'language_fallback' => 'en', // Fallback language when file isn't available for default language
'locale' => 'en_US', // PHP set_locale() setting, null to not set
'locales' => array(
'en' => 'en_US',
'ro' => 'ro_MD',
'ru' => 'ru_RU',
),
<?php
/**
* app/classes/layouthelper.php
*/
class LayoutHelper
{
public static function url($uri = '', $params = array())
{
$lang = Config::get('language');
if ( ! empty($uri))
{
$lang .= '/';
}
if ($new_uri = Router::get($uri, $params))
{
$uri = $new_uri;
}
return Uri::create($lang.$uri);
}
}
<?php
/**
* app/config/routes.php
*/
return array(
'_root_' => 'welcome/index', // The default route
'_404_' => 'welcome/404', // The main 404 route
'history' => 'welcome/history',
);
/**
* app/views/template.php
*/
<a href="<?php LayoutHelper::url('/history'); ?>"><?php echo Lang::get('history'); ?></a>
<?php
/**
* app/classes/uri.php
*/
class Uri extends Fuel\Core\Uri
{
public function __construct($uri = NULL)
{
parent::__construct($uri);
$this->detect_language();
}
public function detect_language()
{
if ( ! count($this->segments))
{
return false;
}
$first = $this->segments[0];
$locales = Config::get('locales');
if(array_key_exists($first, $locales))
{
array_shift($this->segments);
$this->uri = implode('/', $this->segments);
Config::set('language', $first);
Config::set('locale', $locales[$first]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment