Language Selection

Derek Jones edited this page Jul 5, 2012 · 17 revisions
Clone this wiki locally

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.

Further information


  • 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:

Installation and Configuration

  1. Unzip the downloaded archive into your Code Igniter installation. The archive contains subdirectories that show where the files have to go.

  2. Modify your 'application/config/autoload.php' file:

$autoload['libraries'] = array('session','lang_detect');
$autoload['config'] = array('lang_detect');
  1. 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'].

  2. 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.

  3. 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.

  4. Insert the language selection view element into your view file:

  1. Ensure that your web page is rendered in UTF-8 character encoding:
// 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');
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
  <meta http-equiv='content-language' content='<?=$_lang;?>'/>
  1. 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