I wanna make a multilanguage website, but I do not want that the language appear in URI like example.com/fr/about (I do not want this). I just want to change the text language. My problem is that the first load language that I do is for ever. why?
If I do:
$this->config->set_item(‘language’,‘english’);
$this->lang->load(‘messages’);
$this->config->set_item(‘language’,‘french’);
$this->lang->load(‘messages’);
or
$this->lang->load(‘messages’,‘english’);
$this->lang->load(‘messages’,‘french’);
just the english appear. How can I fix this?
My config language autoload is empty.
Thank you for your help.
I use a hook for this.
function pick_language() {
require_once(APPPATH.'/config/language.php');
session_start();
// Lang set in URL via ?lang=something
if(!empty($_GET['lang']))
{
// Turn en-gb into en
$lang = 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'];
}
// Lang has is picked by a user.
// Set it to a session variable so we are only checking one place most of the time
elseif( !empty($_COOKIE['lang_code']) )
{
$lang = $_SESSION['lang_code'] = $_COOKIE['lang_code'];
}
// Still no Lang. Lets try some browser detection then
else if (!empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ))
{
// explode languages into array
$accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
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 = 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;
}
}
}
// If no language has been worked out - or it is not supported - use the default
if(empty($lang) or !in_array($lang, array_keys($config['supported_languages'])))
{
$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;
// Load CI config class
$CI_config =& load_class('Config');
// Set the language config. Selects the folder name from its key of 'en'
$CI_config->set_item('language', $config['supported_languages'][$lang]['folder']);
// Sets a constant to use throughout ALL of CI.
define('CURRENT_LANGUAGE', $lang);
}
Not only will that set the correct language for you but it will give you a constant CURRENT_LANGUAGE which contains the language they are using ('en', 'de', etc).
The available languages for this come from a config item:
/*
|--------------------------------------------------------------------------
| Supported Languages
|--------------------------------------------------------------------------
|
| Contains all languages your site will store data in. Other languages can
| still be displayed via language files, thats totally different.
|
| Check for HTML equivalents for characters such as � with the URL below:
| http://htmlhelp.com/reference/html40/entities/latin1.html
|
*/
$config['supported_languages'] = array(
'en'=> array('name' => 'English', 'folder' => 'english'),
'es'=> array('name' => 'Español', 'folder' => 'spanish'),
'fr'=> array('name' => 'Français', 'folder' => 'french'),
'de'=> array('name' => 'German', 'folder' => 'german')
);
/*
|--------------------------------------------------------------------------
| Default Language
|--------------------------------------------------------------------------
|
| If no language is specified, which one to use? Must be in the array above
|
| en
|
*/
$config['default_language'] = 'en';
This will pick up the correct language from GET (http://somesite.com/?lang=de), then check session variable (populated by a correct match) then checks the browser for accept-lang header.
Whichever has a match first will be used.
Read the developer guide, there are some things to consider:
- "Language files must be named with _lang.php as the file extension." - your file is just called "message"
- Use different files for different languages
- Simply doing
$this->lang->load("messages");
without second argument will load the default language
- Show translated texts with
$this->lang->line('some_key');
You can clear the loaded languages by using:
$this->lang->is_loaded = array();
$this->lang->language = array();
This will let you load another language afterwards by using the $this->lang->load('language_file', 'language').
Hope this helps