From c2c1961bd02edcf1cb91d431a44044eb2e7d2386 Mon Sep 17 00:00:00 2001 From: Manuel Wiedenmann Date: Fri, 31 May 2013 18:42:28 +0200 Subject: [PATCH] adds i18n support to routes and application controller; default_locale is set to :en --- app/controllers/application_controller.rb | 13 +- config/application.rb | 2 +- config/routes.rb | 356 +++++++++++----------- lib/foodsoft/controller_extensions.rb | 52 ++++ 4 files changed, 239 insertions(+), 184 deletions(-) create mode 100644 lib/foodsoft/controller_extensions.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 29b752a0..96b2e510 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,13 @@ # encoding: utf-8 class ApplicationController < ActionController::Base + include Foodsoft::ControllerExtensions::Locale + helper_method :available_locales protect_from_forgery - before_filter :select_language, :select_foodcoop, :authenticate, :store_controller, :items_per_page, :set_redirect_to + before_filter :select_foodcoop, :authenticate, :store_controller, :items_per_page, :set_redirect_to after_filter :remove_controller + # Returns the controller handling the current request. def self.current Thread.current[:application_controller] @@ -26,7 +29,7 @@ class ApplicationController < ActionController::Base redirect_to login_url, :alert => 'Access denied!' end - private + private def authenticate(role = 'any') # Attempt to retrieve authenticated user from controller instance or session... @@ -141,9 +144,5 @@ class ApplicationController < ActionController::Base def default_url_options(options = {}) {foodcoop: FoodsoftConfig.scope} end - - # Used to prevent accidently switching to :en in production mode. - def select_language - I18n.locale = :de - end + end diff --git a/config/application.rb b/config/application.rb index 32ea295b..6d519277 100644 --- a/config/application.rb +++ b/config/application.rb @@ -31,7 +31,7 @@ module Foodsoft # Internationalization. config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] - config.i18n.default_locale = :de + config.i18n.default_locale = :en # Configure the default encoding used in templates for Ruby 1.9. config.encoding = "utf-8" diff --git a/config/routes.rb b/config/routes.rb index cbcf2828..b4e8ab4b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,183 +8,187 @@ Foodsoft::Application.routes.draw do root :to => redirect("/#{FoodsoftConfig.scope}") - scope '/:foodcoop' do - - # Root path - root :to => 'home#index' - - ########### Sessions - - match '/login' => 'sessions#new', :as => 'login' - match '/logout' => 'sessions#destroy', :as => 'logout' - get '/login/forgot_password' => 'login#forgot_password', as: :forgot_password - get '/login/new_password' => 'login#new_password', as: :new_password - match '/login/accept_invitation/:token' => 'login#accept_invitation', as: :accept_invitation - resources :sessions, :only => [:new, :create, :destroy] - - ########### User specific - - match '/home/profile' => 'home#profile', :as => 'my_profile' - match '/home/ordergroup' => 'home#ordergroup', :as => 'my_ordergroup' - match '/home/cancel_membership' => 'home#cancel_membership', :as => 'cancel_membership' - - ############ Wiki - - resources :pages do - get :all, :on => :collection - get :version, :on => :member - get :revert, :on => :member - end - match '/wiki/:permalink' => 'pages#show', :as => 'wiki_page' # , :constraints => {:permalink => /[^\s]+/} - match '/wiki' => 'pages#show', :defaults => {:permalink => 'Home'}, :as => 'wiki' - - ############ Orders, ordering - - resources :orders do - member do - post :finish - post :add_comment - end - end - - resources :group_orders do - get :archive, :on => :collection - end - - resources :order_comments, :only => [:new, :create] - - ############ Foodcoop orga - - resources :invites, :only => [:new, :create] - - resources :tasks do - collection do - get :user - get :archive - get :workgroup - end - member do - post :accept - post :reject - post :set_done - end - end - - resources :messages, :only => [:index, :show, :new, :create] - - namespace :foodcoop do - root :to => 'users#index' - - resources :users, :only => [:index] - - resources :ordergroups, :only => [:index] - - resources :workgroups, :only => [:index, :edit, :update] - end - - ########### Article management - - resources :stock_takings do - collection do - get :fill_new_stock_article_form - post :add_stock_article - end - end - - resources :stock_articles, :to => 'stockit' do - collection do - get :articles_search - get :fill_new_stock_article_form - end - end - - resources :suppliers do - get :shared_suppliers, :on => :collection - - resources :deliveries do - post :drop_stock_change, :on => :member - post :add_stock_article, :on => :collection - end - - resources :articles do - collection do - post :update_selected - get :edit_all - post :update_all - get :upload - post :parse_upload - post :create_from_upload - get :shared - get :import - post :sync - post :update_synchronized + constraints(locale: /[a-z]{2}/) do + scope "(:locale)" do + scope '/:foodcoop' do + + # Root path + root :to => 'home#index' + + ########### Sessions + + match '/login' => 'sessions#new', :as => 'login' + match '/logout' => 'sessions#destroy', :as => 'logout' + get '/login/forgot_password' => 'login#forgot_password', as: :forgot_password + get '/login/new_password' => 'login#new_password', as: :new_password + match '/login/accept_invitation/:token' => 'login#accept_invitation', as: :accept_invitation + resources :sessions, :only => [:new, :create, :destroy] + + ########### User specific + + match '/home/profile' => 'home#profile', :as => 'my_profile' + match '/home/ordergroup' => 'home#ordergroup', :as => 'my_ordergroup' + match '/home/cancel_membership' => 'home#cancel_membership', :as => 'cancel_membership' + + ############ Wiki + + resources :pages do + get :all, :on => :collection + get :version, :on => :member + get :revert, :on => :member end - end - end - - resources :article_categories - - ########### Finance - - namespace :finance do - root :to => 'base#index' - - resources :order, controller: 'balancing', path: 'balancing' do - member do - get :update_summary - get :edit_note - put :update_note - - get :confirm - put :close - put :close_direct + match '/wiki/:permalink' => 'pages#show', :as => 'wiki_page' # , :constraints => {:permalink => /[^\s]+/} + match '/wiki' => 'pages#show', :defaults => {:permalink => 'Home'}, :as => 'wiki' + + ############ Orders, ordering + + resources :orders do + member do + post :finish + post :add_comment + end end - - resources :order_articles - end - - resources :group_order_articles do - member do - put :update_result + + resources :group_orders do + get :archive, :on => :collection end - end - - resources :invoices - - resources :ordergroups, :only => [:index] do - resources :financial_transactions, :as => :transactions - end - - get 'transactions/new_collection' => 'financial_transactions#new_collection', :as => 'new_transaction_collection' - post 'transactions/create_collection' => 'financial_transactions#create_collection', :as => 'create_transaction_collection' - end - - ########### Administration - - namespace :admin do - root :to => 'base#index' - - resources :users - - resources :workgroups do - get :memberships, :on => :member - end - - resources :ordergroups do - get :memberships, :on => :member - end - end - - ############## Feedback - - resource :feedback, :only => [:new, :create], :controller => 'feedback' - - ############## The rest - - resources :users, :only => [:index] - - # TODO: This is very error prone. Better deactivate this catch all route - match ':controller(/:action(/:id))(.:format)' - - end # End of /:foodcoop scope + + resources :order_comments, :only => [:new, :create] + + ############ Foodcoop orga + + resources :invites, :only => [:new, :create] + + resources :tasks do + collection do + get :user + get :archive + get :workgroup + end + member do + post :accept + post :reject + post :set_done + end + end + + resources :messages, :only => [:index, :show, :new, :create] + + namespace :foodcoop do + root :to => 'users#index' + + resources :users, :only => [:index] + + resources :ordergroups, :only => [:index] + + resources :workgroups, :only => [:index, :edit, :update] + end + + ########### Article management + + resources :stock_takings do + collection do + get :fill_new_stock_article_form + post :add_stock_article + end + end + + resources :stock_articles, :to => 'stockit' do + collection do + get :articles_search + get :fill_new_stock_article_form + end + end + + resources :suppliers do + get :shared_suppliers, :on => :collection + + resources :deliveries do + post :drop_stock_change, :on => :member + post :add_stock_article, :on => :collection + end + + resources :articles do + collection do + post :update_selected + get :edit_all + post :update_all + get :upload + post :parse_upload + post :create_from_upload + get :shared + get :import + post :sync + post :update_synchronized + end + end + end + + resources :article_categories + + ########### Finance + + namespace :finance do + root :to => 'base#index' + + resources :order, controller: 'balancing', path: 'balancing' do + member do + get :update_summary + get :edit_note + put :update_note + + get :confirm + put :close + put :close_direct + end + + resources :order_articles + end + + resources :group_order_articles do + member do + put :update_result + end + end + + resources :invoices + + resources :ordergroups, :only => [:index] do + resources :financial_transactions, :as => :transactions + end + + get 'transactions/new_collection' => 'financial_transactions#new_collection', :as => 'new_transaction_collection' + post 'transactions/create_collection' => 'financial_transactions#create_collection', :as => 'create_transaction_collection' + end + + ########### Administration + + namespace :admin do + root :to => 'base#index' + + resources :users + + resources :workgroups do + get :memberships, :on => :member + end + + resources :ordergroups do + get :memberships, :on => :member + end + end + + ############## Feedback + + resource :feedback, :only => [:new, :create], :controller => 'feedback' + + ############## The rest + + resources :users, :only => [:index] + + # TODO: This is very error prone. Better deactivate this catch all route + match ':controller(/:action(/:id))(.:format)' + + end # End of /:foodcoop scope + end # End of /:locale scope + end # End of :locale constraints end diff --git a/lib/foodsoft/controller_extensions.rb b/lib/foodsoft/controller_extensions.rb new file mode 100644 index 00000000..fe8c940f --- /dev/null +++ b/lib/foodsoft/controller_extensions.rb @@ -0,0 +1,52 @@ +# -*- encoding : utf-8 -*- +module Foodsoft + module ControllerExtensions + module Locale + extend ActiveSupport::Concern + + included do + before_filter :set_locale + end + + module InstanceMethods + def explicitly_requested_language + params[:locale] + end + + def session_language + session[:locale] + end + + def browser_language + request.env['HTTP_ACCEPT_LANGUAGE'] ? request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first : nil + end + + def default_language + ::I18n.default_locale + end + + protected + + def select_language_according_to_priority + explicitly_requested_language || session_language || browser_language + end + + def available_locales + ::I18n.available_locales + end + + def set_locale + if available_locales.include?(select_language_according_to_priority) + ::I18n.locale = select_language_according_to_priority + else + ::I18n.locale = default_language + end + + locale = session[:locale] = ::I18n.locale + logger.info("Set locale to #{locale}") + end + end + + end + end +end \ No newline at end of file