CodeIgniter Forums
How can I use lang() function in helper class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How can I use lang() function in helper class (/showthread.php?tid=11220)

Pages: 1 2


How can I use lang() function in helper class - El Forum - 08-30-2008

[eluser]fuksito[/eluser]
Is it posible?
I made helper for building color-picker and I need to translate it dynamicly


How can I use lang() function in helper class - El Forum - 08-30-2008

[eluser]Colin Williams[/eluser]
Of course it's possible. It's just a function. Call it when and where you need to.


How can I use lang() function in helper class - El Forum - 08-31-2008

[eluser]xwero[/eluser]
It's better to use
Code:
$CI =& get_instance();
$CI->lang->line('some_line');
In your functions because the language helper isn't loaded by default.


How can I use lang() function in helper class - El Forum - 08-31-2008

[eluser]Colin Williams[/eluser]
Or just autoload the lang helper...


How can I use lang() function in helper class - El Forum - 09-01-2008

[eluser]xwero[/eluser]
[quote author="Colin Williams" date="1220238472"]Or just autoload the lang helper...[/quote]
You can do that but then you have to do it for each application you want to use the helper in. I think for helper it's saver to use language library which is loaded by default.


How can I use lang() function in helper class - El Forum - 09-01-2008

[eluser]Colin Williams[/eluser]
By that same token, xwero, he can load the helper with the Loader::helper() function. The benefit is that the syntax of lang() is much cleaner.


How can I use lang() function in helper class - El Forum - 09-01-2008

[eluser]xwero[/eluser]
True you have to type less but if he wants to contribute the helper then he has to put in the requirements load language helper. Which is restrictive IMO.


How can I use lang() function in helper class - El Forum - 09-01-2008

[eluser]Colin Williams[/eluser]
Not if he loads it in his helper.. I don't think you're following me.

Code:
$ci =& get_instance();
$ci->load->helper('language');

function color_picker()
{
  $title = lang('cp_title');
  $red = lang('cp_red');
}



How can I use lang() function in helper class - El Forum - 09-01-2008

[eluser]xwero[/eluser]
Ok you got a point there but what if people use a version where the helper isn't present. It's a recent addition, 1.6.1 i believe. This means you have to add the language helper too with his helper. I believe you should strive to create as less dependencies as possible.

The lang() function is nothing more than a wrapper for the lang->line method.


How can I use lang() function in helper class - El Forum - 09-01-2008

[eluser]Colin Williams[/eluser]
Is it not in your fabric to not find a problem with an alternative suggestion? Smile

If pre-language helper support is a goal of yours, fuksito, use the language class like xwero mentioned. Or, better yet, define your own language class wrapper:

Code:
function color_lang($line, $id = '')
{
    $CI =& get_instance();
    $line = $CI->lang->line($line);

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

    return $line;
}

function color_picker()
{
  $title = color_lang('title');
}