Welcome Guest, Not a member yet? Register   Sign In
CI ecommerce
#12

[eluser]tomcode[/eluser]
I use URL's and session, session's only for convenience, in case of someone coming back.

I do :

1. have URL's set up :
http://example.com/lang/controller/method
where lang is a two letter token (ISO 639.1 notation, en, fr, de)

the routes :
Code:
// one set for every controller
$route['([a-z]+)/controller'] = 'controller';
$route['([a-z]+)/controller(:any)'] = 'controller/$1';

// when using remap() in the default controller
// I use this one as last instruction
$route['(:any)'] = "default_controller/$1";

2. I rename my languages accordingly :
english -> en
french -> fr
... etc.

3. I organize my templates under the view folder accordingly, a template folder for common views with subfolders for each language

4. If the site's index is called without a language token :
a. I look for a session language
b. then I look for a browser language
c. If I can't have a match with the site's language I use the site's default language
d. I set the language in the session and redirect to the found language

The code in MY_Controller :
Code:
class MY_Controller extends Controller {

    // ...

    function _remap()
    {
        $cur_lang = $this->set_language();

        // ..
    }

    // ...

    /**
     * Determines which language to use and sets it
     */
    function set_language()
    {
        $iso_lang = $this->uri->segment(1, 'index');
        
        $languages = $this->config->item('languages');
        
        $this->load->vars(array('languages' => $languages));
        
        $languages = array_keys($languages);
        
        $cur_lang = $iso_lang;
        
        if($cur_lang == 'index')
        {
            $user_lang = $this->session->userdata('lang');
            
            if($user_lang)
            {
                $cur_lang = $user_lang;
            }
            else $cur_lang = $this->agent->match_language($languages);
        }
        
        if(! in_array($cur_lang, $languages))
        {
            $cur_lang = false;
        }
        else $this->config->set_item('language', $cur_lang);
        
        if(! $cur_lang || $iso_lang == 'index')
        {
            $this->lang_redirect($languages);
        }

        $this->lang->load('application');

        $this->session->set_userdata('lang', $iso_lang);

        return $iso_lang;
    }

    function lang_redirect($languages)
    {
        $lang = $this->config->item('language');
        
        // check to avoid redirect loops
        switch(TRUE)
        {
            case ! in_array($lang, $languages) :
            case ! is_dir(to_path('', realpath(BASEPATH), 'language', $lang)) :
            case ! is_dir(to_path('', realpath(APPPATH), 'language', $lang)) :

            show_error('Wrong default language configuration. Please contact the administrator.');
        }
        
        redirect($lang .$this->uri->uri_string());
    }
}


The MY_User_agent.php
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* User Agent Class Extension
*
* Matches the language of the browsing agent
*
* @package        CodeIgniter
* @subpackage    Libraries
* @category    User Agent
* @author        Thomas Traub
*/
class MY_User_agent extends CI_User_agent {
    
    
    /**
     * Matches a list of languages to the agent languages
     *
     * @param array numeric ISO 639.1 (two letter) language idents
     * @return mixed the best match or false
     */
    function match_language($languages)
    {
        $browser_langs = $this->languages();
    
        $lang_match = false;
        
        foreach($browser_langs as $browser_lang)
        {
            $browser_lang = $this->iso_639_1($browser_lang);
        
            if(in_array($browser_lang, $languages))
            {
                $lang_match = $browser_lang;
            
                break;
            }
        }
        
        return $lang_match;
    }
    
    function iso_639_1($browser_lang)
    {
        $browser_lang_parts = explode('-', trim($browser_lang, '-'));
        
        return $browser_lang_parts[0];
    }
}

Edit : Forgot, have a config item, too :
Code:
/*
|--------------------------------------------------------------------------
| Languages
|--------------------------------------------------------------------------
|
| The accepted languages in ISO 639.1 notation (two letters),
| set language keys MUST have corresponding installed language packs
| inside system/language and system/application/language
|
| The keys correspond to the first URI segment.
*/
$config['languages']    = array
(
    'nl'        => 'Neerlandes',
    'en'        => 'English',
    'fr'        => 'Français',
    // 'de'        => 'Deutsch',
);


Messages In This Thread
CI ecommerce - by El Forum - 02-03-2010, 03:35 PM
CI ecommerce - by El Forum - 02-03-2010, 03:56 PM
CI ecommerce - by El Forum - 02-03-2010, 04:55 PM
CI ecommerce - by El Forum - 02-04-2010, 12:26 AM
CI ecommerce - by El Forum - 02-04-2010, 01:04 AM
CI ecommerce - by El Forum - 02-04-2010, 02:38 AM
CI ecommerce - by El Forum - 02-04-2010, 03:58 AM
CI ecommerce - by El Forum - 02-04-2010, 12:06 PM
CI ecommerce - by El Forum - 02-18-2010, 09:39 AM
CI ecommerce - by El Forum - 02-22-2010, 01:40 PM
CI ecommerce - by El Forum - 03-15-2010, 07:10 AM
CI ecommerce - by El Forum - 03-15-2010, 07:47 AM
CI ecommerce - by El Forum - 03-16-2010, 05:37 AM
CI ecommerce - by El Forum - 03-16-2010, 05:42 AM
CI ecommerce - by El Forum - 03-16-2010, 05:48 AM
CI ecommerce - by El Forum - 03-16-2010, 05:57 AM
CI ecommerce - by El Forum - 03-16-2010, 06:50 AM
CI ecommerce - by El Forum - 03-16-2010, 07:09 AM
CI ecommerce - by El Forum - 03-16-2010, 07:27 AM
CI ecommerce - by El Forum - 03-16-2010, 05:16 PM
CI ecommerce - by El Forum - 03-16-2010, 05:19 PM



Theme © iAndrew 2016 - Forum software by © MyBB