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]
@ -142,8 +145,4 @@ class ApplicationController < ActionController::Base
{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,6 +8,8 @@ Foodsoft::Application.routes.draw do
root :to => redirect("/#{FoodsoftConfig.scope}")
constraints(locale: /[a-z]{2}/) do
scope "(:locale)" do
scope '/:foodcoop' do
# Root path
@ -187,4 +189,6 @@ Foodsoft::Application.routes.draw do
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