---
src/app/controllers/application_controller.rb | 26 ++++++++++++++++++++++++-
1 files changed, 25 insertions(+), 1 deletions(-)
diff --git a/src/app/controllers/application_controller.rb
b/src/app/controllers/application_controller.rb
index 3dbbb05..35b0b18 100644
--- a/src/app/controllers/application_controller.rb
+++ b/src/app/controllers/application_controller.rb
@@ -25,7 +25,7 @@ class ApplicationController < ActionController::Base
# FIXME: not sure what we're doing aobut service layer w/ deltacloud
include ApplicationService
helper_method :current_user, :filter_view?
- before_filter :read_breadcrumbs
+ before_filter :read_breadcrumbs, :set_locale
# General error handlers, must be in order from least specific
# to most specific
@@ -275,4 +275,28 @@ class ApplicationController < ActionController::Base
end
end
+ # TODO - May want to store this in the database / user session somewhere
+ def set_locale
+ I18n.locale = detect_locale || I18n.default_locale
+ end
+
+ # This is expected to return nil if there is no match
+ def detect_locale
+
+ # This code is inspired slightly by
https://github.com/iain/http_accept_language/blob/master/lib/http_accept_...
+ languages = env['HTTP_ACCEPT_LANGUAGE'].split(',')
+ prefs = []
+ languages.each do |language|
+ language += ";q=1.0" unless language.match(";q=\d+\.\d+")
+ lang_code, lang_weight = language.split(";q=")
+ lang_code = lang_code.gsub(/-[a-z]+$/i) { |x| x.upcase }.to_sym
+ prefs << [lang_weight, lang_code]
+ end
+ # This is slightly abusing array sorting
+ ordered_languages = prefs.sort.reverse.collect{|p| p[1]}
+
+ (ordered_languages & I18n.available_locales).first
+
+ end
+
end
--
1.7.6.4