Language improvements Codeigniter

September 22, 2010 by: Xavier Perez

The language library in Codeigniter is very simple, and doesn’t  support variable substitution, only partial or complete phrases.

For this reason, I have made minor changes to allow the variable substitution, thus it’s not the same espelling in different idiomas.

For example:

To show a price, every country have it’s own format, sometimes sig are before, sometimes after the number ( float or integer). My language helper solves it.

In your language/english directory, you will have your language file with this:

$lang[‘product_price’]      = ‘$ %6.0d’;

In your language/spanish directory, you will have your kanguage file with this:

$lang[‘price’]      = ‘%6.2f €’;

Herem the language_helper.php file (must to be create with this name and in the helper appliation/directory):

<?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        ExpressionEngine Dev Team
* @copyright    Copyright (c) 2008 – 2010, EllisLab, Inc.
* @license        http://codeigniter.com/user_guide/license.html
* @link        http://codeigniter.com
* @since        Version 1.0
* @filesource
*/

// ————————————————————————

/**
* CodeIgniter Language Helpers
*
* @package        CodeIgniter
* @subpackage    Helpers
* @category    Helpers
* @author        ExpressionEngine Dev Team
* @link        http://codeigniter.com/user_guide/helpers/language_helper.html
*/

// ————————————————————————

/**
* Lang
*
* Fetches a language variable and optionally outputs a form label
*
* @access    public
* @param    string    the language line
* @param    string    the id of the form element
* @return    string
*/
if ( ! function_exists(‘lang’))
{
function lang($line, $id = ”)
{
$CI =& get_instance();
$line = $CI->lang->line($line);

if ($id != ”)
{
$line = ‘<label for=”‘.$id.'”>’.$line.”</label>”;
}

return $line;
}
}

/**
* SLang
*
* Fetches a language variable, optional substitution vars and optionally outputs a form label
*
* @author    Xavier Pérez
* @version    1.0.1
* @access    public
* @param    string    the language line
* @param    mixed    the substitution values (varchar, integer, or array)
* @param    string    the id of the form element
* @return    string
*/

if ( ! function_exists(‘slang’))
{
function slang($line, $values=”, $id = ”)
{
$CI =& get_instance();
$line = $CI->lang->line($line);

if (is_array($values) && count($values) > 0)
$line = vsprintf($line,$values);

if (!is_array($values) && $values != “”)
$line = sprintf($line,$values);

if ($id != ”)
{
$line = ‘<label for=”‘.$id.'”>’.$line.”</label>”;
}

return $line;
}
}

// ————————————————————————
/* End of file language_helper.php */
/* Location: ./system/helpers/language_helper.php */

Leave a Reply

You must be logged in to post a comment.