Welcome Guest, Not a member yet? Register   Sign In
Multilingual website gestion
#1

[eluser]dorwin[/eluser]
Hi,

(For the record I'm french and beginner with CI so please excuse me and if you answer, could you please use easy words ?Wink )

So, I'm on a multilingual website. I found this post :
http://ellislab.com/forums/viewthread/101790/P0/ very helpfull so,

There im my route config :
Code:
$route['(en|fr)/(:any)'] = "$2/$1";

and there is a few code (wich is working (i think)) :

[algo]
if language is in URL (ex : site.com/en)
-> loading of the lang file (here "en")
else
-> check navigator lang
->if navigator lang = fr or en
-> redirect (rewrite the url) with $lang
else
->redirect with "en" as lang

Code :
Code:
//---CHECK LANG-----//
        if ($this->uri->segment(1) == "fr" OR $this->uri->segment(1) == "en") //si URL OK
        {
            //chargement du fichier de langue choisi
            $this->lang->load($this->uri->segment(2), $this->uri->segment(1));
        }
        else //sinon langue non précisé
        {
            //recherche de la langue du navigateur
            $lang = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
            $lang = strtolower(substr(chop($lang[0]),0,2));
            
            if ($lang == "fr" OR $lang == "en") //si le navigateur est en FR ou EN
            {
                //on réécrit l'url avec la langue du navigateur
                redirect(base_url().$lang.$this->uri->uri_string());
            }
            else    //par exemple RU, IT, etc..
            {
                //on redirige page EN (english)
                redirect(base_url().'en'.$this->uri->uri_string());
            }
                
        }

I just developped it and i know it's optimisable, but :
the question is, have I to put this code on every constructor of every controller ?
I know i can put this on a function but the problem is the same, I have to call it on every constuctor ?

First, is there a place I can put this code just one time and,
Is my code (optimized or not) the good solution for multilingue website ?

Thanks !
#2

[eluser]flaky[/eluser]
Best would be (at least what I prefer), you write a controller which has this code (and other controlling stuff) and the other controllers extends this controller. Or as others suggest use MY_Controller. In both ways, it should work.
#3

[eluser]dorwin[/eluser]
Damned I'm so stupid !

Thanks a lot for this tips, wich will help me for more than juste lang gestion imo Wink
#4

[eluser]dorwin[/eluser]
Ok,

its works, I had to add few lines because it doesnt work with base URL, like site.com : Redirect is ok, ia have site.com/fr but there is not "fr" controller so:

Route :
Code:
$route['(en|fr)/(:any)'] = "$2";
$route['(en|fr)'] = "accueil";
accueil is my defaut controller if none specified

and My_Controller :
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller {

/*
| -------------------------------------------------------------------
|  Constructeur : Sorte de méga header
| -------------------------------------------------------------------
| Description : chargela langue du site, redirige etc...
*/
    function MY_Controller()
    {
        parent::Controller();
        
        //---CHECK LANG-----//
        if ($this->uri->segment(1) == "fr" OR $this->uri->segment(1) == "en") //si URL OK
        {
            if (!$this->uri->segment(2)) //si on est sur la page d'accueil
                $this->lang->load('accueil', $this->uri->segment(1)); //chargement du fichier de langue accueil
            else    
                $this->lang->load($this->uri->segment(2), $this->uri->segment(1)); //sinon le fichier demandé
        }
        else //sinon langue non précisé
        {
            //recherche de la langue du navigateur
            $lang = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
            $lang = strtolower(substr(chop($lang[0]),0,2));
            
            if ($lang == "fr" OR $lang == "en") //si le navigateur est en FR ou EN
            {
                //on réécrit l'url avec la langue du navigateur
                redirect(base_url().$lang.$this->uri->uri_string());
            }
            else    //par exemple RU, IT, etc..
            {
                //on redirige page EN (english)
                redirect(base_url().'en'.$this->uri->uri_string());
            }
                
        }        
    }        
}
?>
There is a new if : if (segement(2) don't exist (site.com/fr), lang file called is accueil_lang.

Every others controllers extend My_Controller, wich extend Controller.

Multilingual website is on good way now Smile

Thanks !




Theme © iAndrew 2016 - Forum software by © MyBB