Created
February 16, 2012 11:30
-
-
Save vitalyrotari/1844219 to your computer and use it in GitHub Desktop.
Language support for FuelPHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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', | |
)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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', | |
), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* app/config/routes.php | |
*/ | |
return array( | |
'_root_' => 'welcome/index', // The default route | |
'_404_' => 'welcome/404', // The main 404 route | |
'history' => 'welcome/history', | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* app/views/template.php | |
*/ | |
<a href="<?php LayoutHelper::url('/history'); ?>"><?php echo Lang::get('history'); ?></a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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