Skip to content

Language Selection 2

Derek Jones edited this page Jul 5, 2012 · 10 revisions

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.

Further information

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:

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('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:

...
<?=$this->load->view('elements/language');?>
...
  1. 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;?>'/>
...
  1. And last but not least - write the texts in your language files in UTF-8 character encoding.

Category:Internationalization

Clone this wiki locally