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

View File

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

View File

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

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