Commit 34e900f2 authored by's avatar
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)
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]
return nil if req_langs.empty?
req_langs = req_langs.sort_by(&:last).map(&:first).reverse
req_langs ={|l| l[0..1]}.uniq
available ={|l| valid_l(l)}
available.first unless available.empty?
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