Zend_Form 占位符翻译

发布于 2024-12-19 11:55:01 字数 681 浏览 2 评论 0原文

我有一个带有 Zend_Form 的 Zend 应用程序,它应该使用 HTML5 placeholder 属性而不是标签,就像这里所做的那样

class Application_Form_Usereditprofile extends Zend_Form
{
     public function init()
     {
         [...]
         $this->addElement('text', 'FirstName', array(
            'filters'    => [...],
            'validators' => [...],
            'placeholder'=> 'user_editprofile_firstname', // string I want to translate
         ));
         [...]
     }
}

我初始化了 Zend_Translate,因此它应该默认翻译我的表单。这对于标签来说效果很好。但是,占位符将按原样使用,而不进行翻译。

如何翻译占位符字符串?

I have a Zend application with a Zend_Form, which should use the HTML5 placeholder attribute instead of labels, like done here.

class Application_Form_Usereditprofile extends Zend_Form
{
     public function init()
     {
         [...]
         $this->addElement('text', 'FirstName', array(
            'filters'    => [...],
            'validators' => [...],
            'placeholder'=> 'user_editprofile_firstname', // string I want to translate
         ));
         [...]
     }
}

I initialized Zend_Translate so it should translate my forms by default. This works fine with labels. However, the placeholder gets used as it is, without being translated.

How can I translate the placeholder strings?

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

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

发布评论

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

评论(3

我一直都在从未离去 2024-12-26 11:55:01

顺便说一句,您可以像这样访问翻译助手

'placeholder'=> $this->getView()->translate('user_editprofile_firstname),

plceholder 属性不能替代 label

来自规范

占位符属性不应用作标签的替代品。

You can access the the translate helper like this

'placeholder'=> $this->getView()->translate('user_editprofile_firstname),

btw. the plceholder attribute is not a substitution for the label.

From the spec:

The placeholder attribute should not be used as an alternative to a label.

小耗子 2024-12-26 11:55:01

这是我的最终解决方案。它翻译所有占位符。感谢乔纳的回答。

foreach($this->getElements() as $key => $element)
{
    $placeholder = $element->getAttrib('placeholder');
    if(isset($placeholder))
    {
        $this->$key->setAttrib('placeholder',$this->getView()->translate($placeholder));
    }
}

就是这样!

Here is my final solution. It translates all placeholders. Thanks to Jona for the answer.

foreach($this->getElements() as $key => $element)
{
    $placeholder = $element->getAttrib('placeholder');
    if(isset($placeholder))
    {
        $this->$key->setAttrib('placeholder',$this->getView()->translate($placeholder));
    }
}

That's it!

半﹌身腐败 2024-12-26 11:55:01

实际上我喜欢让事情自动化,所以我简单地创建了新的 My_Form 类来扩展 Zend_Form 并替换了 render 方法来处理事情:

public function render(Zend_View_Interface $view = null)
{
        /**
         * Getting elements.
         */
        $elements = $this->getElements();

        foreach ($elements as $eachElement) {

            /**
             * Auto placeholder translation
             */
            if($eachElement->getAttrib('placeholder') !== NULL && $eachElement->getTranslator() !== NULL ) {
                $placeholderText = $eachElement->getAttrib('placeholder');
                $textTrans =  $eachElement->getTranslator()->translate( $placeholderText);
                $eachElement->setAttrib('placeholder', $textTrans);
            }

        }

    /**
     * Rendering.
     */

    return parent::render($view);
}

Actually I like to have things automated, so I've simply made new My_Form class extending Zend_Form and replaced render method to handle things:

public function render(Zend_View_Interface $view = null)
{
        /**
         * Getting elements.
         */
        $elements = $this->getElements();

        foreach ($elements as $eachElement) {

            /**
             * Auto placeholder translation
             */
            if($eachElement->getAttrib('placeholder') !== NULL && $eachElement->getTranslator() !== NULL ) {
                $placeholderText = $eachElement->getAttrib('placeholder');
                $textTrans =  $eachElement->getTranslator()->translate( $placeholderText);
                $eachElement->setAttrib('placeholder', $textTrans);
            }

        }

    /**
     * Rendering.
     */

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