views:

11

answers:

2

I'm making a site that will be translated into x languages.

All strings must be localized.

There are occasions when I need to display a language name, country name or other information that has been retrieved from a database. The data being dealt with in this way will seldom be changed - as above I'm talking about language names, countries etc.

In this example I'm using the array that holds the languages into which the site's UI has been translated. To allow translation of the names (used for title text when the "change language" flag / link is hovered), I have an array like*:

Array("zh_CN" => _("Chinese - Simplified"), "en_GB" => _("English"));

I use them to get the relevant name string for a given language.

Currently I'm using a global array:

$global_langNames = Array("zh_CN" => _("Chinese - Simplified"), "en_GB" => _("English"));

Usage:

global $global_langNames;

echo $global_langNames[$code]; // $code = 'zh_CN'

Output (locale = en_GB):

Chinese Simplified

Output (locale = zh_CN):

简体中文

I would much rather declare this (and other) constant arrays as private members of the class, but it seems PHP isn't willing:

class constants_lang{

 private static $langNames = Array("zh_CN" => _("Chinese - Simplified"), "en_GB" => _("English"));

 static function getLangName($code){
  return self::$langNames($code);
 }

}

Results in:

Parse error: syntax error, unexpected '(', expecting ')' in /site/http/includes/classes/constants/lang.php on line 20

Should I hang my head low and go back to the global array, or is there another, better way for me to have a 'constant' array to be used in this manner?

*The array keys are from the database table storing language codes and whether we have a UI translation:

code     ui translation
zh_CN       1
en_GB       1
zh_TW       0
      ....

Solution

class constants{

     private $langNamesFromCode;

     function __construct()
     {
          $this->langNamesFromCode = $this->initLangNamesFromCode();
     }

     /* INIT */

     private static function initLangNamesFromCode()
     {
          return Array("zh_CN" => _("Chinese - Simplified"), "en_GB" => _("English"));
     }

     /* GETTERS */

     public static function getLangNameFromCode($code)
     {
          if(self::isStatic()){
               $langNamesFromCode = self::initLangNamesFromCode();
               return $langNamesFromCode[$code];
          }
          else{
               return $this->langNamesFromCode[$code];
          }
     }

     /* UTILITY */

     private static function isStatic()
     {
          return !(isset($this) && get_class($this) == __CLASS__);
     }
}
+1  A: 

Yes, you can only use (most) literals in variable initializations.

The work-around is something like:

class A  {
    private $var;
    public function init() {
        $this->var = func();
    }
}
A::init();
Artefacto
Would doing this be preferable to using a global array?
Michael Robinson
@Mich I'd say yes.
Artefacto
Updated my question with solution taken from your answer. Thanks.
Michael Robinson
A: 

You cant use functions in member declarations. If you need to do this move it to the constructor or a static function.

prodigitalson