diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb
index ff44849..f3a4f09 100644
--- a/actionpack/lib/action_controller/routing/route_set.rb
+++ b/actionpack/lib/action_controller/routing/route_set.rb
@@ -170,18 +170,24 @@ module ActionController
def #{selector}(*args)
#{generate_optimisation_block(route, kind)}
+ locale = #{route.segment_keys.include?(:locale) ? '{ :locale => I18n.locale || I18n.default_locale }' : '{}'}
+
opts = if args.empty? || Hash === args.first
args.first || {}
else
options = args.extract_options!
- args = args.zip(#{route.segment_keys.inspect}).inject({}) do |h, (v, k)|
+
+ segments = #{route.segment_keys.inspect}
+ #{'segments.delete(:locale) if segments.size > args.size' if route.segment_keys.include?(:locale)}
+
+ args = args.zip(segments).inject({}) do |h, (v, k)|
h[k] = v
h
end
options.merge(args)
end
- url_for(#{hash_access_method}(opts))
+ url_for(#{hash_access_method}(locale.merge(opts)))
end
protected :#{selector}
end_eval
次に routes.rb ファイルを設定する必要あり。
map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' # ==================================================== # Put all non locale controlled routes above this line # 通常ルートの下に、path_prefix で管理したルートを設定 # ==================================================== map.resources :mypages, :path_prefix => "/:locale" map.root :controller => "mypages", :action => "index", :locale => "ja" map.connect '/:locale/:controller/:action/:id' map.connect '/:locale/:controller/:action/:id.:format'これをやっておけば、リンクヘルパーを使った際、自動的に現在の locale が引き継がれるので便利。
例えば、http://mysite.com/ja/home の中で、<%= link_to "記事", mypages_path %> と書くと、次のようになる <a href="http://mysite.com/ja/mypages">記事もちろん、アプリケーションコントローラーで言語の設定は必要。
Making a link to change locale
application_helper.rb
def change_locale(locale)
link = request.parameters
link[:locale] = locale
link
end

0 件のコメント:
コメントを投稿