Try and patch up poor browser language selections

The HTTP RFC says that a language range specified by a browser only
matches a resource whose language tag is the same, or longer. This
means that a browser language range of de-DE will not match a resource
with a language tag of de.

Because of this browsers should always send the generic range, such
as de, as a secondary choice after a more specific range like de-DE.

Some browers don't this however, so if we don't get a language match
we try and patch up the list of language ranges by inserting the
more generic ranges.
This commit is contained in:
Tom Hughes 2011-04-04 22:44:43 +01:00
parent c39f688ae7
commit 685a35524a

View file

@ -199,6 +199,24 @@ class ApplicationController < ActionController::Base
end
end
if request.compatible_language_from(I18n.available_locales).nil?
request.user_preferred_languages = request.user_preferred_languages.collect do |pl|
pls = [ pl ]
while pl.match(/^(.*)-[^-]+$/)
pls.push($1) if I18n.available_locales.include?($1.to_sym)
pl = $1
end
pls
end.flatten
if @user and not request.compatible_language_from(I18n.available_locales).nil?
@user.languages = request.user_preferred_languages
@user.save
end
end
I18n.locale = request.compatible_language_from(I18n.available_locales)
response.headers['Content-Language'] = I18n.locale.to_s