From 83a9cadbb13bd2292c7d064d40721fa5f64119fb Mon Sep 17 00:00:00 2001 From: elijah Date: Mon, 15 Jun 2015 17:14:26 -0700 Subject: added CommonLanguages gem --- app/controllers/application_controller.rb | 36 ++++++++++++++++--------------- app/controllers/pages_controller.rb | 2 +- 2 files changed, 20 insertions(+), 18 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ff291f2..58e6f83 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -37,11 +37,6 @@ class ApplicationController < ActionController::Base ## LOCALE ## - # - # URL paths for which we don't enforce the locale as the prefix of the path. - # - NON_LOCALE_PATHS = /^\/(assets|webfinger|.well-known|rails|key|[0-9]+)($|\/)/ - # # Before filter to set the current locale. Possible outcomes: # @@ -49,30 +44,24 @@ class ApplicationController < ActionController::Base # (b) if path already starts with locale, set I18n.locale and default_url_options. # (c) otherwise, redirect so that path starts with locale. # - # If the locale happens to be the default local, no paths are prefixed with the locale. - # def set_locale - if request.method == "GET" && request.format == 'text/html' && request.path !~ NON_LOCALE_PATHS - if params[:locale] && LOCALES_STRING.include?(params[:locale]) + if request_may_have_locale?(request) + if CommonLanguages::available_code?(params[:locale]) I18n.locale = params[:locale] - update_default_url_options else I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales) || I18n.default_locale - update_default_url_options if I18n.locale != I18n.default_locale redirect_to url_for(params.merge(:locale => I18n.locale)) end end - else - update_default_url_options end end - def update_default_url_options - if I18n.locale != I18n.default_locale - self.default_url_options[:locale] = I18n.locale + def default_url_options(options={}) + if request_may_have_locale?(request) + { :locale => I18n.locale } else - self.default_url_options[:locale] = nil + { :locale => nil } end end @@ -104,4 +93,17 @@ class ApplicationController < ActionController::Base response.headers["Expires"] = "0" end + private + + # + # URL paths for which we don't enforce the locale as the prefix of the path. + # + NON_LOCALE_PATHS = /^\/(assets|webfinger|.well-known|rails|key|[0-9]+)($|\/)/ + + # + # For some requests, we ignore locale determination. + # + def request_may_have_locale?(request) + request.method == "GET" && request.format == 'text/html' && request.path !~ NON_LOCALE_PATHS + end end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index c477b7c..e0f39e3 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -12,7 +12,7 @@ class PagesController < ApplicationController private def page_name - request.path.sub(/^\/(#{MATCH_LOCALE}\/)?/, '') + request.path.sub(/^\/(#{CommonLanguages.match_available}\/)?/, '') end end -- cgit v1.2.3