From 89018d89ecb0f07efa05048cc859447d341431e0 Mon Sep 17 00:00:00 2001 From: Luca Guidi Date: Mon, 6 Apr 2009 12:37:32 +0200 Subject: [PATCH] Added locales controller --- app/controllers/locales_controller.rb | 14 ++++++++++++++ app/helpers/locales_helper.rb | 2 ++ config/routes.rb | 3 ++- test/functional/locales_controller_test.rb | 14 ++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 app/controllers/locales_controller.rb create mode 100644 app/helpers/locales_helper.rb create mode 100644 test/functional/locales_controller_test.rb diff --git a/app/controllers/locales_controller.rb b/app/controllers/locales_controller.rb new file mode 100644 index 0000000..3824b0c --- /dev/null +++ b/app/controllers/locales_controller.rb @@ -0,0 +1,14 @@ +class LocalesController < ApplicationController + before_filter :normalize_http_referer + + # PUT /locales/en/change + def change + I18n.locale = params[:id] + redirect_to :back + end + + private + def normalize_http_referer + request.env["HTTP_REFERER"] ||= "/" + end +end diff --git a/app/helpers/locales_helper.rb b/app/helpers/locales_helper.rb new file mode 100644 index 0000000..2f932ec --- /dev/null +++ b/app/helpers/locales_helper.rb @@ -0,0 +1,2 @@ +module LocalesHelper +end diff --git a/config/routes.rb b/config/routes.rb index 5018aba..c90f0ef 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,4 @@ ActionController::Routing::Routes.draw do |map| - map.resources :translations, :only => [ :index ], :member => { :save => :post } + map.resources :translations, :only => [ :index ], :member => { :save => :post } + map.resources :locales, :only => [ ], :member => { :change => :put } end diff --git a/test/functional/locales_controller_test.rb b/test/functional/locales_controller_test.rb new file mode 100644 index 0000000..2d4d341 --- /dev/null +++ b/test/functional/locales_controller_test.rb @@ -0,0 +1,14 @@ +require File.expand_path(File.dirname(__FILE__) + '/../test_helper') + +class LocalesControllerTest < ActionController::TestCase + test "routing" do + assert_routing({ :method => :put, :path => "/locales/en/change" }, + :controller => "locales", :action => "change", :id => "en" ) + end + + test "should change locale" do + put :change, :id => "it" + assert_redirected_to "/" + assert_equal "it", I18n.locale + end +end \ No newline at end of file