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

[eluser]ashabi[/eluser]
So the project is well on the way. Things are going much better than anticipated.
But I am running into a problem with the multilanguage.
I have 2 languages: hebrew and english. English is the default. Since they are completely different languages (right to left, left to right) I figured I need 2 different sets of templates. So I have the english views sitting directly under the views folder and the arabic is views/heb/ folder. I also have to allow the ability for multiple currencies. Meaning English site is just in dollars but the hebrew site can be in dollars or the hebrew currency. The user on the home page picks which site he goes to.
Can anybody help me organize all this? I am so confused. Should I be using sessions or urls like www.mydomain.com/he or www.mydomain.com/en
Thanks.
#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',
);
#13

[eluser]ashabi[/eluser]
thanks but i don't know what i did wrong. its not working for me.
just to clarify. i loaded My_Controller in the library as well as My_User_Agent. Right?
#14

[eluser]Total Shop UK[/eluser]
To save you some time try setting up this eCommerce app.

http://codeigniter.com/wiki/Total_Shop_UK/
#15

[eluser]tomcode[/eluser]
Quote:... its not working for me
???

You do not need to load MY_Controller (not My_Controller) and MY_User_agent (not My_User_Agent).

The MY_ prefix triggers CI to autoload and extend the core classes, see the
User Guide, Extending Native Libraries
#16

[eluser]ashabi[/eluser]
tomcode - sorry I did that correctly, i just wanted to make it clear. But my home page works ok but the links don't work now, and also when i try http://mydomain.com/en/ or http://mydomain.com/he

is there something missing on the htaccess?

totalshopuk - looks great. but its missing the internationalization and the localization that I desparetly need. but i will definately take a more in depth look at it.
#17

[eluser]tomcode[/eluser]
ashabi, my solution works only with controllers which have a _remap() function.
#18

[eluser]ashabi[/eluser]
as a beginner, what does that mean?
#19

[eluser]tomcode[/eluser]
The _remap() function in a controller catches all incoming requests, all other methodes are ignored, so what You can do :

Code:
// put this method in Your Controllers
    function _remap()
    {
        // catch the requested method
        $cur_page = $this->uri->segment(2, 'index');
        
        // the public methods You wish to expose
        $methods = array('index');
        
        if(! in_array($cur_page, $methods))
        {
            show_404();
        }
        
        // call the method
        $this->$cur_page();
    }
#20

[eluser]Bart v B[/eluser]
@tomcode
Code:
<?php
$config['languages']    = array
(
    'nl'        => 'Neerlandes',
    'en'        => 'English',
    'fr'        => 'Français',
    // 'de'        => 'Deutsch',
);
?>

must be:

Code:
<?php
$config['languages']    = array
(
    'nl'        => 'Nederlands',
    'en'        => 'English',
    'fr'        => 'Français',
    // 'de
);
?>
:coolsmile:




Theme © iAndrew 2016 - Forum software by © MyBB