履歴: ソフトウェア/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/6 14:50:13) → 比較: ソフトウェア/rails/言語ネゴシエーション (2009/6/7 13:55:06)
@@ -166,17 +166,18 @@
lang_regexp= ActionController::AbstractRequest.acceptable_languages.to_regexp
- map.connect ':controller/:action/:id.:rails_language',
+ map.connect ':controller.:rails_language', :format => 'html', :action => 'index',
:requirements => { :rails_language => lang_regexp }
- map.connect ':controller/:action.:rails_language',
+ map.connect ':controller/:action.:rails_language', :format => 'html',
:requirements => { :rails_language => lang_regexp }
- map.connect ':controller.:rails_language',
+ map.connect ':controller/:action/:id.:rails_language', :format => 'html',
:requirements => { :rails_language => lang_regexp }
- map.connect ':controller/:action/:id'
+ 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'
+ map.connect ':controller/:action/:id.:format',
+ :defaults => { :action => 'index', :format => 'html' }
end
@@ -650,8 +651,6 @@
* ページキャッシュの多言語対応 [#z6cc2a4b]
-キャッシュファイルは ActionController::Caching が作成する。
-
まずは rails そのままで、ページキャッシュ機能をONにしてみる。
LANG:console
@@ -681,4 +680,319 @@
public/test.html public/test.en
test.html というファイルができてしまうと、
-次回からは常にこれが表示されてしまうため、うまくいかない。
+次回からは言語設定にかかわらず常にこれが表示されてしまうため、うまくいかない。
+
+ページキャッシュは ActionController::Caching::Pages が作成する。~
+このときファイル名に言語指定を正しく付ければよい。
+
+ファイル名に付ける言語指定は、path として指定された物では無く、
+実際に使ったテンプレートの言語にすべき。
+
+ちなみに cache_page が暗黙に呼び出される時、引数は与えられないので、
+キャッシュファイル名は request.path から作る事になる。
+
+ LANG:ruby(linenumber)
+ module ActionController::Caching::Pages
+ def cache_page(content = nil, options = nil)
+ return unless perform_caching && caching_allowed
+
+ path = case options
+ when Hash
+ url_for(options.merge(
+ :only_path => true,
+ :skip_relative_url_root => true,
+ :format => params[:format],
+ :rails_language => request.language))
+ when String
+ options
+ else
+ p= request.path.split('.')
+ p.pop if ActionController::AbstractRequest.acceptable_language?(p.last)
+ p[0]+= self.class.page_cache_extension if p.count==1
+ p << request.language
+ p.join('.')
+ end
+
+ self.class.cache_page(content || response.body, path)
+ end
+ end
+
+** テスト [#t5c0d140]
+
+ LANG:console
+ $ rm -r public/test*
+ $ wgetc -qO- http://localhost:3000/test
+ English: Test OK
+
+ $ ls public/test* public/test*/*
+ public/test.html.en
+ $ wgetc -qO- http://localhost:3000/test.en
+ English: Test OK
+
+ $ ls public/test* public/test*/*
+ public/test.html.en
+ $ wgetc -qO- http://localhost:3000/test.ja
+ 日本語:テスト ばっちり
+
+ $ ls public/test* public/test*/*
+ public/test.html.en public/test.html.ja
+ $ wgetc -qO- http://localhost:3000/test
+ 日本語:テスト ばっちり
+
+ $ ls public/test* public/test*/*
+ public/test.html.en public/test.html.ja
+ $ wgetc -qO- http://localhost:3000/test/index
+ 日本語:テスト ばっちり
+
+ $ ls public/test* public/test*/*
+ public/test.html.en public/test.html.ja
+
+ public/test:
+ index.html.ja
+ $ wgetc -qO- http://localhost:3000/test/index/0
+ 日本語:テスト ばっちり
+
+ $ ls public/test* public/test*/*
+ public/test.html.en public/test.html.ja public/test/index.html.ja
+
+ public/test:
+ index index.html.ja
+
+ public/test/index:
+ 0.html.ja
+ $ wgetc -qO- http://localhost:3000/test/index/0.html
+ 日本語:テスト ばっちり
+
+ $ ls public/test* public/test*/*
+ public/test.html.en public/test.html.ja public/test/index.html.ja
+
+ public/test:
+ index index.html.ja
+
+ public/test/index:
+ 0.html.ja
+ $ wgetc -qO- http://localhost:3000/test/index/0.html.ja
+ 日本語:テスト ばっちり
+
+ $ ls public/test* public/test*/*
+ public/test.html.en public/test.html.ja public/test/index.html.ja
+
+ public/test:
+ index index.html.ja
+
+ public/test/index:
+ 0.html.ja
+
+* アクションキャッシュ [#gd9d14bc]
+
+まずは rails そのままで、アクションキャッシュ機能をONにしてみる。
+
+ LANG:console
+ $ jed app/controller/test_controller.rb
+ class TestController < ApplicationController
+ # caches_page :index
+ caches_action :index
+ def index
+ end
+ end
+ $ wgetc -qO- http://localhost:3000/test.en
+ English: Test OK
+
+
+ $ tail log/development.log
+ Cached fragment miss: views/localhost:3000/test (0.1ms)
+ $ wgetc -qO- http://localhost:3000/test
+ English: Test OK
+
+
+ $ wgetc -qO- http://localhost:3000/test.en
+ Cached fragment miss: views/localhost:3000/test?format=en (0.1ms)
+
+キャッシュ内容を表すキーが
+views/localhost:3000/test や views/localhost:3000/test?format=en などと
+なってしまっている。本来 views/localhost:3000/test.en であるべきだ。
+
+このあたりの処理は、ActionController::Caching::Actions::ActionCachePath
+で行われている。
+
+ LANG:ruby(linenumber)
+ module ActionController::Caching::Actions
+ class ActionCachePath
+ attr_reader :language
+
+ def initialize(controller, options = {}, infer_extension=true)
+ if infer_extension and options.is_a? Hash
+ request_extension = extract_extension(controller.request)
+ options = controller.params.merge(
+ options.reverse_merge(:format => request_extension,
+ :rails_language => controller.request.language_order.first))
+ end
+ path = controller.url_for(options).split('://').last
+ if infer_extension
+ @extension = request_extension
+ add_extension!(path, @extension)
+ end
+ @path = URI.unescape(path)
+ end
+
+ private
+
+ def add_extension!(path, extension)
+ if extension
+ p= path.split('.')
+ p.insert(-2, extension) unless path =~ /\b#{Regexp.escape(extension)}\b/
+ path.join('.')
+ end
+ end
+
+ def extract_extension(request)
+ p= request.path.split('.')
+ # drop file name
+ p.shift
+ # drop language
+ p.pop if !p.empty? && ActionController::AbstractRequest.acceptable_language?(p.last)
+ extension = p.join('.')
+
+ # If there's no extension in the path, check request.format
+ extension = request.cache_format if extension==""
+
+ extension
+ end
+ end
+ end
+
+8〜10行目の部分、元のコードでは controller.params とマージせずに
+うまく行っているのだけど、このコードでは controller.params とマージしないと
+action や id の指定が無視されるようだったので、対症療法として入れてある。
+
+後で要チェック
+
+request.language_order.first の言語テンプレートファイルが存在しない時、
+キャッシュキーには request.language_order.first で示される言語指定が
+付くにもかかわらず、中身は提供可能な言語で表示された内容になるなあ・・・
+まあ、問題は無いかな?
+
+** テスト [#n1f7d9d6]
+
+ LANG:console
+ $ wgetc -qO- http://localhost:3000/test
+ 日本語:テスト ばっちり
+
+ $ less log/development.log
+ Cached fragment miss: views/localhost:3000/test.ja (0.1ms)
+ $ wgetc -qO- http://localhost:3000/test
+ 日本語:テスト ばっちり
+
+ $ less log/development.log
+ Cached fragment hit: views/localhost:3000/test.ja (0.1ms)
+ $ wgetc -qO- http://localhost:3000/test.ja
+ 日本語:テスト ばっちり
+
+ $ less log/development.log
+ Cached fragment hit: views/localhost:3000/test.ja (0.1ms)
+ $ wgetc -qO- http://localhost:3000/test.en
+ English: Test OK
+
+ $ less log/development.log
+ Cached fragment miss: views/localhost:3000/test.en (0.1ms)
+ $ wgetc -qO- http://localhost:3000/test.en
+ English: Test OK
+
+ $ less log/development.log
+ Cached fragment hit: views/localhost:3000/test.en (0.1ms)
+ $ wgetc -qO- http://localhost:3000/test
+ English: Test OK
+
+ $ less log/development.log
+ Cached fragment hit: views/localhost:3000/test.en (0.1ms)
+ $ wgetc -qO- http://localhost:3000/test/index
+ English: Test OK
+
+ $ less log/development.log
+ Cached fragment hit: views/localhost:3000/test.en (0.1ms)
+ $ wgetc -qO- http://localhost:3000/test/index/0
+ English: Test OK
+
+ $ less log/development.log
+ Cached fragment miss: views/localhost:3000/test/index/0.en (0.1ms)
+ $ wgetc -qO- http://localhost:3000/test/index/0.html
+ English: Test OK
+
+ $ less log/development.log
+ Cached fragment hit: views/localhost:3000/test/index/0.en (0.1ms)
+
+http://localhost:3000/test/index.en へのアクセスに対して~
+views/localhost:3000/test.en が、~
+http://localhost:3000/test/index/0.html.en へのアクセスに対して~
+views/localhost:3000/test/index/0.en が、~
+それぞれ引かれていることに注目。
+
+url_for で変換し直している効果が現れている。
+
+routes の記述順によっては逆に冗長な記述への変換(/test を /test/index へ、など)
+が起こってしまう場合があるので、routes では省略形をより上の方に書くのが良いようだ。
+
+* フラグメントキャッシュ [#p06455c8]
+
+まずは例によって 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
+ $ jed app/controllers/test_controller.rb
+ class TestController < ApplicationController
+ # caches_page :index
+ # caches_action :index
+ def index
+ end
+ end
+ $ wgetc -qO- http://localhost:3000/test
+ English: Test OK
+
+ $ less log/development.log
+ Cached fragment miss: views/localhost:3000/test (0.1ms)
+
+やっぱりうまく行っていない。
+
+ここは、views/localhost:3000/test.en となってほしいところ。
+
+これは ActiveSupport::Cache のテリトリーらしい。
+
+ LANG:ruby(linenumber)
+ module ActionController
+ module Caching
+ module Fragments
+ def fragment_cache_key(key)
+ ActiveSupport::Cache.expand_cache_key(
+ key.is_a?(Hash) ?
+ url_for(key.reverse_merge(
+ :rails_language=>request.language_order.first)
+ ).split("://").last :
+ key,
+ :views)
+ end
+ end
+ end
+ end
+
+** テスト [#le18b1e9]
+
+ LANG:console
+ $ wgetc -qO- 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
+ 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
+ English: Test OK
+
+ $ less log/development.log
+ Cached fragment miss: views/localhost:3000/test.en?param1=test (0.1ms)
+
+うまく行っているみたい。