symfonyのadmin generatorをジャパナイズするための3ステップ

symfonyのadmin generatorは、これがないと開発しませんと言いたくなるほど便利で大好き。

日本語化もsymofny1.4ではi18nのmessage.xmlがデフォルトで付属しているので簡単だ。

しかし、いろいろ知らないと結構めんどくさいことになる

まずは基本中の基本であるi18nを有効化しデフォルト言語を日本語にする

settings.yml

all:
  .settings:
    i18n:                   true
    default_culture:        ja        # Default user culture

次に、symofnyの頻出語と使いがちな語句のmessage.txtを準備する

ボクのデフォルトは下記

/i18n/message.ja.xml

message.ja.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xliff PUBLIC "-//XLIFF//DTD XLIFF//EN" "http://www.oasis-open.org/committees/xliff/documents/xliff.dtd">
<xliff version="1.0">
  <file source-language="en" target-language="ja" datatype="plaintext" original="messages" product-name="messages" date="2010-01-12T15:23:11Z">
    <header/>
    <body>
      <!-- copy from sf_admin.ja.xml -->
      <trans-unit>
        <source>is empty</source>
        <target>空の値も含む</target>
      </trans-unit>
      <trans-unit>
        <source>yes or no</source>
        <target>全て</target>
      </trans-unit>
      <trans-unit>
        <source>yes</source>
        <target>はい</target>
      </trans-unit>
      <trans-unit>
        <source>no</source>
        <target>いいえ</target>
      </trans-unit>
      <trans-unit>
        <source><![CDATA[from %from_date% to %to_date%]]></source>
        <target><![CDATA[%from_date% から %to_date%]]></target>
      </trans-unit>
      <trans-unit>
        <source><![CDATA[from %from_date%<br />to %to_date%]]></source>
        <target><![CDATA[%from_date% から<br /> %to_date%]]></target>
      </trans-unit>

      <!-- symfony common field names -->
      <trans-unit>
        <source>Updated at</source>
        <target>更新時間</target>
      </trans-unit>
      <trans-unit>
        <source>Created at</source>
        <target>作成時間</target>
      </trans-unit>

      <!-- general field names -->
      <trans-unit>
        <source>Group</source>
        <target>グループ</target>
      </trans-unit>
      <trans-unit>
        <source>Category</source>
        <target>カテゴリ</target>
      </trans-unit>
      <trans-unit>
        <source>Id</source>
        <target>ID</target>
      </trans-unit>
      <trans-unit>
        <source>Name</source>
        <target>名前</target>
      </trans-unit>
      <trans-unit>
        <source>Title</source>
        <target>タイトル</target>
      </trans-unit>
      <trans-unit>
        <source>Description</source>
        <target>説明</target>
      </trans-unit>
      <trans-unit>
        <source>File name</source>
        <target>ファイル名</target>
      </trans-unit>
      <trans-unit>
        <source>Url</source>
        <target>URL</target>
      </trans-unit>

      <trans-unit>
        <source>Is valid</source>
        <target>有効</target>
      </trans-unit>
      <trans-unit>
        <source>Is deleted</source>
        <target>論理削除</target>
      </trans-unit>
      <trans-unit>
        <source>Open time</source>
        <target>公開時間</target>
      </trans-unit>
      <trans-unit>
        <source>Close time</source>
        <target>公開終了</target>
      </trans-unit>
      <trans-unit>
        <source>Note</source>
        <target>メモ</target>
      </trans-unit>
      <trans-unit>
        <source>Valid</source>
        <target>有効</target>
      </trans-unit>
      <trans-unit>
        <source>Inalid</source>
        <target>無効</target>
      </trans-unit>
    </body>
  </file>
</xliff>


ついでにお気に入りのsfAdminDashPlugin用

sf_admin_dash.ja.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xliff PUBLIC "-//XLIFF//DTD XLIFF//EN" "http://www.oasis-open.org/committees/xliff/documents/xliff.dtd">
<xliff version="1.0">
  <file source-language="en" target-language="ja" datatype="plaintext" original="messages" product-name="messages">
    <header/>
    <body>
      <trans-unit id="1">
        <source>Welcome to</source>
        <target />
      </trans-unit>
      <trans-unit id="2">
        <source>Use a valid username and password to gain access to the administration console.</source>
        <target>有効なアカウントとパスワードを入力して管理画面にログインしてください</target>
      </trans-unit>
      <trans-unit id="3">
        <source>Username</source>
        <target>アカウント</target>
      </trans-unit>
      <trans-unit id="4">
        <source>Remember?</source>
        <target>次回から自動的にログイン</target>
      </trans-unit>
      <trans-unit>
        <source>Password</source>
        <target>パスワード</target>
      </trans-unit>
      <trans-unit>
        <source>Login</source>
        <target>ログイン</target>
      </trans-unit>
    </body>
  </file>
</xliff>

ここまでだけで普通にDB設計していると6割方日本語になっているから不思議だ。


最後に、フォームで日付の入力がキモイのを整える

これを直す方法はきっともっといい方法があると思うんだけど、ココだけは力技で下記のようにしている

lib/form/BaseFormPropel.class.php

<?php
abstract class BaseFormPropel extends sfFormPropel
{
  public function setup()
  {
    foreach ($this->getWidgetSchema()->getFields() as $name => $widget)
    {
      switch (get_class($widget))
      {
        case 'sfWidgetFormDateTime':
          $widget->setOption('date', array('format' => '%year%年%month%月%day%日'));
          break;
        case 'sfWidgetFormDate':
          $widget->setOption('format', '%year%年%month%月%day%日');
          break;
      }
    }
  }
}


lib/filter/BaseFormFilterPropel.class.php

<?php
abstract class BaseFormFilterPropel extends sfFormFilterPropel
{
  public function setup()
  {
    foreach ($this->getWidgetSchema()->getFields() as $name => $widget)
    {
      switch (get_class($widget))
      {
        case 'sfWidgetFormFilterDate':
          $widget->getOption('from_date')->setOption('format', '%year%年%month%月%day%日');
          $widget->getOption('to_date')->setOption('format', '%year%年%month%月%day%日');
          break;
      }
    }
  }
}

doctrineの場合もBaseFormFilterDoctrineとBaseFormDoctrineを同様にいじればオーケー

あとはエラーメッセージ系で積み残しがあるが、取り敢えずはこれだけやれば十分に日本人でも安心な管理画面が鼻くそをほじっている間に出来上がる