|
|
@ -1,7 +1,9 @@ |
|
|
class LanguagesController < ApplicationController |
|
|
class LanguagesController < ApplicationController |
|
|
|
|
|
|
|
|
|
|
|
before_action :set_locale_to_default |
|
|
|
|
|
helper_method :accept_language |
|
|
|
|
|
|
|
|
def index |
|
|
def index |
|
|
language = parse_accept_language() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end |
|
|
end |
|
|
@ -9,11 +11,16 @@ class LanguagesController < ApplicationController |
|
|
|
|
|
|
|
|
private |
|
|
private |
|
|
|
|
|
|
|
|
def parse_accept_language(header=request.env['HTTP_ACCEPT_LANGUAGE']) |
|
|
|
|
|
header.to_s.split(',').map { |l| |
|
|
|
|
|
|
|
|
def accept_language |
|
|
|
|
|
@accept_language ||= request.env['HTTP_ACCEPT_LANGUAGE'].to_s.split(',').map { |l| |
|
|
lang, q_factor = l.split(';q=') |
|
|
lang, q_factor = l.split(';q=') |
|
|
[lang, (q_factor || '1').to_f] |
|
|
[lang, (q_factor || '1').to_f] |
|
|
}.sort_by { |_, q| -q }.map(&:first).first |
|
|
|
|
|
|
|
|
}.sort_by { |_, q| -q }.map(&:first).first&.split('-')&.first |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_locale_to_default |
|
|
|
|
|
I18n.locale = I18n.default_locale |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
end |
|
|
end |