Welcome Guest, Not a member yet? Register   Sign In
Language extension
#1

[eluser]XMadMax[/eluser]
I have made some improvements to the language helper, to allow multiple substitutions.

Yo can have this on your english (dolar) language file:

Quote:$lang['price'] = '$ %6.0d';
$lang['is_lower_than'] = '%s is lower than %s';
$lang['must_to_be_as'] = '%s must to be as %s';

Yo can have this on your spanish (euro) language file:

Quote:$lang['price'] = '%6.2f €';
$lang['is_lower_than'] = '%s es más pequeño que %s';
$lang['must_to_be_as'] = '%s debe ser como %s';

To call slang function, you must to load your language file, load the language helper and call the slang function as:

Quote:$this->lang->load('pagination','english'); // Can be declared in your controller
$this->load->helper('language'); // Can be declared in any template

// inside your template
echo slang("price",$data["price"]);
echo slang("is_lower_than",array('1','2'));
echo slang("is_lower_than",array($data["val1"],$data["val2"]));


I have put a language_helper.php in my application/helper directory. The lang function still in the code to allow normal use of it, and added a new slang function.

Here the language_helper.php code:
Code:
<?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://ellislab.com/codeigniter/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://ellislab.com/codeigniter/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 Perez
* @version   1.0
* @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 ($id != '')
        {
            $line = '<label for="'.$id.'">'.$line."</label>";
        }
        
        if (is_array($values) && count($values) > 0)
            $line = vsprintf($line,$values);
            
        if (!is_array($values) && $values != "")
            $line = sprintf($line,$values);

        return $line;
    }
}

// ------------------------------------------------------------------------
/* End of file language_helper.php */
/* Location: ./application/helpers/language_helper.php */
#2

[eluser]XMadMax[/eluser]
Revision.

Forgot to include improvements to slang function when ID parameter present.


Code:
&lt;?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://ellislab.com/codeigniter/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://ellislab.com/codeigniter/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 */
#3

[eluser]takasia[/eluser]
Perfect! Thank you so much! :-D

Works nicely with CI 2.0.
#4

[eluser]InsiteFX[/eluser]
"Extending" Helpers
To "extend" Helpers, create a file in your application/helpers/ folder with an identical name to the existing Helper, but prefixed with MY_ (this item is configurable. See below.).

CodeIgniter Helper Functions

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB