在Smarty中将月名从其他语言更改为英语


Change month name from other language to english in Smarty

如何将月份名称从当前语言环境(波兰PL)更改为Smarty中的英语?

我有这个

 {$product->specificPrice.to|date_format:'%d %B %Y %H:%M:%S'}

得到

 17 maj 2015 00:00:00

"maj"在波兰语中的意思是五月,我想有这样的标记:

 17 May 2015 00:00:00

您可能需要在php代码中设置时间/日期格式的区域设置:

    setlocale(LC_TIME, en_US.utf8);

如果你想在你的模板中只在少数地方用英语输出日期,并保持你的时间/日期区域设置的波兰,你应该编写自定义Smarty修饰符,并使用它来以自定义格式输出日期。

不是最好的,但简单的方法是在自定义修饰符中重用smarty的date_format,就像下面的例子所示(考虑到smarty 3):

    class Smarty_Extended extends Smarty
    {
        private $_locale;
        public function __construct($defaultLocale)
        {
            parent::__construct();
            $this->_locale = $defaultLocale;
            $this->loadPlugin('smarty_modifier_date_format');
            $this->registerPlugin('modifier', 'date_format_eng', [$this, 'smarty_modifier_date_format_eng']);
        }
        public function smarty_modifier_date_format_eng($string, $format = null, $default_date = '', $formatter = 'auto')
        {
            setlocale(LC_TIME, 'en_US.utf8');
            $date = smarty_modifier_date_format($string, $format, $default_date, $formatter);
            setlocale(LC_TIME, $this->_locale);
            return $date;
        }
    }

现在你可以在你的模板中使用date_format_eng:

{$time|date_format_eng}
May 22, 2015