From 685a35524a6e4f91155008e9850049feff298593 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Mon, 4 Apr 2011 22:44:43 +0100 Subject: [PATCH] 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. --- app/controllers/application_controller.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 34987c938..926be87ba 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 -- 2.43.2