Module | ActionController::MuckApplication::InstanceMethods |
In: |
lib/action_controller/muck_application.rb
|
********************************************** Locale methods I18n methods from: guides.rubyonrails.org/i18n.html zargony.com/2009/01/09/selecting-the-locale-for-a-request
# File lib/action_controller/muck_application.rb, line 17 17: def discover_locale 18: I18n.locale = extract_locale_from_user_selection || extract_locale_from_tld || extract_locale_from_subdomain || extract_locale_from_headers || I18n.default_locale 19: end
# File lib/action_controller/muck_application.rb, line 21 21: def extract_locale_from_browser 22: if http_lang = request.env["HTTP_ACCEPT_LANGUAGE"] and ! http_lang.blank? 23: browser_locale = http_lang[/^[a-z]{2}/i].downcase + '-' + http_lang[3,2].upcase 24: browser_locale.sub!(/-US/, '') 25: end 26: nil 27: end
# File lib/action_controller/muck_application.rb, line 38 38: def extract_locale_from_headers 39: if http_lang = request.headers["HTTP_ACCEPT_LANGUAGE"] and ! http_lang.blank? 40: preferred_locales = http_lang.split(',').map { |l| l.split(';').first } 41: accepted_locales = preferred_locales.select { |l| I18n.available_locales.include?(l.to_sym) } 42: accepted_locales.empty? ? nil : accepted_locales.first.to_sym 43: end 44: end
Get locale code from request subdomain (like 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
# File lib/action_controller/muck_application.rb, line 59 59: def extract_locale_from_subdomain 60: parsed_locale = request.subdomains.first 61: if !parsed_locale.blank? 62: I18n.available_locales.include?(parsed_locale.to_sym) ? parsed_locale.to_sym : nil 63: else 64: nil 65: end 66: 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
# File lib/action_controller/muck_application.rb, line 50 50: def extract_locale_from_tld 51: parsed_locale = request.host.split('.').last 52: (I18n.available_locales.include? parsed_locale) ? parsed_locale.to_sym : nil 53: end
# File lib/action_controller/muck_application.rb, line 29 29: def extract_locale_from_user_selection 30: if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym) 31: cookies['locale'] = { :value => params[:locale], :expires => 1.year.from_now } 32: params[:locale].to_sym 33: elsif cookies['locale'] && I18n.available_locales.include?(cookies['locale'].to_sym) 34: cookies['locale'].to_sym 35: end 36: end
# File lib/action_controller/muck_application.rb, line 94 94: def get_redirect_to 95: if params[:redirect_to] 96: redirect_to params[:redirect_to] 97: else 98: yield 99: end 100: end
Use send_form_email to send the contents of any form to the support email address
# File lib/action_controller/muck_application.rb, line 87 87: def send_form_email(params, subject) 88: body = [] 89: params.each_pair { |k,v| body << "#{k}: #{v}" } 90: BasicMailer.deliver_mail(:subject => subject, :body => body.join("\n")) 91: end
# File lib/action_controller/muck_application.rb, line 76 76: def set_will_paginate_string 77: # Because I18n.locale are dynamically determined in ApplicationController, 78: # it should not put in config/initializers/will_paginate.rb 79: WillPaginate::ViewHelpers.pagination_options[:previous_label] = t('common.previous_page') 80: WillPaginate::ViewHelpers.pagination_options[:next_label] = t('common.next_page') 81: end