-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Language Selection
There is the new Language Selection 2 package that exhibits a slightly different behavior and should be preferred for new projects.
Overview This package 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. To persist this overridden language selection a cookie is used. For this selection a flashdata supporting CodeIgniter Session library is necessary, e.g. DB Session, OB Session, etc.
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.5.x.
- DB Session, OB Session, etc.
- PHP of course - I have tested it on PHP 4.4.4, and 5.2.1.
Download All files necessary for the Language Selection are here:
-
File:lang_select_20090326.zip Release 3, 2009-03-26, fixed syntax error.
-
File:lang_select_20090114.zip Release 2, 2009-01-14, fixed cookie_prefix bug.
-
File:lang_select_20070401.zip Release 1, 2007-04-01, 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('session','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.
Inspired by
- CakePHP Locale Package, by Andy Dawson
- Code Igniter Mini-App, Language Detect Library by George Dunlop