Jump to content

language class


doddsey_65

Recommended Posts

i have a language class like so:

 

class languageClass
{
    public static $lang;
    
    public static function setLang($lang)
    {
        self::$lang = $lang;
    }
    
    public static function getLang()
    {
        include(dirname(dirname(__FILE__)).'/languages/'.self::$lang.'.php');
        
        return $lang;
    }
    
    public function loadFile($file)
    {
        $include = dirname(dirname(__FILE__)).'/languages/'.self::$lang.'/'.$file.'_lang.php';
        
        if(file_exists($include))
        {
            include $include;
        }
        else
        {
            echo 'File does not exist';
        }
    }
}

 

so i include a language file within index.php like this:

 

$lang->loadFile('common');

 

common_lang.php has an array like so:

 

$l = array(
     'HELLO' => 'hello',
);

 

but when i try to echo $l['HELLO'] in the index.php file it says variable $l is undefined. But the language script includes the file where it is defined. If i include the file within index.php then everything works however

 

Link to comment
Share on other sites

i thought so so i did this instead:

 

public function loadFile($file)
    {
        $include = dirname(dirname(__FILE__)).'/languages/'.self::$lang.'/'.$file.'_lang.php';
        
        if(file_exists($include))
        {
            include $include;
            
            //var_dump($l);
            
            return $l;
        }
        else
        {
            echo 'File does not exist';
            
            return false;
        }
    }

 

but still get the same error

 

Link to comment
Share on other sites

Hi for Everyone!

 

I am totally new at PHP programming... I think uve heard it a lot... :)

So my question:

I have got a nrmal HTML frameset with 3 frames.

One for logo ( has to be changed )

One for all contents ( has to be changed, consists 2 frames [menu and the readable content ])

One footer frame what should consists one link to change the site English and when its on English change it to Hungarian

 

Ive tried to make it but that wasnt succesfull. for me the basic things are missing and i have still syntax problems.

 

So where should i take the code about changing function? Is that function or session and what is the main logic it should works?

My idea: take the main function about changing to the main( index.php ) and call it from footer with a link ( or button )

and i think the variables should store at a config.php file.

 

But where to take the codes and how can someone write it to me? with a small explaination? I want to understand how does it work. I want to write it by myself after.

 

Thanks a lot for help: Peter

Link to comment
Share on other sites

@kisleki - You need to start your own thread for a new question.

 

@doddsey_65

When you include a file within a function, any variables defined in that file are defined within the scope of the function. So, the $l variable only exists within the scope of the LoadFile method. Outside of that method, it is undefined. If you return this variable, then you need to capture the return value:

 

$langMap = $lang->loadFile('common');

 

Although, in keeping with the encapsulation concept of OOP, I think I would assign that variable to a private property within the class, and define another method to translate. Something like this:

 

class languageClass
{
    public static $lang;

    private $_map;            // PROPERTY FOR LANGUAGE MAP
    
    public static function setLang($lang)
    {
        self::$lang = $lang;
    }
    
    public static function getLang()
    {
        include(dirname(dirname(__FILE__)).'/languages/'.self::$lang.'.php');
        
        return $lang;
    }
    
    public function loadFile($file)
    {
        $include = dirname(dirname(__FILE__)).'/languages/'.self::$lang.'/'.$file.'_lang.php';
        
        if(file_exists($include))
        {
            include $include;
            $this->_map = $l;            // STORE THE MAP IN A PRIVATE PROPERTY
            return $l;                   // OR RETURN THE MAP TO THE CALLER (OR BOTH)
        }
        else
        {
            echo 'File does not exist';
        }
    }

    // METHOD TO TRANSLATE A WORD
    public function translate($word) {
        return $this->_map[$word];
    }
}

 

Of course, I just copied your code and added a couple of things. I don't know why you are using STATIC for parts of it and not for others. You probably need to reconsider the design. Also, you'll need to add error checking (what if $word does not exist in the map?).

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.