summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/application_controller.rb68
-rw-r--r--app/views/layouts/_navigation.html.haml6
2 files changed, 65 insertions, 9 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index de8d06b..35d6cb4 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,13 +1,14 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+ before_filter :set_locale
before_filter :no_cache_header
before_filter :no_frame_header
+ before_filter :language_header
ActiveSupport.run_load_hooks(:application_controller, self)
protected
-
rescue_from StandardError do |e|
respond_to do |format|
format.json { render_json_error(e) }
@@ -32,13 +33,59 @@ class ApplicationController < ActionController::Base
end
helper_method :bold
+ ##
+ ## LOCALE
+ ##
+
#
- # we want to prevent the browser from caching anything, just to be safe.
+ # URL paths for which we don't enforce the locale as the prefix of the path.
#
- def no_cache_header
- response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
- response.headers["Pragma"] = "no-cache"
- response.headers["Expires"] = "0"
+ NON_LOCALE_PATHS = /^\/(assets|webfinger|.well-known|rails|key|[0-9]+)($|\/)/
+
+ #
+ # Before filter to set the current locale. Possible outcomes:
+ #
+ # (a) do nothing for certain routes and requests.
+ # (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])
+ 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
+ else
+ self.default_url_options[:locale] = nil
+ end
+ end
+
+ ##
+ ## HTTP HEADERS
+ ## These are in individual helpers so that controllers can disable them if needed.
+ ##
+
+ #
+ # Not necessary, but kind to let the browser know the current locale.
+ #
+ def language_header
+ response.headers["Content-Language"] = I18n.locale.to_s
end
#
@@ -48,4 +95,13 @@ class ApplicationController < ActionController::Base
response.headers["X-Frame-Options"] = "DENY"
end
+ #
+ # we want to prevent the browser from caching anything, just to be safe.
+ #
+ def no_cache_header
+ response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
+ response.headers["Pragma"] = "no-cache"
+ response.headers["Expires"] = "0"
+ end
+
end
diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml
index 6de567a..b81c43d 100644
--- a/app/views/layouts/_navigation.html.haml
+++ b/app/views/layouts/_navigation.html.haml
@@ -1,7 +1,7 @@
%ul.nav.sidenav
- = link_to_navigation t(:overview), @user, :active => controller?(:overviews)
- = link_to_navigation t(:account_settings), edit_user_path(@user), :active => controller?(:users)
+ = link_to_navigation t(:overview), @user, :active => (controller?(:users) && action?(:show))
+ = link_to_navigation t(:account_settings), edit_user_path(@user), :active => (controller?(:users) && !action?(:show))
- # will want link for identity settings
= link_to_navigation t(:support_tickets), auto_tickets_path, :active => controller?(:tickets)
- = link_to_navigation t(:billing_settings), billing_top_link(@user), :active => controller?(:customer, :payments, :subscriptions, :credit_card_info) if APP_CONFIG[:payment].present?
+ = link_to_navigation t(:billing_settings), billing_top_link(@user), :active => controller?(:customer, :payments, :subscriptions, :credit_card_info) if APP_CONFIG[:billing]
= link_to_navigation t(:logout), logout_path, :method => :delete