-
Notifications
You must be signed in to change notification settings - Fork 0
Language Selection 2
Overview This package is a modified version of the Language Selection. It allows you to select the national language of your web site. The initial language is determined from the browser's language list as available to PHP as the $_SERVER['HTTP_ACCEPT_LANGUAGE'] value. This list should reflect the language preferences of the user.
The entries of the browser's list usually are predefined by the language variant of the browser, but the user can modify this list if he or she likes to read web content in various languages.
In general your web site will not support all languages of the world. So you configure the supported languages and the directories that contain the necessary CodeIgniter language files. The language detection tries to find a supported language that is in the browswer's list. If that fails, a configured default language is used.
Additionally you can insert a view element for language selection at some position of your web page that allows to override the initial setting.
NOTE: This version does not need a redirect to change the language. There are no more dependencies on the Session class.
To persist this overridden language selection both a cookie and an additional URI segment are used. The additional URI segment contains the string 'l_' and the ISO language code, for example 'l_en-uk' or 'l_de-at'. This URI segment is appended to all link or form URLS via the modified url_helper and the extended Config class.
The language selection results in a change of the standard Code Igniter language configuration ($config['language']), so no further modifications of your web application is necessary.
Requirements
- CodeIgniter - I have tested it on 1.7.0 but other versions should work fine too.
- PHP of course - I have tested it on PHP 4.4.9, and 5.2.1.
See It Run Here you can find a demo site.
Download All files necessary for the Language Selection are here:
-
File:lang_select_2_20090604.zip Release 3, 2009-06-04, fixed suppress language URI segment bug.
-
File:lang_select_2_20090114.zip Release 2, 2009-01-14, fixed cookie_prefix bug.
-
File:lang_select_2_20090113.zip Release 1, 2009-01-13, original release.
Installation and Configuration
-
Unzip the downloaded archive into your Code Igniter installation. The archive contains subdirectories that show where the files have to go.
-
Modify your 'application/config/autoload.php' file:
...
$autoload['libraries'] = array('lang_detect');
...
$autoload['config'] = array('lang_detect');
-
Modify your 'application/config/lang_detect.php' file: Assign the standard ISO language code and optionally the ISO country code (that the browser sends) to the name of the respective language directory in the array $config['lang_avail']. And set one of the language-country codes as default value to $config['lang_default'].
-
Provide language flag images for each of the supported languages in your site's 'img/lang' directory. The images need to have the names .gif for the unselected and _sel.gif for the selected language, e.g. en-uk.gif and en-uk_sel.gif.
-
Modify all 'application/language//lang_lang.php files of the supported languages so that the tool tips of the flag image buttons show up correctly.
-
Insert the language selection view element into your view file:
...
<?=$this->load->view('elements/language');?>
...
- Ensure that your web page is rendered in UTF-8 character encoding:
<?php
// get selected language
$_lang = $this->config->item('lang_selected');
// send raw HTTP headers to set the content type for MS IE
$this->output->set_header('Content-Type: text/html; charset=UTF-8');
?>
...
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<meta http-equiv='content-language' content='<?=$_lang;?>'/>
...
- And last but not least - write the texts in your language files in UTF-8 character encoding.