Skip to content

Commit 89018d8

Browse files
author
Luca Guidi
committed
Added locales controller
1 parent be66c12 commit 89018d8

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

Diff for: app/controllers/locales_controller.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class LocalesController < ApplicationController
2+
before_filter :normalize_http_referer
3+
4+
# PUT /locales/en/change
5+
def change
6+
I18n.locale = params[:id]
7+
redirect_to :back
8+
end
9+
10+
private
11+
def normalize_http_referer
12+
request.env["HTTP_REFERER"] ||= "/"
13+
end
14+
end

Diff for: app/helpers/locales_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module LocalesHelper
2+
end

Diff for: config/routes.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
ActionController::Routing::Routes.draw do |map|
2-
map.resources :translations, :only => [ :index ], :member => { :save => :post }
2+
map.resources :translations, :only => [ :index ], :member => { :save => :post }
3+
map.resources :locales, :only => [ ], :member => { :change => :put }
34
end

Diff for: test/functional/locales_controller_test.rb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2+
3+
class LocalesControllerTest < ActionController::TestCase
4+
test "routing" do
5+
assert_routing({ :method => :put, :path => "/locales/en/change" },
6+
:controller => "locales", :action => "change", :id => "en" )
7+
end
8+
9+
test "should change locale" do
10+
put :change, :id => "it"
11+
assert_redirected_to "/"
12+
assert_equal "it", I18n.locale
13+
end
14+
end

0 commit comments

Comments
 (0)