-
Notifications
You must be signed in to change notification settings - Fork 0
Internationalizing the Language class
I needed support for several languages in my application, so I modified the language class to support multiple languages, based on the "idiom". This is useful in case some of your users would like to view pages in other languages or you need to display several languages at the same time.
I have solved the following issues in the Language class:
- The ability to load languages based on their code, i.e. en-US, ru, etc..
- The ability to load files with same name, but in different folders. At present, if you try to load 2 files with the same name, which are in different folders, the second file will not be loaded. For example:
./en-US/warnings_lang.php ./ru/warnings_lang.php
As a result of loading these files, the ./ru/warnings_lang.php will not be loaded.
Usage:
- To load a file, use the following syntax:
$this->lang->load('info','ru');
- To get a line from the loaded file:
```php
$this->lang->line('loading','ru');
You can also do the following:
$this->lang->line('loading','en-US');
$this->lang->line('loading','ru');
If you set your default language to "en-US" in the config.php file, then you can rewrite the above as:
$this->lang->line('loading');
$this->lang->line('loading','ru');
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package CodeIgniter
* @author Rick Ellis
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://www.codeignitor.com/user_guide/license.html
* @link http://www.codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Language Class
*
* @package CodeIgniter
* @subpackage Libraries
* @category Language
* @author Rick Ellis
* @link http://www.codeigniter.com/user_guide/libraries/language.html
*/
class CI_Language {
var $language = array();
var $is_loaded = array();
var $default_lang="";
/**
* Constructor
*
* @access public
*/
function CI_Language()
{
log_message('debug', "Language Class Initialized");
}
function updateDefaultLanguage()
{
//Set the language from the config file
$CI =&get;_instance();
$deft_lang = $CI->config->item('language');
if ($deft_lang == '')
$this->default_lang='en-US';
else
$this->default_lang=$deft_lang;
if (!isset($this->language[$this->default_lang]))
$this->language[$this->default_lang]= array();
if (!isset($this->is_loaded[$this->default_lang]))
$this->is_loaded[$this->default_lang]= array();
log_message('debug',"Default language set to:".$this->default_lang);
}
// --------------------------------------------------------------------
/**
* Load a language file
*
* @access public
* @param mixed the name of the language file to be loaded. Can be an array
* @param string the language (english, etc.)
* @return void
*/
function load($langfile = '', $idiom = '', $return = FALSE)
{
$langfile = str_replace(EXT, '', str_replace('_lang.', '', $langfile)).'_lang'.EXT;
$this->updateDefaultLanguage();
//Set the idiom
if ($idiom == '')
$idiom=$this->default_lang;
//Check if an array for this language exists
if (!isset($this->language[$idiom]))
$this->language[$idiom]=array();
if (!isset($this->is_loaded[$idiom]))
$this->is_loaded[$idiom]=array();
if (in_array($langfile, $this->is_loaded[$idiom], TRUE))
{
return;
}
// Determine where the language file is and load it
if (file_exists(APPPATH.'language/'.$idiom.'/'.$langfile))
{
log_message('debug','Language file found at:'.APPPATH.'language/'.$idiom.'/'.$langfile);
include(APPPATH.'language/'.$idiom.'/'.$langfile);
}
else
{
if (file_exists(BASEPATH.'language/'.$idiom.'/'.$langfile))
{
log_message('debug','Language file found at:'.APPPATH.'language/'.$idiom.'/'.$langfile);
include(BASEPATH.'language/'.$idiom.'/'.$langfile);
}
else
{
show_error('Unable to load the requested language file: language/'.$langfile);
}
}
if ( ! isset($lang))
{
log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
return;
}
if ($return == TRUE)
{
return $lang;
}
$this->is_loaded[$idiom][] = $langfile;
$this->language[$idiom] = array_merge($this->language[$idiom], $lang);
unset($lang);
log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
return TRUE;
}
// --------------------------------------------------------------------
/**
* Fetch a single line of text from the language array
*
* @access public
* @param string the language line
* @return string
*/
function line($line = '', $lang="")
{
if ($lang=="")
$lang=$this->default_lang;
return ($line == '' OR ! isset($this->language[$lang][$line])) ? FALSE : $this->language[$lang][$line];
}
}
// END Language Class
?>
If you want to use "en-US" or "ru" or "en-GB", make sure that the folders inside the "language" folder are named respectively.