履歴: ソフトウェア/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 4:15:30) → 比較: ソフトウェア/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
@@ -799,23 +800,199 @@
$ tail log/development.log
- Processing TestController#index (for 127.0.0.1 at 2009-06-07 13:14:18) [GET]
- Cached fragment hit: views/localhost:3000/test (0.1ms)
- Rendering template within layouts/application
- Rendering test/index
- Rendered test/_ok (0.4ms)
Cached fragment miss: views/localhost:3000/test (0.1ms)
- Completed in 15ms (View: 8, DB: 0) | 200 OK [http://localhost/test]
$ wgetc -qO- http://localhost:3000/test
English: Test OK
$ wgetc -qO- http://localhost:3000/test.en
- Processing TestController#index (for 127.0.0.1 at 2009-06-07 13:04:57) [GET]
- Parameters: {"rails_language"=>"en"}
- Cached fragment hit: views/localhost:3000/test?format=en (0.1ms)
- Rendering template within layouts/application
- Rendering test/index
- Rendered test/_ok (0.4ms)
Cached fragment miss: views/localhost:3000/test?format=en (0.1ms)
- Completed in 13ms (View: 8, DB: 0) | 200 OK [http://localhost/test.en]
+
+キャッシュ内容を表すキーが
+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)
+
+うまく行っているみたい。