]> git.openstreetmap.org Git - rails.git/commitdiff
Try and patch up poor browser language selections
authorTom Hughes <tom@compton.nu>
Mon, 4 Apr 2011 21:44:43 +0000 (22:44 +0100)
committerTom Hughes <tom@compton.nu>
Mon, 4 Apr 2011 21:44:43 +0000 (22:44 +0100)
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

index 34987c93880f87141084d5e7bca4c8d62e7f5c60..926be87ba3148183df3f463ca8e5f7986d71d563 100644 (file)
@@ -199,6 +199,24 @@ class ApplicationController < ActionController::Base
       end
     end
 
       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
     I18n.locale = request.compatible_language_from(I18n.available_locales)
 
     response.headers['Content-Language'] = I18n.locale.to_s