Commit 34e900f2 authored by o@immerda.ch's avatar o@immerda.ch
Browse files

let's do something clever and actually parse the accept lang header

parent f10b6bd5
......@@ -8,12 +8,20 @@ class ApplicationController < ActionController::Base
before_action :set_locale
def valid_l(l)
return l if l && I18n.available_locales.include?(l)
return l if l && I18n.available_locales.include?(l.to_sym)
nil
end
def extract_locale
(request.env['HTTP_ACCEPT_LANGUAGE']||'').scan(/^[a-z]{2}/).select{|l| valid_l(l)}.first
req_langs = request.env['HTTP_ACCEPT_LANGUAGE'].to_s.split(",").map do |lang|
l, q = lang.split(";q=")
[l, (q || '1').to_f]
end
return nil if req_langs.empty?
req_langs = req_langs.sort_by(&:last).map(&:first).reverse
req_langs = req_langs.map{|l| l[0..1]}.uniq
available = req_langs.select{|l| valid_l(l)}
available.first unless available.empty?
end
def set_locale
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment