Upgrade to http_accept_language 2.x
authorTom Hughes <tom@compton.nu>
Tue, 24 Sep 2013 19:22:44 +0000 (20:22 +0100)
committerTom Hughes <tom@compton.nu>
Tue, 24 Sep 2013 19:22:44 +0000 (20:22 +0100)
Gemfile
Gemfile.lock
app/controllers/amf_controller.rb
app/controllers/application_controller.rb
app/controllers/geocoder_controller.rb
app/controllers/user_controller.rb
config/initializers/http_accept_language.rb
lib/nominatim.rb

diff --git a/Gemfile b/Gemfile
index 6b2fe4970a7ca3a36d44b146fdde5857d6618054..c1ca5fa353bf3ea8a3623e8a3bfebb667949aceb 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -45,7 +45,7 @@ gem 'oauth-plugin', '>= 0.5.1'
 gem 'open_id_authentication', '>= 1.1.0'
 gem 'validates_email_format_of', '>= 1.5.1'
 gem 'composite_primary_keys', '>= 6.0.0'
-gem 'http_accept_language', '~> 1.0.2'
+gem 'http_accept_language', '~> 2.0.0'
 gem 'paperclip', '~> 3.0'
 gem 'deadlock_retry', '>= 1.2.0'
 gem 'openstreetmap-i18n-js', '>= 3.0.0.rc5.2', :require => 'i18n-js'
index 9c3460671861f88a24f92915565384737e78822f..51936b05ff4ab61e364becbdac1fe519717aeeb0 100644 (file)
@@ -55,7 +55,7 @@ GEM
       multipart-post (~> 1.2.0)
     hike (1.2.3)
     htmlentities (4.3.1)
-    http_accept_language (1.0.2)
+    http_accept_language (2.0.0)
     httpauth (0.2.0)
     httpclient (2.3.4.1)
     i18n (0.6.5)
@@ -182,7 +182,7 @@ DEPENDENCIES
   dynamic_form
   ejs
   htmlentities
-  http_accept_language (~> 1.0.2)
+  http_accept_language (~> 2.0.0)
   httpclient
   iconv (= 0.1)
   jquery-rails
index ef7a636390de6df9ecea484bd6331ea91af66749..13372273c2eba0f940203bee76ff8964e2f3d279 100644 (file)
@@ -185,10 +185,10 @@ class AmfController < ApplicationController
     user = getuser(usertoken)
 
     if user && !user.languages.empty?
-      request.user_preferred_languages = user.languages
+      http_accept_language.user_preferred_languages = user.languages
     end
 
-    lang = request.compatible_language_from(getlocales)
+    lang = http_accept_language.compatible_language_from(getlocales)
     (real_lang, localised) = getlocalized(lang)
 
     # Tell Potlatch what language it's using
index aec6120808d8f202e4dcf7e6ed231e1ac431e568..12cdb15d4ebbc85c19bdcc353df7900044ec0427 100644 (file)
@@ -284,14 +284,14 @@ class ApplicationController < ActionController::Base
     response.header['Vary'] = 'Accept-Language'
 
     if @user && !@user.languages.empty?
-      request.user_preferred_languages = @user.languages
+      http_accept_language.user_preferred_languages = @user.languages
       response.header['Vary'] = '*'
     end
 
     I18n.locale = select_locale
 
-    if @user && @user.languages.empty? && !request.user_preferred_languages.empty?
-      @user.languages = request.user_preferred_languages
+    if @user && @user.languages.empty? && !http_accept_language.user_preferred_languages.empty?
+      @user.languages = http_accept_language.user_preferred_languages
       @user.save
     end
 
@@ -300,11 +300,11 @@ class ApplicationController < ActionController::Base
 
   def select_locale(locales = I18n.available_locales)
     if params[:locale]
-      request.user_preferred_languages = [ params[:locale] ]
+      http_accept_language.user_preferred_languages = [ params[:locale] ]
     end
 
