CodeIgniter Forums
Load Language from within an own Library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Load Language from within an own Library (/showthread.php?tid=24143)



Load Language from within an own Library - El Forum - 11-01-2009

[eluser]codeworxx[/eluser]
Hey Guys,

i have created my own Language File and my own Class in the application/libaries Folder.

The Language is defined in the Configuration File and Autoloaded!

I have access to the File through the Views - there's no Problem, but when i try to access the Language File through my Libaries Class, i get an Error!!

How to fix that? In my own Library Class i have to define a Standard Message - and this one depends on the Language, so i need access to the File.

Hope everyone understands what i mean???

Greetings from Germany,

Sascha


Load Language from within an own Library - El Forum - 11-01-2009

[eluser]pistolPete[/eluser]
Please post the code of the library.
Which error do you get?


Load Language from within an own Library - El Forum - 11-02-2009

[eluser]codeworxx[/eluser]
Okay, that's the code of the Library:

Code:
class Displayerror
{
    
    function raiseError( $message = '' )
    {
        $return = '';
        if( empty( $message ) ) $message = $this->lang->line( 'error_undefined' );
        $return .= "\n".'<div class="error">'."\n";
        $return .= "\t".'<div class="message"><h1>Error</h1><span>'.$message.'</span></div>'."\n";
        $return .= '</div>'."\n";
        return $return;
    }
    
}

An this is the Error:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Displayerror::$lang

Filename: libraries/Displayerror.php

Line Number: 19
Fatal error: Call to a member function line() on a non-object in /srv/www/vhosts/mydomain.de/httpdocs/application/libraries/Displayerror.php on line 19

Line 19 is that line:
Code:
if( empty( $message ) ) $message = $this->lang->line( 'error_undefined' );

Thanks,
Sascha


Load Language from within an own Library - El Forum - 11-02-2009

[eluser]cahva[/eluser]
If thats the whole code, yoy forgot to use get_instance() so that you can use CI libraries. This can be found in the user guide

For example:
Code:
class Displayerror
{
    function Displayerror()
    {
        $this->CI =& get_instance();
    }
    
    function raiseError( $message = '' )
    {
        $return = '';
        if( empty( $message ) ) $message = $this->CI->lang->line( 'error_undefined' );
        $return .= "\n".'<div class="error">'."\n";
        $return .= "\t".'<div class="message"><h1>Error</h1><span>'.$message.'</span></div>'."\n";
        $return .= '</div>'."\n";
        return $return;
    }
    
}



Load Language from within an own Library - El Forum - 11-02-2009

[eluser]codeworxx[/eluser]
oh man... thank you so much...

Think it was toooo late yesterday. Have used get_instance 2-3 Times and just forgotten.

Thanks,
Sascha