Welcome Guest, Not a member yet? Register   Sign In
How to make a multi country web site? (Best practices)
#11

[eluser]Vicente Russo[/eluser]
Few questions about this hook.

- I can`t use $this inside the picklanguage.php file because it`s not a class.
- Is this method already tested?

Smile

[quote author="Phil Sturgeon" date="1231346969"]Have just finished a hook that does exactly this.

2x CI config variables.
- Supported languages = array('EN' => English, 'FR' => French, etc)
- Default language = 'EN'

1x Hook entry in config/hooks.php

Code:
$hook['pre_controller'] = array(
'function' => 'PickLanguage',
'filename' => 'pick_language.php',
'filepath' => 'hooks',
);

1x Hook file

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

session_start();

function PickLanguage() {
    require_once(APPPATH.'/config/languages.php');
      
    // Lang set in URL via ?lang=something
    if(!empty($_GET['lang']))
    {
        // Uppercase and only 2 characters long
        $lang = strtoupper(substr($_GET['lang'], 0, 2));
        $_SESSION['lang_code'] = $lang;
    }
    
    // Lang has already been set and is stored in a session
    elseif( !empty($_SESSION['lang_code']) )
    {
        $lang = $_SESSION['lang_code'];
    }
    
    // Still no Lang. Lets try some browser detection then
    else
    {
        $accept_langs = $this->obj->input->server('HTTP_ACCEPT_LANGUAGE');
        
        if (!empty( $accept_langs ))
        {
            // explode languages into array
            $accept_langs = explode(',', $accept_langs);
        
            log_message('debug', 'Checking browser languages: '.implode(', ', $accept_langs));
            
            // Check them all, until we find a match
            foreach ($accept_langs as $lang)
            {
                // Turn en-gb into EN
                $lang = strtoupper(substr($lang, 0, 2));
                
                // Check its in the array. If so, break the loop, we have one!
                if(in_array($lang, array_keys($config['supported_languages'])))
                {
                    break;
                }
            }
        }

        // Still not found a lang, lets just use the default
        if(empty($lang))
        {
            $lang = $config['default_language'];
        }
        
        // Whatever we decided the lang was, save it for next time to avoid working it out again
        $_SESSION['lang_code'] = $lang;
    }
    
    // Check the provided lang is available
    if(!in_array($lang, array_keys($config['supported_languages'])))
    {
        exit('This language doesnt exist boso!');
    }

    // Load CI config class
    $CI_config =& load_class('Config');

    // Set the language config. Getting 'English' from the array by calling it from its key of 'EN', and make it lowercase so CI looks for 'english'
    $CI_config->set_item('language', $config['supported_languages'][$lang]);

    // Sets a constant to use throughout ALL of CI.
    define('DEFAULT_LANGUAGE', $lang);
}

?>

Then, you can use DEFAULT_LANGUAGE in all your models and everywhere else in CI to reference your lang specific data. Works beautifully!

Edit - 07/01/09: Added in browser detection support.[/quote]
#12

[eluser]Jondolar[/eluser]
The problem that I see with this technique is that search engines will not be able to find anything other than your home page for that language. Once they land on the new language page (set with the "get" variable), the session will be lost and the next page that is spidered will be back to the original language.

If you put the language in your URL then you will ensure that your pages will get indexed.
#13

[eluser]henry178[/eluser]
I don't find this file in the function PickLanguage.... why?

require_once(APPPATH.'/config/languages.php');
#14

[eluser]alex2012[/eluser]
[quote author="xwero" date="1231336132"][quote author="Nonox" date="1231351860"]
I have to divide the structure in different directories for each country? and repeat all the programmings site countless times?[/quote]

Simple answer no. This is when the only thing that changes are the strings and some odd layout elements.
If you are talking about different data and totally different layouts than you should consider programming the common elements as modules and put country specific parts in separate directories.

But as i understand you are not planning to have many differences between the countries.[/quote]

Hello!

i want to know if you can help me out with the following:

If we built a complex ecommerce but we didnt made it multicountry, but now we want to make it multicountry, so if you click on USA then you will see only the products in the USA, and if you click on BRASIL then you will see only the products in Brasil, and so on with the rest of the countries.

How complicated is to make the structure in multicountry AFTER the ecommerce platform is already finished???

I mean we want the system to be ONE, but with different country versions, like for example: amazon.com, amazon.uk, amazon.fr, etc.

ill wait for your details! many thanks!




Theme © iAndrew 2016 - Forum software by © MyBB