-    if request.compatible_language_from(locales).nil?
-      request.user_preferred_languages = request.user_preferred_languages.collect do |pl|
+    if http_accept_language.compatible_language_from(locales).nil?
+      http_accept_language.user_preferred_languages = http_accept_language.user_preferred_languages.collect do |pl|
         pls = [ pl ]
 
         while pl.match(/^(.*)-[^-]+$/)
@@ -316,7 +316,7 @@ class ApplicationController < ActionController::Base
       end.flatten
     end
 
-    request.compatible_language_from(locales) || I18n.default_locale
+    http_accept_language.compatible_language_from(locales) || I18n.default_locale
   end
 
   helper_method :select_locale
index 8f02ce51d2b1a3739804277fc130deab25f61644..8d29ad505828b345a085a90fdd362877a3a05092 100644 (file)
@@ -154,7 +154,7 @@ class GeocoderController < ApplicationController
     end
 
     # ask nominatim
-    response = fetch_xml("#{NOMINATIM_URL}search?format=xml&q=#{escape_query(query)}#{viewbox}#{exclude}&accept-language=#{request.user_preferred_languages.join(',')}")
+    response = fetch_xml("#{NOMINATIM_URL}search?format=xml&q=#{escape_query(query)}#{viewbox}#{exclude}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}")
 
     # create result array
     @results = Array.new
@@ -241,7 +241,7 @@ class GeocoderController < ApplicationController
     @results = Array.new
 
     # ask nominatim
-    response = fetch_xml("#{NOMINATIM_URL}reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{request.user_preferred_languages.join(',')}")
+    response = fetch_xml("#{NOMINATIM_URL}reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}")
 
     # parse the response
     response.elements.each("reversegeocode/result") do |result|
index 7a0fa5f272d324fed0730b5aec107df09a4f1d4a..325d7b3fe380ae4dd37f4c6be0e8e18c5cfae44d 100644 (file)
@@ -79,7 +79,7 @@ class UserController < ApplicationController
         @user.data_public = true
         @user.description = "" if @user.description.nil?
         @user.creation_ip = request.remote_ip
-        @user.languages = request.user_preferred_languages
+        @user.languages = http_accept_language.user_preferred_languages
         @user.terms_agreed = Time.now.getutc
         @user.terms_seen = true
         @user.openid_url = nil if @user.openid_url and @user.openid_url.empty?
index 04c3dfaf53395b544d2b589ba9aa6dee97c671cb..2e5c92df1aca1a58de7b3a0c847c2fa8dd046f80 100644 (file)
@@ -3,10 +3,12 @@
 # https://github.com/iain/http_accept_language/pull/6
 #
 module HttpAcceptLanguage
-  def compatible_language_from(available_languages)
-    user_preferred_languages.find do |x|
-      available_languages.find { |y| y.to_s == x.to_s } ||
-      available_languages.find { |y| y.to_s =~ /^#{Regexp.escape(x.to_s)}-/ }
+  class Parser
+    def compatible_language_from(available_languages)
+      user_preferred_languages.find do |x|
+        available_languages.find { |y| y.to_s == x.to_s } ||
+          available_languages.find { |y| y.to_s =~ /^#{Regexp.escape(x.to_s)}-/ }
+      end
     end
   end
 end
index 1d2037d8d171c30badf2a07feb5b7d5ceb92eafa..33a0c3e26ac04e7297355414a29916effb4f2871 100644 (file)
@@ -3,7 +3,7 @@ module Nominatim
 
   def self.describe_location(lat, lon, zoom = nil, language = nil)
     zoom = zoom || 14
-    language = language || request.user_preferred_languages.join(',')
+    language = language || http_accept_language.user_preferred_languages.join(',')
 
     Rails.cache.fetch "/nominatim/location/#{lat}/#{lon}/#{zoom}/#{language}" do
       url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"