module ActionController module MuckApplication module ClassMethods end module InstanceMethods private def set_locale # TODO add set local code # http://zargony.com/2009/01/09/selecting-the-locale-for-a-request # preferred_locales = request.headers['HTTP_ACCEPT_LANGUAGE'].split(',').map { |l| l.split(';').first } # I18n.locale = preferred_locales.select { |l| I18n.available_locales.include?(l.to_sym) } # # if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym) # cookies['locale'] = { :value => params[:locale], :expires => 1.year.from_now } # I18n.locale = params[:locale].to_sym # elsif cookies['locale'] && I18n.available_locales.include?(cookies['locale'].to_sym) # I18n.locale = cookies['locale'].to_sym # end # # session[:locale] = params[:locale] if params[:locale] # if http_lang = request.env["HTTP_ACCEPT_LANGUAGE"] and ! http_lang.blank? # browser_locale = http_lang[/^[a-z]{2}/i].downcase + '-' + http_lang[3,2].upcase # browser_locale.sub!(/-US/, '') # end # I18n.locale = session[:locale] || cookies[:locale] || browser_locale || I18n.default_locale || 'en' # # set_will_paginate_string if defined? WillPaginate # # #@locales_available ||= I18n.load_path.collect do |locale_file| # # File.basename(File.basename(locale_file, '.rb'), '.yml') # #end.uniq.sort # @locales_available ||= [["English", "en"]] end def setup_paging @page = (params[:page] || 1).to_i @page = 1 if @page < 1 @per_page = (params[:per_page] || (RAILS_ENV=='test' ? 1 : 40)).to_i end def set_will_paginate_string # Because I18n.locale are dynamically determined in ApplicationController, # it should not put in config/initializers/will_paginate.rb WillPaginate::ViewHelpers.pagination_options[:previous_label] = t('common.previous_page') WillPaginate::ViewHelpers.pagination_options[:next_label] = t('common.next_page') end def get_redirect_to if params[:redirect_to] redirect_to params[:redirect_to] else yield end end end def self.included(receiver) receiver.extend ClassMethods receiver.class_eval do include InstanceMethods before_filter :set_locale end end end end