Welcome Guest, Not a member yet? Register   Sign In
method for multi language without session your opinion
#1

[eluser]axmed[/eluser]
i wanted to make multi language site that could cache the pages in the language selected, using session would only save one copy of that page in the last language selected(cause one controller for all languages), i did not want to lose caching, so i read about using routes and using a controller for every language thanks to n0xie, since i will be needing to write all urls in the config/routes.php i choose to do it instead in the language files.here is the example code tell me if this is a good way to do this, will it cause problems if its done in this way.

i used the Template Library from williamsconcepts

so here goes
the English controllers will be
Quote:http://test/home
and the Arabic
Quote: http://test/arabic/home

Code:
<?php
/* this is the English home the Arabic home is same thing only i use 1 for getGui_home(n)*/
class Home extends Controller {
    function Home()    {
        parent::Controller();
                /* this model loads the gui stuff for the page according to language */
        $this->load->model('gui_model');        
    }
    function index()    {
        $this->output->cache(1);
                 /* load the home the 0 is for english 1 is for Arabic*/
        $this->gui_model->getGui_home(0);
        $this->template->render();
    }
        
}

now the Gui_model its job is to load the region into the master template and to parse the data according to the language selected.

Code:
<?php
class Gui_model extends Model {
    
    function Gui_model() {
        parent::Model();
    }
    /*loads the appropriate language according to selection */
    function getGui_home($lan = 0){
        if ($lan == '1')    {
            $this->lang->load('gui', 'arabic');
            $this->template->add_css('css/arab_font.css');
        }else{
            $this->lang->load('gui', 'english');
            $this->template->add_css('css/eng_font.css');
        }    
        $this->template->add_css('css/base.css');    
        $title = "test";
        $data = $this->lang->language;
        $this->template->parse_view('menu', 'template/menu_home', $data,true);
        $this->template->write('title', $title, TRUE);
        $this->template->parse_view('header', 'template/header', $data,true);
        $this->template->parse_view('footer', 'template/footer', $data,true);
    }
}
?>

here is an example of a language files this one is the Arabic the English one is the same but the uri_* dont have "arabic/" to them and the text is in English Smile.

Code:
$lang['host'] = "اختبار";
$lang['home'] = "الرئيسية";
$lang['games'] = "الألعاب";
$lang['movies'] = "أفلام";
$lang['forum'] = "منتدى";

/*the uri of the arabic links*/
$lang['uri_home'] = "arabic/home";
$lang['uri_games'] = "arabic/games";
$lang['uri_movies'] = "arabic/movies";
$lang['uri_forum'] = "arabic/forum";

now for the view files i used the tags for the languages ex: {home} and for regions i used &lt;?= $content ?&gt; the regions are placed in the master template.the anchors on the view pages i used <?=base_url()."{uri_home}";?&gt; here is an example of a region i used for the main bar.

the browser will not show the code below so here is representation
X1: &lt;?=base_url()."{uri_home}";?&gt;
X2: &lt;?=base_url()."{uri_games}";?&gt;
X3: &lt;?=base_url()."{uri_movies}";?&gt;
X4: &lt;?=base_url()."{uri_forum}";?&gt;

Code:
<div id="top_menu">
    <ul>
        <li id="active"><a href="X1">{home}</a></li>
        <li><a href="X2">{games}</a></li>
        <li><a href="X3">{movies}</a></li>
        <li><a href="X4">{forum}</a></li>
    </ul>
</div>

the controllers are named the same for Arabic i use home.php and is placed in controllers/arabic/ and the English home.php in controllers/ .
so to keep track of the current pages and when the page is translated the selected controller will load in the selected language i used for the language anchors this code its placed in the header region.its 2 images representing the lang.thanks to theprodigy.

the browser will not show the code below so here is representation
x1:&lt;?= base_url().'arabic/'.$this->router->class; ?&gt;
x2:&lt;?= base_url().$this->router->class; ?&gt;

Code:
<div id="lang">
    <a href="X1"><img id="arabic" src="&lt;?= base_url(); ?&gt;images/arb.jpg" width="18"height="9" alt="arabic" /></a>
    <a href="X2"><img id="english" src="&lt;?= base_url(); ?&gt;images/eng.jpg" width="18"height="9" alt="english" /></a>
</div>

now i can cache the pages in the languages the user has selected i keep the uri's in the language files instead of the route and have a controller for every language.
what do you think is this a good way to do this,now i dont have to worry about user cookies enabled and cashing helps speed the pages with less cpu utilization, i dont know about search engine indexing. but is this a good way to do it.
i hope you can make since of it all
regards




Theme © iAndrew 2016 - Forum software by © MyBB