Home > Zend_Form > Use of Zend_Form(i18n)

Use of Zend_Form(i18n)

intro
Let's create Japanese view using Zend_Form.You can download the source code of this article from here.

1.Create Controller
Create the file application/constorllers/IndexController.php with the following content.Please modify the path of csv files in the function preDispath.

<?php
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Form.php';
require_once 'Zend/Translate.php';
require_once 'Zend/Locale.php';
require_once 'Zend/Registry.php';
 
class IndexController extends Zend_Controller_Action
{
    public function preDispatch()
    {
        $locale = new Zend_Locale();
        $translate = new Zend_Translate('csv', '../application/msg_en.csv', 'en');
        $translate->addTranslation('../application/msg_ja.csv', 'ja');
        if (! $translate->isAvailable($locale->getLanguage()))
        {
            $translate->setLocale('en');
        }
        else
        {
            $translate->setLocale('auto');
        }
        Zend_Registry::set('Zend_Translate', $translate);
    }
 
    /**
     * This function returns the form used for
     * adding comments in blog application
     */
    public static function getAddCommentForm()
    {
        $form = new Zend_Form(array(
            'method'   => 'post',
            'elements' => array(
                'comments' => array('textarea', array(
                    'required' => true,
                    'label' => 'Write your comments'
                )),
                'submit' => array('submit', array(
                    'label' => 'Send'
                ))
            ),
        ));
 
        return $form;
    }
 
    public function indexAction()
    {
        $form = $this->getAddCommentForm();
 
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {
                $values = $form->getValues();
                $this->view->values = $values;
            }
        }
 
        $this->view->form = $form;
    }
}
2.Create View
Create the file application/views/scripts/index/index.phtml as following content.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Zend Framework Quick Start</title>
  </head>
  <body>
<h1 id="6__1" ><?= $this->translate('Basic Blog'); ?></h1>
 
    <?php if($this->values) : ?>
<h3 id="6__2" >
      <?= $this->translate('You just submitted the following values'); ?>:
    </h3>
<ul>
      <?php foreach ($this->values as $value) :?>
<li>
        <?= $this->escape($value); ?>
      </li>
 
      <?php endforeach; ?>
    </ul>
 
    <?php endif; ?>
    <?= $this->form; ?>
  </body>
</html>
3.Create Message Files
Create the file application/msg_en.csv as following content.

Basic Blog;Basic Blog
You just submitted the following values;You just submitted the following values
Write your comments;Write your comments
Send;Send
isEmpty;Value is empty, but a non-empty value is required

Create the file application/msg_ja.csv as following content.
(*Need to save it as UTF-8 encoding*)

Basic Blog;基本のブログ
You just submitted the following values;以下のデータを送信しました
Write your comments;コメントをどうぞ
Send;送信
isEmpty;コメントを入力してください

4.Check
Let's access your web server(ex.http://localhost/).
If it goes well, the messages change according to your browser language preferences(Including error messages!).
Screen of Japanese version:

Screen of English version:

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://www.oplabo.com/article/6/trackback
Listed below are links to weblogs that reference
Use of Zend_Form(i18n) from Open Programming Laboratory

Home > Zend_Form > Use of Zend_Form(i18n)

Japanese
Search
Feeds

Return to page top