言語ネゴシエーション のバックアップ(No.1)

更新


公開メモ

Rails で言語ネゴシエーション(Language Negotiation)

http://blog.omdb-beta.org/

を見ながら、ブラウザの Language Negotiation の機能を使って rails アプリケーションの多言語化する方法を調べてみた。

やりたい内容としては

  • views/*/*.html.ja.erb と views/*/*.html.en.erb とを両方作っておき、 自動的に切り替えて読み込む
  • appache のネゴシエーション機能を使ってキャッシュされたページも 自動で切り替わるようにする

rails アプリの多言語化については他にも、

  • ネゴシエーションっぽいもの

などがあるみたい。

以下手順。

negotiation という名前のアプリケーションの作成

LANG:console
$ rails negotiation
$ cd negotiation

開発用サーバーの起動と環境の確認

LANG:console
$ script/server &
$ wget -qO- http://localhost:3000/ | html2text
 ...
 
 ****** Welcome aboard ******
 ***** You’re riding Ruby on Rails! *****
 **** About_your_application’s_environment ****
 ...
 
$ wget -qO- http://localhost:3000/rails/info/properties | html2text
 Ruby version            1.8.7 (i486-linux)
 RubyGems version        1.3.2
 Rails version           2.2.2
 Active Record version   2.2.2
 Action Pack version     2.2.2
 Active Resource version 2.2.2
 Action Mailer version   2.2.2
 Active Support version  2.2.2
 Edge Rails revision     unknown
 Application root        /home/samba/www/rails/negotiation
 Environment             development
 Database adapter        sqlite3
 Database schema version 0

 

test コントローラとビューの作成

LANG:console
$ script/generate controller Test
     exists  app/controllers/
     exists  app/helpers/
     create  app/views/test
     exists  test/functional/
     create  app/controllers/test_controller.rb
     create  test/functional/test_controller_test.rb
     create  app/helpers/test_helper.rb
$ jed app/controllers/test_controller.rb
 class TestController < ApplicationController
   def index
   end
 end
$ echo "Test" > app/views/test/index.html.erb
$ wget -qO- http://localhost:3000/test
 Test

多言語化したビューの作成

今作ったビューを削除して、日本語版と英語版を作成。

まだ 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
 500 Internal Server Error

これを正しく表示できるようにするのが目標。


Counter: 9589 (from 2010/06/03), today: 2, yesterday: 0