履歴: ソフトウェア/rails/言語ネゴシエーション
各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 21 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます
| 日時 | タイトル | 文字数 | 追加 | 削除 | 内容 | 差分 | 基準 |
|---|---|---|---|---|---|---|---|
| 2014/5/20 19:20:23 | ソフトウェア/rails/言語ネゴシエーション 最新版 | 3,891 | +1 | -1 | 表示 | 差分 | — |
| 2014/5/20 19:20:23 | ソフトウェア/rails/言語ネゴシエーション | 3,897 | +8 | -1 | 表示 | 差分 | 選択 |
| 2013/1/5 18:30:15 | ソフトウェア/rails/言語ネゴシエーション | 3,842 | +3 | -0 | 表示 | 差分 | 選択 |
| 2009/7/14 9:35:50 | ソフトウェア/rails/言語ネゴシエーション | 3,732 | +43 | -27 | 表示 | 差分 | 選択 |
| 2009/7/11 15:06:20 | ソフトウェア/rails/言語ネゴシエーション | 3,727 | +9 | -0 | 表示 | 差分 | 選択 |
| 2009/6/28 2:02:38 | ソフトウェア/rails/言語ネゴシエーション | 3,515 | +2 | -0 | 表示 | 差分 | 選択 |
| 2009/6/27 8:15:42 | ソフトウェア/rails/言語ネゴシエーション | 3,458 | +4 | -0 | 表示 | 差分 | 選択 |
| 2009/6/21 9:14:00 | ソフトウェア/rails/言語ネゴシエーション | 3,403 | +15 | -0 | 表示 | 差分 | 選択 |
| 2009/6/21 6:41:27 | ソフトウェア/rails/言語ネゴシエーション | 3,126 | +62 | -2793 | 表示 | 差分 | 選択 |
| 2009/6/10 11:14:13 | ソフトウェア/rails/言語ネゴシエーション | 84,051 | +327 | -1 | 表示 | 差分 | 選択 |
| 2009/6/10 3:14:21 | ソフトウェア/rails/言語ネゴシエーション | 73,730 | +459 | -3 | 表示 | 差分 | 選択 |
| 2009/6/9 15:42:54 | ソフトウェア/rails/言語ネゴシエーション | 58,379 | +247 | -473 | 表示 | 差分 | 選択 |
| 2009/6/9 11:29:37 | ソフトウェア/rails/言語ネゴシエーション | 66,074 | +317 | -40 | 表示 | 差分 | 選択 |
| 2009/6/8 20:22:16 | ソフトウェア/rails/言語ネゴシエーション | 58,607 | +75 | -2 | 表示 | 差分 | 選択 |
| 2009/6/8 15:18:39 | ソフトウェア/rails/言語ネゴシエーション | 55,917 | +958 | -223 | 表示 | 差分 | 選択 |
| 2009/6/8 8:00:44 | ソフトウェア/rails/言語ネゴシエーション | 33,858 | +247 | -35 | 表示 | 表示中 | 選択 |
| 2009/6/7 13:55:06 | ソフトウェア/rails/言語ネゴシエーション | 28,314 | +195 | -18 | 表示 | 差分 | 基準 |
| 2009/6/7 4:15:30 | ソフトウェア/rails/言語ネゴシエーション | 23,332 | +140 | -3 | 表示 | 差分 | 選択 |
| 2009/6/6 14:50:13 | ソフトウェア/rails/言語ネゴシエーション | 19,622 | +30 | -27 | 表示 | 差分 | 選択 |
| 2009/6/6 6:50:47 | ソフトウェア/rails/言語ネゴシエーション | 19,701 | +581 | -3 | 表示 | 差分 | 選択 |
| 2009/6/6 2:59:56 | ソフトウェア/rails/言語ネゴシエーション | 2,869 | — | — | 表示 | — | 選択 |
基準: ソフトウェア/rails/言語ネゴシエーション (2009/6/7 13:55:06) → 比較: ソフトウェア/rails/言語ネゴシエーション (2009/6/8 8:00:44)
@@ -9,12 +9,33 @@
を見ながら、ブラウザの Language Negotiation の機能を使って
rails アプリケーションの多言語化する方法を調べてみた。
+rails のバージョンが上がっているせいと、それ以外の問題とで、結構苦労したのでメモ。
+
やりたい内容としては
-- views/*/*.html.ja.erb と views/*/*.html.en.erb とを両方作っておき、
-自動的に切り替えて読み込む
-- キャッシュされたページも apache のネゴシエーション機能を使って
++ views/*/*.html.ja.erb と views/*/*.html.en.erb とを両方作っておき、
+閲覧者の環境設定、手動選択に合わせて自動的に切り替えて送信する
++ キャッシュされたページも apache のネゴシエーション機能を使って
自動で切り替わるようにする
+** ブラウザ設定における表示言語の優先順位 [#w3484f9d]
+
+1. は、ブラウザの言語設定で日本語と英語のどちらを優先しているかを判別して、
+優先順位の高い方で表示するという話。
+
+ブラウザはこの言語設定を元に、Accept-Language: というヘッダ情報をサーバーに
+送るので、rails 側でこのヘッダ情報から表示言語を選べばよい。
+
+** キャッシュに対する apache2 の言語ネゴシエーション [#w6b05fcf]
+
+2. について。
+
+rails のページキャッシュ機能は、表示内容を .html ファイルとして保存する事で、
+次回からは rails を通さず、apache で直接処理させるというものなので、
+適切な名前でキャッシュファイルを作成する事と、apache2 にキャッシュファイルを
+正しく認識させる事が必要になる。
+
+** その他の多言語化情報 [#f2bd1cf6]
+
rails アプリの多言語化については他にも、
-- ネゴシエーションっぽいもの
@@ -29,12 +50,14 @@
-- [[RubyOnRails を使ってみる 【第 5 回】 ActiveHeart>http://jp.rubyist.net/magazine/?0012-RubyOnRails]]
-- /usr/share/rails/actionpack/lib/action_view/locale/xx.yml とか
-などがあるみたい。
+などがあるようだけれど、よく調べていない。
-以下手順。
+以下は試行錯誤の経過とその成果物
* negotiation という名前のアプリケーションの作成 [#vb4e8dd6]
+テスト用に negotiation という名前のアプリケーションを作成。
+
LANG:console
$ rails negotiation
$ cd negotiation
@@ -51,6 +74,8 @@
**** About_your_application’s_environment ****
...
+ $ $ ls db/*
+ ls: cannot access db/*: そのようなファイルやディレクトリはありません
$ wget -qO- http://localhost:3000/rails/info/properties | html2text
Ruby version 1.8.7 (i486-linux)
RubyGems version 1.3.2
@@ -65,7 +90,11 @@
Environment development
Database adapter sqlite3
Database schema version 0
+ $ ls db/*
+ db/development.sqlite3
+このとき自動的に sqlite3 のデータベースが作成される。
+
* test コントローラとビューの作成 [#k0a5e25a]
LANG:console
@@ -77,12 +106,8 @@
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
+ $ (restart_server)
$ wget -qO- http://localhost:3000/test
Test
@@ -97,26 +122,27 @@
$ 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
+ エラー 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~
+ 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 などの言語指定がない場合には、ブラウザが指定する言語で表示する
- .ja などの言語指定があれば、その言語で表示する
- 一旦言語指定を行ったら、以降は言語指定がない場合にも直前に行った言語指定と
同じ言語で表示する
@@ -213,7 +239,7 @@
+ 指定された言語 (指定されていれば)
+ 前回指定された言語 (もしあれば session に保存しておく)
-+ ブラウザから送られる HTTP_ACCEPT_LANGUAGE (もしあれば)
++ ブラウザから送られる Accept-Language (もしあれば)
+ ActionController::AbstractRequest.acceptable_languages に記述された言語(記述された順)
この順で提供可能な言語(作成されている言語テンプレート)を検索して、
@@ -246,7 +272,7 @@
end
- class ActionController::Base
+ class ApplicationController
before_filter :set_language
def set_language
session[:rails_language]= params[:rails_language] if params[:rails_language] &&
@@ -266,9 +292,9 @@
end
$ wget -qO- http://localhost:3000/test
[:ja, :en]
- $ wget -qO- --header="ACCEPT_LANGUAGE:en" http://localhost:3000/test
+ $ wget -qO- --header="Accept-Language:en" http://localhost:3000/test
[:en, :ja]
- $ wget -qO- --header="ACCEPT_LANGUAGE:ja" http://localhost:3000/test
+ $ wget -qO- --header="Accept-Language:ja" http://localhost:3000/test
[:ja, :en]
$ wget -qO- http://localhost:3000/test
[:ja, :en]
@@ -470,7 +496,7 @@
$ rm tmp/wget.cookie
$ wgetc -qO- http://localhost:3000/test
テスト
- $ wgetc --header="ACCEPT_LANGUAGE:en" http://localhost:3000/test
+ $ wgetc --header="Accept-Language:en" http://localhost:3000/test
Test
$ wgetc -qO- http://localhost:3000/test
テスト
@@ -478,7 +504,7 @@
Test
$ wgetc http://localhost:3000/test
Test
- $ wgetc --header="ACCEPT_LANGUAGE:ja" http://localhost:3000/test
+ $ wgetc --header="Accept-Language:ja" http://localhost:3000/test
Test
** レイアウトに関するテスト [#j36ad09b]
@@ -656,7 +682,7 @@
LANG:console
$ jed config/environments/development.rb
config.action_controller.perform_caching = true
- $ jed app/controller/test_controller.rb
+ $ jed app/controllers/test_controller.rb
class TestController < ApplicationController
caches_page :index
def index
@@ -783,12 +809,198 @@
public/test/index:
0.html.ja
+* ページキャッシュの多言語化に対応させるための apache の設定 [#t2a0cd95]
+
+まずは rails を apache から cgi 経由で production 環境で動かす。
+
+ LANG:console
+ $ cp db/development.sqlite3 db/production.sqlite3
+ $ ln -s public/ /var/www/negotiation
+ $ wgetc http://localhost/negotiation | html2text
+ ...
+
+ ****** Welcome aboard ******
+ ***** You’re riding Ruby on Rails! *****
+ **** About_your_application’s_environment ****
+ ...
+
+ $ cat > public/.htaccess
+ SetEnv RAILS_RELATIVE_URL_ROOT /negotiation
+ SetEnv RAILS_ENV production
+
+ RewriteEngine On
+ RewriteRule ^$ index.html [QSA]
+ RewriteRule ^([^.]+)$ $1.html [QSA]
+ RewriteCond %{REQUEST_FILENAME} !-f
+ RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
+ ^D
+ $ echo "Test <%= render :partial=>'ok' %>" > app/views/test/index.html.en.erb
+ $ echo "テスト <%= render :partial=>'ok' %>" > app/views/test/index.html.ja.erb
+ $ wgetc http://localhost/negotiation/test
+ 日本語: テスト ばっちり
+
+ $ wgetc http://localhost/negotiation/test.en
+ English: Test OK
+
+ $ wgetc http://localhost/negotiation/test
+ English: Test OK
+
+
+では、ページキャッシュを ON にしてテスト。
+
+ LANG:console
+ $ jed app/controllers/test_controller.rb
+ class TestController < ApplicationController
+ caches_page :index
+ # caches_action :index
+ def index
+ end
+ end
+ $ ls public/test*
+ ls: cannot access public/test*: そのようなファイルやディレクトリはありません
+ $ wgetc http://localhost/negotiation/test.ja
+ 日本語: テスト ばっちり
+
+ $ ls public/test*
+ public/test.html.ja
+ $ wgetc http://localhost/negotiation/test
+ 日本語: テスト ばっちり
+
+ $ tail /var/log/apache2/rewrite.log
+ [perdir /home/samba/www/negotiation/] pass through /home/samba/www/negotiation/test.html.ja
+ $ wgetc http://localhost/negotiation/test.ja
+ 日本語: テスト ばっちり
+
+ $ tail /var/log/apache2/rewrite.log
+ [perdir /home/samba/www/negotiation/] pass through /home/samba/www/negotiation/dispatch.cgi
+ $ rm temp/wget.cookie
+ $ wgetc --header="Accept-Language: en" http://localhost/negotiation/test
+ エラー 406: Not Acceptable
+ $ wgetc http://localhost/negotiation/test.en
+ English: Test OK
+
+ $ ls public/test*
+ public/test.html.en public/test.html.ja
+ $ wgetc http://localhost/negotiation/test
+ 日本語: テスト ばっちり
+
+ $ tail /var/log/apache2/rewrite.log
+ [perdir /home/samba/www/negotiation/] pass through /home/samba/www/negotiation/test.html.ja
+ $ rm temp/wget.cookie
+ $ wgetc --header="Accept-Language: en" http://localhost/negotiation/test
+ English: Test OK
+
+ $ wgetc --header="Accept-Language: ja" http://localhost/negotiation/test
+ 日本語: テスト ばっちり
+
+
+まず、public/test.html.ja が作成された後も http://localhost/negotiation/test.ja
+の読み込みがキャッシュファイルではなく cgi 呼び出しに rewrite されている点。
+
+これは、
+ LANG:Shell(linenumber)
+ RewriteCond %{HTTP_ACCEPT} text/html
+ RewriteCond %{SCRIPT_FILENAME} !\.html\.
+ RewriteCond %{SCRIPT_FILENAME} (.*)\.(..)$
+ RewriteCond %1.html.%2 -f
+ RewriteRule (.*)\.(..)$ $1.html.$2 [L]
+
+というルールを追加してやる事でキャッシュを読ませる事ができる。
+
+ルールの意味としては、
+
++ ACCEPT ヘッダに text/html が含まれていて
++ ファイル名が .html. を含まず
++ ファイル名が .ja や .en のように、2文字の拡張子で終わっていて
++ .html を追加すると、同名のファイルが見つかる
++ という条件の下、.html を追加して、test.en を test.html.en に書き換える
+
+次に、public/test.html.ja があって public/test.html.en が無い
+状況で wgetc --header="Accept-Language: en" http://localhost/negotiation/test すると
+406 エラーになる。
+
+これは、要求された en のファイルが無いためだ。
+
+本来であればこれを dispatch.cgi に rewrite したいところだが、
+その方法が分からなかったので、
+
+ LANG:Shell
+ LanguagePriority ja en
+ ForceLanguagePriority Prefer Fallback
+
+としてやることで、望みの言語が見つからない場合には ja, en の順に探して、
+見つかった物を返すようにした。
+
+これだと英語を要求したのに日本語の答えが返って来る、
+という意味で完全な動作とは言えないが、
+apache のネゴシエーションのルールからするとこれは仕方のないところか。
+
+最後の部分で確かめたように、
+public/test.html.en と public/test.html.ja とが両方あれば
+Accept-Language が効くので、そのページを表示した最初の一人だけが
+被害を被るという事で、あきらめる。
+
+もう一点、wgetc http://localhost/negotiation/test.en の後の
+wgetc http://localhost/negotiation/test 呼び出しで
+日本語が表示されてしまっている部分に不具合がある。
+
+これは、直前に選択した言語を apache が覚えていないせいだ(あたりまえ)。
+
+ SetEnvIf Cookie "rails_language=(..)" prefer-language=$1
+
+とすることで、cookie の値に応じて言語を選ぶことができる。
+
+さらに、
+ RewriteCond %{ENV:prefer-language} !=$2
+ RewriteRule (.*\.html\.)(..) $1%{ENV:prefer-language}
+
+とする。
+
+これは、test.html.ja が存在して test.html.en が存在しない時、
+cookies["rails_language"]=="en" であっても test の呼び出しが
+apache のネゴシエーションで test.html.ja になってしまうので、
+これを検出して test.html.en に書き直すためのもの。
+
+当然 test.html.en は存在しないので、結果的に dispatch.cgi に
+rewrite されて test.html.en が作られる事になる。
+
+最終的に、public/.htaccess の内容は
+
+ SetEnv RAILS_RELATIVE_URL_ROOT /negotiation
+ SetEnv RAILS_ENV production
+
+ LanguagePriority ja en
+ ForceLanguagePriority Prefer Fallback
+
+ SetEnvIf Cookie "rails_language=(..)" prefer-language=$1
+
+ RewriteEngine On
+
+ RewriteRule ^$ index.html [QSA]
+ RewriteRule ^([^.]+)$ $1.html [QSA]
+
+ RewriteCond %{HTTP_ACCEPT} text/html
+ RewriteCond %{SCRIPT_FILENAME} !\.html\.
+ RewriteCond %{SCRIPT_FILENAME} (.*)\.(..)
+ RewriteCond %1.html.%2 -f
+ RewriteRule (.*)\.(..) $1.html.$2 [L]
+
+ RewriteCond %{ENV:prefer-language} !=$2
+ RewriteRule (.*\.html\.)(..) $1%{ENV:prefer-language}
+
+ RewriteCond %{SCRIPT_FILENAME} !-f
+ RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
+
+となった。
+
+** テスト [#f34fa374]
+
* アクションキャッシュ [#gd9d14bc]
まずは rails そのままで、アクションキャッシュ機能をONにしてみる。
LANG:console
- $ jed app/controller/test_controller.rb
+ $ jed app/controllers/test_controller.rb
class TestController < ApplicationController
# caches_page :index
caches_action :index
@@ -937,8 +1149,8 @@
まずは例によって rails そのままで、フラグメントキャッシュ機能を使ってみる。
LANG:console
- $ echo "<% cache do %>Test <%= render :partial=>'ok' %><% end %>" > app/views/test/index.html.en
- $ echo "<% cache do %>テスト <%= render :partial=>'ok' %><% end %>" > app/views/test/index.html.ja
+ $ echo "<% cache do %>Test <%= render :partial=>'ok' %><% end %>" > app/views/test/index.html.en.erb
+ $ echo "<% cache do %>テスト <%= render :partial=>'ok' %><% end %>" > app/views/test/index.html.ja.erb
$ jed app/controllers/test_controller.rb
class TestController < ApplicationController
# caches_page :index
@@ -978,18 +1190,18 @@
** テスト [#le18b1e9]
LANG:console
- $ wgetc -qO- http://localhost:3000/test
+ $ wgetc http://localhost:3000/test
English: Test OK
$ less log/development.log
Cached fragment miss: views/localhost:3000/test.en (0.1ms)
- $ wgetc -qO- http://localhost:3000/test
+ $ wgetc http://localhost:3000/test
English: Test OK
$ less log/development.log
Cached fragment hit: views/localhost:3000/test.en (0.1ms)
- $ echo "<% cache :param1=>:test do %>Test <%= render :partial=>'ok' %><% end %>" > app/views/test/index.html.en
- $ wgetc -qO- http://localhost:3000/test
+ $ echo "<% cache :param1=>:test do %>Test <%= render :partial=>'ok' %><% end %>" > app/views/test/index.html.en.erb
+ $ wgetc http://localhost:3000/test
English: Test OK
$ less log/development.log