-
Notifications
You must be signed in to change notification settings - Fork 21
/
controller.rb
28 lines (26 loc) · 1.02 KB
/
controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module TranslationIO
module Controller
def set_locale
requested_locale = params[:locale] ||
session[:locale] ||
cookies[:locale] ||
extract_browser_locale(request.env['HTTP_ACCEPT_LANGUAGE']) ||
I18n.default_locale
if I18n.available_locales.include?(requested_locale.to_sym)
session[:locale] = requested_locale
I18n.locale = requested_locale
else
if respond_to?(:root_path)
redirect_to root_path(:locale => I18n.default_locale)
else
redirect_to "/?locale=#{I18n.default_locale}"
end
end
end
def extract_browser_locale(http_accept_language)
http_accept_language.to_s.scan(/[a-z]{2}(?:-[A-Z]{2})?/).detect do |candidate|
I18n.available_locales.include?(candidate.to_sym)
end
end
end
end