Assigning a Gettext String to a Class

I am making a website that will be translated into x languages.

All lines must be localized.

There are cases when I need to display the name of the language, the name of the country or other information received from the database. The data considered in this way will rarely be changed - as above, I'm talking about the names of languages, countries, etc.

In this example, I use an array that stores the languages ​​into which the site's user interface has been translated. To allow name wrapping (used for header text when the "change language" flag / link hangs), I have an array like *:

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

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

I am currently using a global array:

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

Using:

global $global_langNames;

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

Output (locale = en_GB):

Chinese simplified

(locale = zh_CN):

简体 中文

( ) , , , PHP :

class constants_lang{

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

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

}

:

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

, , "" ?

* , , :

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

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__);
     }
}
+3
2

, () .

- - :

class A  {
    private $var;
    public function init() {
        $this->var = func();
    }
}
A::init();
+2

. , .

0

Source: https://habr.com/ru/post/1759994/


All Articles