0

I have a class that translate language array base. So the problem is that the language does not change base on a cookie value.

this function should set the language value but it does not. it seems that no matter what i do i always get "ar" as a self::$currlang value. how can i correct this issue?

public function _set(){

    if( $_COOKIE['defaultLang'] != '' ) {
        self::$currlang = $_COOKIE['defaultLang'];
    } else {
        //this is the default language
        self::$currlang = 'ar';
    }

}

here is my code

thanks for your help :)

<?php
include('../langs/english.php');
include('../langs/arabic.php');

class Translator{
    private static $strs = array();
    private static $currlang = "";


    public function _set(){

        if( $_COOKIE['defaultLang'] != '' ) {
            self::$currlang = $_COOKIE['defaultLang'];
        } else {
            //this is the default language
            self::$currlang = 'ar';
        }

    }


    public static function loadTranslation($lang, $strs){
        if (empty(self::$strs[$lang]))
            self::$strs[$lang] = array();

        self::$strs[$lang] = array_merge(self::$strs[$lang], $strs);        
    }

    public static function setDefaultLang($lang){
        self::$currlang = $lang;        
    }

     public static function getDefaultLang(){
        return self::$currlang;        
    }

    public static function translate($key, $lang=""){
        if ($lang == ""){
            $lang = self::$currlang;
        }
        $str = self::$strs[$lang][$key];
        if (empty($str)){
            //$str = "$lang.$key"; 
            $str = 'Language "'. $lang . '", '. $key . ' is not defined.';           
        } 
        return $str;       
    }    

    public static function freeUnused(){
        foreach(self::$strs as $lang => $data){
            if ($lang != self::$currlang){
                $lstr = self::$strs[$lang]['langname'];
                self::$strs[$lang] = array();
                self::$strs[$lang]['langname'] = $lstr;                
            }            
        }        
    }

    public static function getLangList(){
        $list = array();
        foreach(self::$strs as $lang => $data){
            $h['name'] = $lang;
            $h['desc'] = self::$strs[$lang]['langname'];
            $h['current'] = $lang == self::$currlang;
            $list[] = $h;
        }
        return $list;        
    }

    public static function &getAllStrings($lang){
        return self::$strs[$lang];
    }

}

?>
4
  • why is the current language a STATIC attribute of your class?! what's the point of a translator class if they all share the same language? Commented Feb 24, 2013 at 2:09
  • I dont't see a problem in your code. Provide a usage sample and the value, the static variable has, once the code finished. Commented Feb 24, 2013 at 2:12
  • @mike Additionally, why do you implement a magic setter _set() - without evaluating the name of the variable to be set. php.net/manual/en/language.oop5.overloading.php#object.set Commented Feb 24, 2013 at 2:15
  • I have converted all self:: to $this-> and also removed static. But I now get a new error "Using $this when not in object context in this code line 2" public function loadTranslation($lang, $strs){ if ( empty($this->$strs[$lang]) ){ $this->$strs[$lang] = array(); } $this->$strs[$lang] = array_merge($this->$strs[$lang], $strs); } current code private $strs = array(); public $currlang; public function _set(){ if( $_COOKIE['defaultLang'] != '' ) { $this->$currlang = $_COOKIE['defaultLang']; } else { $this->$currlang = 'ar'; } } Commented Feb 24, 2013 at 18:29

1 Answer 1

1

The _set() magic method works with the -> operator, which works with instantiated objects. You can't use static member variables with instantiated objects, it's one or the other.

Sign up to request clarification or add additional context in comments.

5 Comments

So what should I change set to? How can I set those variables base on the cookies?
I would create a set( $varname, 'value' ) method that is static, that basically has the same internal code that you currently have =) Then you'd call it with $obj::set('name','value)
jhansen this would work when i change the language but i want the language to be automatically detected if the cookies is set. so i set the cookies of a drop down menu change and when ever the translator class is included then it should set the language base of the cookie value
Jhansen, I did what you said and I called that method where I included the class and it worked :). Thanks :)
Glad I could help, don't forget to up vote and select me as the solution! =D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.