[FIXED] language in url working local not on mt hosting - El Forum - 10-08-2007
[eluser]yusufdestina[/eluser]
I've just finished a site where the language is specified in the url like: http://www.mysite.com/en/
If no language is given it takes a default one.
based on this thread : http://ellislab.com/forums/viewthread/47189/
I'm using two hooks for this.
Code: $hook['pre_system'] = array(
'class' => 'user_pref',
'function' => 'user_language',
'filename' => 'user_pref.php',
'filepath' => 'hooks'
);
$hook['post_controller_constructor'] = array(
'class' => 'set_lang',
'function' => 'setLanguage',
'filename' => 'set_lang.php',
'filepath' => 'hooks'
);
hooks/user_pref.php
Code: <?php
class user_pref {
var $languages = array (
'en' => 'english',
'nl' => 'dutch',
'fr' => 'french'
);
var $browser_lang;
function user_pref(){
list($this->browser_lang) = explode(",",getenv('HTTP_ACCEPT_LANGUAGE'));
}
function user_language() {
if(isset($_SERVER['PATH_INFO'])){
$pref = explode('/', trim($_SERVER['PATH_INFO'], "/"));
if (isset ($this->languages[strtolower($pref[0])])) {
$GLOBALS['language'] = $pref[0];
array_shift($pref);
$_SERVER['PATH_INFO'] = implode('/', $pref);
} else {
$GLOBALS['language'] = 'nl';
}
}else {
$GLOBALS['language'] = (isset ($this->languages[strtolower($this->browser_lang)]))? strtolower($this->browser_lang) : 'nl';
//$GLOBALS['lang'] = 'nl';
}
}
}
?>
hooks/set_lang.php
Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class set_lang{
var $sitelang;
var $lang;
function set_lang(){
$this->obj =& get_instance();
}
function setLanguage(){
$this->obj->sitelang = $GLOBALS['language'].'/';
$this->obj->language = $GLOBALS['language'];
}
}
?>
No extra modifications in the routes file are needed, note this works fine localy and tested recently on another server online, but fails on Media Temple (gs) running php5.
This is what i get when browsing without a language set in the url:
Code: DEBUG - 2007-10-08 16:26:53 --> Config Class Initialized
DEBUG - 2007-10-08 16:26:53 --> Hooks Class Initialized
DEBUG - 2007-10-08 16:26:53 --> user_pref Class Initialized
DEBUG - 2007-10-08 16:26:53 --> user_pref Class constructer var browser_lang: en-us
DEBUG - 2007-10-08 16:26:53 --> user_pref Class method user_language Initialized
DEBUG - 2007-10-08 16:26:53 --> user_pref Class method user_language PATH_INFO not set
DEBUG - 2007-10-08 16:26:53 --> user_pref Class method user_language using default language
ERROR - 2007-10-08 16:26:53 --> 404 Page Not Found -->
This is when i define a language in the url
Code: DEBUG - 2007-10-08 16:28:48 --> Config Class Initialized
DEBUG - 2007-10-08 16:28:48 --> Hooks Class Initialized
DEBUG - 2007-10-08 16:28:48 --> user_pref Class Initialized
DEBUG - 2007-10-08 16:28:48 --> user_pref Class constructer var browser_lang: en-us
DEBUG - 2007-10-08 16:28:48 --> user_pref Class method user_language Initialized
DEBUG - 2007-10-08 16:28:48 --> user_pref Class method user_language PATH_INFO is set.
DEBUG - 2007-10-08 16:28:48 --> user_pref Class method user_language -> found language in array set GLOBALS lang: nl
My .htaccess file...
Code: <IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dev
RewriteRule ^public/css/(.*\.css) combine.php?type=css&files;=$1
RewriteRule ^public/js/(.*\.js) combine.php?type=javascript&files;=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Help....
[FIXED] language in url working local not on mt hosting - El Forum - 10-09-2007
[eluser]yusufdestina[/eluser]
Fixed, I've changed in the config.php
Code: #$config['uri_protocol'] = "AUTO";
$config['uri_protocol'] = "PATH_INFO";
tnx for your time.
|