Categories

open all | close all

Tags

フォーム | RESTful | 認証 | 名称

Search

Archive for December 2007

フォームと言語の処理

フォームはRailsのビューの機能を使ってパーシャル・レンダリングで実装します。具体的にはcommentform-notloggedin.templateだったら,これをviewsの下のformsディレクトリに置き,「_commentform_notloggedin.rhtml」と,①最初に_を付ける,②ハイフンを_にする,③拡張子をrhtmlにするといったファイル名の変更をします。中身もビューの仕様に合わせて修正します。


<a id="nucleus_cf"></a>
<form method="post" action="#nucleus_cf">
  <div class="commentform">
<input type="hidden" name="action" value="addcomment" />
<input type="hidden" name="url" value="<%= formdata(:destinationurl) %>" />
<input type="hidden" name="itemid" value="<%= itemid %>" />
    <%= errordiv %>

    <label for="nucleus_cf_body"><%= text("_COMMENTFORM_COMMENT") %></label>
    <textarea name="body" class="formfield" cols="40" rows="10" id="nucleus_cf_body"><%= formdata(:body) %></textarea>

    <label for="nucleus_cf_name"><%= text("_COMMENTFORM_NAME") %></label>
    <input name="user" size="40" maxlength="40" value="<%= formdata(:user) %>" class="formfield" id="nucleus_cf_name" />

    <label for="nucleus_cf_mail"><%= text("_COMMENTFORM_MAIL") %></label>
    <input name="userid" size="40" maxlength="60" value="<%= formdata(:userid) %>" class="formfield" id="nucleus_cf_mail" />

    <label for="nucleus_cf_email"><%= text("_COMMENTFORM_EMAIL") %></label>
    <input name="email" size="40" maxlength="100" value="<%= formdata(:email) %>" class="formfield" id="nucleus_cf_email" />

    <%= callback(:FormExtra,:commentform_notloggedin) %>

    <input type="checkbox" value="1" name="remember" id="nucleus_cf_remember" <%= formdata(:rememberchecked) %> />
    <label for="nucleus_cf_remember"><%= text("_COMMENTFORM_REMEMBER") %></label>

    <input type="submit" alt="<%= text("_COMMENTFORM_SUBMIT") %>" value="<%= text("_COMMENTFORM_SUBMIT") %>" class="formbutton" />
  </div>
</form>

で,formdataやtextをアプリケーションのヘルパで実装します。ここではtextのところだけ示します。

  def text sym
    l(sym.to_sym)
  end

上の例ではtextの引数は文字列にしていますが,シンボルでも構いません。

次に多言語化の処理です。

Simple Localizationは,YAML形式の言語ファイルを作ることで多国語化を行うので,現在のNucleusのスタイルと非常によく似ています。さらにYAMLの階層構造を利用できるので,ネームスペースをあまり気にしなくていいというメリットがあります。今回の実装では,Nucleusの言語ファイルからなるべく簡単に移植できるように,次のようにしています。

libの下にlanguagesディレクトリを置き,そこに言語ファイルを置いていきます。今のところja.ymlだけが存在しています。中身はこんな感じ。

app:
  _ERROR_ITEMCLOSED: このアイテムは閉じました
  _COMMENTFORM_COMMENT: コメント
  _COMMENTFORM_NAME: お名前
  _COMMENTFORM_MAIL: ウェブサイト
  _COMMENTFORM_EMAIL: メール
  _COMMENTFORM_REMEMBER: 情報を記憶しておく
  _COMMENTFORM_SUBMIT: コメントを追加

本当は階層構造にしていった方がきれいですが,移植のしやすさを考えてフラットにしてしまいました。
そして,environment.rbに次のように書きます。

simple_localization :language => [:ja, :en]
ArkanisDevelopment::SimpleLocalization::Language.lang_file_dirs << "#{File.dirname(__FILE__)}/../lib/languages"

最初の行は言語ファイルの種類を示しています。1番目が優先的に使われます。2行目がこのアプリ用の言語ファイルを読ませるための設定です。これで,先ほどのYAMLを読み込んで表示するようになりました。

01 Dec, 2007 | General | | Andy | Leave comment - 0 -

© 2007 yoursite.com | Designed by DesignsByDarren
Ported to Nucleus CMS: Suvoroff