routesの設定と言語選択の優先順位 のバックアップ差分(No.1)
更新- 追加された行はこの色です。
- 削除された行はこの色です。
[[ソフトウェア/rails/言語ネゴシエーション]] #contents * 多言語化したビューの作成 [#b2db2fc3] 今作ったビューを削除して、日本語版と英語版を作成。 まだ rails に手を入れていないので、そのままではエラーになる。 LANG:console $ rm app/views/test/index.html.erb $ echo "Test" > app/views/test/index.html.en.erb $ echo "テスト" > app/views/test/index.html.ja.erb $ wget -O- http://localhost:3000/test エラー 404: Not Found これを正しく表示できるようにするのが目標なのだが、 その前に rails に言語ファイル選択の優先順位を教える必要がある。 * config/routes.rb の設定 [#w47da434] 多言語化後は、各ページに対して http://localhost:3000/test http://localhost:3000/test.ja http://localhost:3000/test/index http://localhost:3000/test/index.ja http://localhost:3000/test/index/0 http://localhost:3000/test/index/0.ja http://localhost:3000/test/index/0.html http://localhost:3000/test/index/0.html.ja などの形でアクセスできるようにする。 + .ja などの言語指定がない場合 -- ブラウザからも指定が無ければサーバー側の優先順位で表示言語を選ぶ -- ブラウザが優先順位を指定していれば、その順で表示言語を選ぶ + .ja などの言語指定があれば、その言語で表示する + 一旦言語指定を行ったら、以降は言語指定がない場合にサーバーやブラウザの優先順位を無視して、 直前に行った言語指定と同じ言語で表示する + 表示したい言語のテンプレートファイルが無ければ、ある物を使って表示する 3. は、明示的に表示言語を選択できるようにするためのもの。 受け入れ可能な言語のリストは config/routes.rb の先頭で ENV['RAILS_ACCEPTABLE_LANGUAGES'] ||= 'ja|en' として設定する事にする。 サーバー側の優先順位はここでの指定順で決まる物とする。~ これ以外の言語が指定されても無視することにする。 route の設定は、 map.connect ':controller/:action/:id.:rails_language', :requirements => { :rails_language => /ja|en/ } のようになる。/^(ja|en)$/ ではなく /ja|en/ とするのが正しいらしい。 以下がコード。 config/routes.rb LANG:ruby(linenumber) ENV['RAILS_ACCEPTABLE_LANGUAGES'] ||= 'ja|en' ActionController::Routing::Routes.draw do |map| lang_regexp= Regexp.new( ENV['RAILS_ACCEPTABLE_LANGUAGES'] ) map.connect ':controller.:rails_language', :format => 'html', :action => 'index', :requirements => { :rails_language => lang_regexp } map.connect ':controller/:action.:rails_language', :format => 'html', :requirements => { :rails_language => lang_regexp } map.connect ':controller/:action/:id.:rails_language', :format => 'html', :requirements => { :rails_language => lang_regexp } map.connect ':controller/:action/:id', :format => 'html' map.connect ':controller/:action/:id.:format.:rails_language', :requirements => { :rails_language => lang_regexp } map.connect ':controller/:action/:id.:format', :defaults => { :action => 'index', :format => 'html' } end ** テスト [#s18f0fb5] LANG:console $ (restart script/server) $ jed app/controllers/test_controller.rb class TestController < ApplicationController def index render :text => params[:rails_language].inspect + "\n" end end $ wget -qO- http://localhost:3000/test nil $ wget -qO- http://localhost:3000/test.ja "ja" $ wget -O- http://localhost:3000/test.html.ja 404: Not Found $ wget -qO- http://localhost:3000/test/index nil $ wget -qO- http://localhost:3000/test/index.ja "ja" $ wget -O- http://localhost:3000/test/index.html.ja 404: Not Found $ wget -qO- http://localhost:3000/test/index/0.html nil $ wget -qO- http://localhost:3000/test/index/0.html.ja "ja" 言語指定を正しく行えている事が分かる。 [[#### 前へ>ソフトウェア/rails/言語ネゴシエーション/negotiation という名前のアプリケーションの作成]]~ [[#### 上へ>ソフトウェア/rails/言語ネゴシエーション]]~ [[#### 次へ>ソフトウェア/rails/言語ネゴシエーション/言語が指定されていない時を含めた言語選択の優先順位]]~
Counter: 4658 (from 2010/06/03),
today: 1,
yesterday: 2