Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.