adds i18n support to routes and application controller; default_locale is set to :en

This commit is contained in:
Manuel Wiedenmann 2013-05-31 18:42:28 +02:00
parent c42c00b5f1
commit c2c1961bd0
4 changed files with 239 additions and 184 deletions

View file

@ -1,10 +1,13 @@
# encoding: utf-8 # encoding: utf-8
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
include Foodsoft::ControllerExtensions::Locale
helper_method :available_locales
protect_from_forgery 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 after_filter :remove_controller
# Returns the controller handling the current request. # Returns the controller handling the current request.
def self.current def self.current
Thread.current[:application_controller] Thread.current[:application_controller]
@ -26,7 +29,7 @@ class ApplicationController < ActionController::Base
redirect_to login_url, :alert => 'Access denied!' redirect_to login_url, :alert => 'Access denied!'
end end
private private
def authenticate(role = 'any') def authenticate(role = 'any')
# Attempt to retrieve authenticated user from controller instance or session... # Attempt to retrieve authenticated user from controller instance or session...
@ -141,9 +144,5 @@ class ApplicationController < ActionController::Base
def default_url_options(options = {}) def default_url_options(options = {})
{foodcoop: FoodsoftConfig.scope} {foodcoop: FoodsoftConfig.scope}
end end
# Used to prevent accidently switching to :en in production mode.
def select_language
I18n.locale = :de
end
end end

View file

@ -31,7 +31,7 @@ module Foodsoft
# Internationalization. # Internationalization.
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] 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. # Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8" config.encoding = "utf-8"

View file

@ -8,183 +8,187 @@ Foodsoft::Application.routes.draw do
root :to => redirect("/#{FoodsoftConfig.scope}") root :to => redirect("/#{FoodsoftConfig.scope}")
scope '/:foodcoop' do constraints(locale: /[a-z]{2}/) do
scope "(:locale)" do
# Root path scope '/:foodcoop' do
root :to => 'home#index'
# Root path
########### Sessions root :to => 'home#index'
match '/login' => 'sessions#new', :as => 'login' ########### Sessions
match '/logout' => 'sessions#destroy', :as => 'logout'
get '/login/forgot_password' => 'login#forgot_password', as: :forgot_password match '/login' => 'sessions#new', :as => 'login'
get '/login/new_password' => 'login#new_password', as: :new_password match '/logout' => 'sessions#destroy', :as => 'logout'
match '/login/accept_invitation/:token' => 'login#accept_invitation', as: :accept_invitation get '/login/forgot_password' => 'login#forgot_password', as: :forgot_password
resources :sessions, :only => [:new, :create, :destroy] get '/login/new_password' => 'login#new_password', as: :new_password
match '/login/accept_invitation/:token' => 'login#accept_invitation', as: :accept_invitation
########### User specific resources :sessions, :only => [:new, :create, :destroy]
match '/home/profile' => 'home#profile', :as => 'my_profile' ########### User specific
match '/home/ordergroup' => 'home#ordergroup', :as => 'my_ordergroup'
match '/home/cancel_membership' => 'home#cancel_membership', :as => 'cancel_membership' match '/home/profile' => 'home#profile', :as => 'my_profile'
match '/home/ordergroup' => 'home#ordergroup', :as => 'my_ordergroup'
############ Wiki match '/home/cancel_membership' => 'home#cancel_membership', :as => 'cancel_membership'
resources :pages do ############ Wiki
get :all, :on => :collection
get :version, :on => :member resources :pages do
get :revert, :on => :member get :all, :on => :collection
end get :version, :on => :member
match '/wiki/:permalink' => 'pages#show', :as => 'wiki_page' # , :constraints => {:permalink => /[^\s]+/} get :revert, :on => :member
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
end end
end match '/wiki/:permalink' => 'pages#show', :as => 'wiki_page' # , :constraints => {:permalink => /[^\s]+/}
end match '/wiki' => 'pages#show', :defaults => {:permalink => 'Home'}, :as => 'wiki'
resources :article_categories ############ Orders, ordering
########### Finance resources :orders do
member do
namespace :finance do post :finish
root :to => 'base#index' post :add_comment
end
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 end
resources :order_articles resources :group_orders do
end get :archive, :on => :collection
resources :group_order_articles do
member do
put :update_result
end end
end
resources :order_comments, :only => [:new, :create]
resources :invoices
############ Foodcoop orga
resources :ordergroups, :only => [:index] do
resources :financial_transactions, :as => :transactions resources :invites, :only => [:new, :create]
end
resources :tasks do
get 'transactions/new_collection' => 'financial_transactions#new_collection', :as => 'new_transaction_collection' collection do
post 'transactions/create_collection' => 'financial_transactions#create_collection', :as => 'create_transaction_collection' get :user
end get :archive
get :workgroup
########### Administration end
member do
namespace :admin do post :accept
root :to => 'base#index' post :reject
post :set_done
resources :users end
end
resources :workgroups do
get :memberships, :on => :member resources :messages, :only => [:index, :show, :new, :create]
end
namespace :foodcoop do
resources :ordergroups do root :to => 'users#index'
get :memberships, :on => :member
end resources :users, :only => [:index]
end
resources :ordergroups, :only => [:index]
############## Feedback
resources :workgroups, :only => [:index, :edit, :update]
resource :feedback, :only => [:new, :create], :controller => 'feedback' end
############## The rest ########### Article management
resources :users, :only => [:index] resources :stock_takings do
collection do
# TODO: This is very error prone. Better deactivate this catch all route get :fill_new_stock_article_form
match ':controller(/:action(/:id))(.:format)' post :add_stock_article
end
end # End of /:foodcoop scope 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 end

View file

@ -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