- バックアップ一覧
- ソース を表示
- バックアップ を表示
- Ruby on Rails/第7回勉強会 - gettextを使ったvalidationエラーの日本語化 は削除されています。
- 追加された行はこの色です。
- 削除された行はこの色です。
&pgid;
** rails勉強会一覧 [#dd041670]
#ls2(rails)
** 第7回勉強会のネタ [#jeadb044]
ネタもとは下記です。
-Ruby on Railsで日本語しか使わない場合でもRuby-GetText-Packageを使う理由
http://www.yotabanana.com/lab/20060505.html
-Ruby-GetText-Packageとは?
http://www.yotabanana.com/hiki/ja/ruby-gettext.html
-GetTextのrdoc
http://www.yotabanana.com/gettextapi/
-Rails API ドキュメント ActionView::Helpers::ActiveRecordHelper
http://railsapi.masuidrive.jp/module/ActionView%3A%3AHelpers%3A%3AActiveRecordHelper
** 日時 [#g4f8ac7b]
- 2006/11/24(金)
** 参加者 [#u5cc1f6d]
-[[志田]](発表者)
-[[竹村]]さん
-[[進地]]さん
-[[小沼]]さん
-[[フラッツ久末さん:http://flatz.jp]]
久末さん、どうもありがとうございます!また参加してねーー。 &bigsmile;
#blikimore
** 動画 [#g3db69c1]
** gettextとは [#hff6572b]
- [[WikiPedia.ja:Gettext]]
- poファイル
#: app/controllers/application.rb:11
msgid "The errors are:"
msgstr "エラーがあります。"
** Ruby-GetTextとは? [#s344a8af]
- GetText::Rails
- gem
gem install gettext
**作業のながれ [#b2fda889]
-コーディングする
echo _("The errors are:")
- msgidはソースコードから自動的に抽出。
http://www.yotabanana.com/gettextapi/classes/GetText.html#M000045
#: app/controllers/application.rb:11
msgid "The errors are:"
msgstr ""
- msgstrを翻訳
#: app/controllers/application.rb:11
msgid "The errors are:"
msgstr ""
-moを生成
http://www.yotabanana.com/gettextapi/classes/GetText.html#M000049
- コーディングを続ける
- po再生成
updatepo
http://www.yotabanana.com/gettextapi/classes/GetText.html#M000050
- msgstrを翻訳してmoを生成
- コーディングを続ける...
** Ruby-GetTextのrailsへの組み込み [#n87bb105]
-GetText::Rails
http://www.yotabanana.com/gettextapi/classes/GetText/Rails.html
+config/environment.rbの先頭に
$KCODE = 'u' # UTF-8の場合。's' = SJIS, 'e' = EUC-JPなど
require 'jcode' # Stringクラスのメソッドなどを$KCODEに指定した
# 文字コードで適切に動作するように置き換えます
+config/environment.rbの末尾に
require 'gettext/rails' # Ruby-GetText-Packageを使う宣言です。
+app/controllers/application.rbに
class ApplicationController < ActionController::Base
init_gettext "sample"
end
http://www.yotabanana.com/gettextapi/classes/ActionController/Base.html#M000016
+
** msgidを指定しながらコーディング [#h267444f]
***コントローラ [#r5f7b8d1]
http://www.yotabanana.com/hiki/ja/ruby-gettext-howto-ror.html#Controller
-「_」または「n_」
***ビュー [#t39429f0]
http://www.yotabanana.com/hiki/ja/ruby-gettext-howto-ror.html#Views/ActionMailer
-「_」または「n_」
***モデル [#d4afe481]
http://www.yotabanana.com/hiki/ja/ruby-gettext-howto-ror.html#Models
- validates_xxx_of でのエラーメッセージ
--「N_」または「Nn_」
- validateでのエラーメッセージ
--「_」または「n_」
errors.add("title", _("%{fn} is not correct: %{title}") % {:title => title})
:%{fn}|フィールド名
:%{d}|長さチェックなどの境界値
:%{title}|タイトル名
***error_messages_forのタイトルの日本語化 [#p4c95ce4]
[[http://www.yotabanana.com/hiki/ja/ruby-gettext-howto-ror.html#%A5%A8%A5%E9...:http://www.yotabanana.com/hiki/ja/ruby-gettext-howto-ror.html#%A5%A8%A5%E9%A1%BC%A5%E1%A5%C3%A5%BB%A1%BC%A5%B8%A4%CE%A5%BF%A5%A4%A5%C8%A5%EB%C9%F4%CA%AC%A4%F2%A5%AB%A5%B9%A5%BF%A5%DE%A5%A4%A5%BA]]
ActionView::Helpers::ActiveRecordHelper::L10n.set_error_message_title(
N_("An error was found in %{record}"),
N_("%{num} errors were found in %{record}"))
ActionView::Helpers::ActiveRecordHelper::L10n.set_error_message_explanation(
N_("The error is:"),
N_("The errors are:"))
*** potファイルの生成 [#b3af82b6]
- lib/tasks/gettext.rakeにtaskを追加
desc "Update pot/po files."
task :updatepo do
require 'gettext/utils'
GetText.update_pofiles(
"blog",
#テキストドメイン名(init_gettextで使用した名前)
Dir.glob("{app,config,components,lib}/**/*.{rb,rhtml}"), #ターゲットとなるファイル
"blog 1.0.0" #アプリケーションのバージョン)
end
desc "Create mo-files"
task :makemo do
require 'gettext/utils'
GetText.create_mofiles(true, "po", "locale")
end
- rakeからupdatepoタスクを実行
$ rake updatepo
$ ls po/sample.pot
- 日本語用のpoファイルの生成
$ mkdir po/ja
$ cp po/sample.pot po/ja/sample.po
** moファイルの生成 [#wab7e3d1]
$ rake makemo
$ ls locale/ja/LC_MESSAGES/sample.mo
#blikifooter(志田)
tag: [[rails>tag/rails]], [[gettext>tag/gettext]], [[勉強会>tag/勉強会]]