module ActionController module MuckApplication module ClassMethods end module InstanceMethods private # I18n methods from: # http://guides.rubyonrails.org/i18n.html # http://zargony.com/2009/01/09/selecting-the-locale-for-a-request def discover_locale I18n.locale = extract_locale_from_user_selection || extract_locale_from_tld || extract_locale_from_subdomain || extract_locale_from_headers || I18n.default_locale end def extract_locale_from_browser 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 nil end def extract_locale_from_user_selection if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym) cookies['locale'] = { :value => params[:locale], :expires => 1.year.from_now } params[:locale].to_sym elsif cookies['locale'] && I18n.available_locales.include?(cookies['locale'].to_sym) cookies['locale'].to_sym end end def extract_locale_from_headers preferred_locales = request.headers['HTTP_ACCEPT_LANGUAGE'].split(',').map { |l| l.split(';').first } accepted_locales = preferred_locales.select { |l| I18n.available_locales.include?(l.to_sym) } accepted_locales.empty? ? nil : accepted_locales.first end # Get locale from top-level domain or return nil if such locale is not available # You have to put something like: # 127.0.0.1 application.com # 127.0.0.1 application.it # 127.0.0.1 application.pl # in your /etc/hosts file to try this out locally def extract_locale_from_tld parsed_locale = request.host.split('.').last (I18n.available_locales.include? parsed_locale) ? parsed_locale.to_sym : nil end # Get locale code from request subdomain (like http://it.application.local:3000) # You have to put something like: # 127.0.0.1 gr.application.local # in your /etc/hosts file to try this out locally def extract_locale_from_subdomain parsed_locale = request.subdomains.first if !parsed_locale.blank? I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale.to_sym : nil else nil end 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 end end end end