将 Gettext 字符串分配给类成员

发布于 2024-09-14 14:14:50 字数 2257 浏览 3 评论 0原文

我正在制作一个将被翻译成 x 种语言的网站。

所有字符串都必须本地化。

有时我需要显示语言名称、国家/地区名称或从数据库检索的其他信息。以这种方式处理的数据很少会改变 - 如上所述,我谈论的是语言名称、国家/地区等。

在本例中,我使用的数组保存了网站 UI 已翻译成的语言。为了允许翻译名称(当“更改语言”标志/链接悬停时用于标题文本),我有一个像*这样的数组:

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

我使用它们来获取给定语言的相关名称字符串。

目前我正在使用全局数组:

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

用法:

global $global_langNames;

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

输出(区域设置= en_GB):

简体中文

输出(区域设置 = 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

低下头并返回到全局数组,还是在那里另一种更好的方法让我以这种方式使用“常量”数组?

*数组键来自存储语言代码的数据库表以及我们是否有 UI 翻译:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的呼吸 2024-09-21 14:14:50

是的,您只能在变量初始化中使用(大多数)文字。

解决方法是这样的:

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

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();
枯叶蝶 2024-09-21 14:14:50

您不能在成员声明中使用函数。如果您需要这样做,请将其移至构造函数或静态函数。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文