geocoder gem and locale by request location or browser HTTP_accept_language

jamesperet 10 anni fa
parent
commit
2599c942eb
5 ha cambiato i file con 24 aggiunte e 18 eliminazioni
  1. 1 1
      Gemfile
  2. 2 4
      Gemfile.lock
  3. 0 1
      app/assets/javascripts/application.js
  4. 20 12
      app/controllers/application_controller.rb
  5. 1 0
      app/views/layouts/landing_page.erb

+ 1 - 1
Gemfile

@@ -64,7 +64,7 @@ gem 'gibbon'
64 64
 gem 'redis'
65 65
 gem 'resque', '~> 1.22.0', :require => "resque/server"
66 66
 gem 'mixpanel-ruby'
67
-gem "jquery-turbolinks"
67
+gem 'geocoder'
68 68
 
69 69
 group :development do
70 70
   gem "rename"

+ 2 - 4
Gemfile.lock

@@ -119,6 +119,7 @@ GEM
119 119
     formatador (0.2.5)
120 120
     friendly_id (5.0.4)
121 121
       activerecord (>= 4.0.0)
122
+    geocoder (1.2.6)
122 123
     gherkin (2.12.2)
123 124
       multi_json (~> 1.3)
124 125
     gibbon (1.1.4)
@@ -138,9 +139,6 @@ GEM
138 139
     jquery-rails (3.1.2)
139 140
       railties (>= 3.0, < 5.0)
140 141
       thor (>= 0.14, < 2.0)
141
-    jquery-turbolinks (2.1.0)
142
-      railties (>= 3.1.0)
143
-      turbolinks
144 142
     json (1.8.1)
145 143
     kgio (2.9.2)
146 144
     launchy (2.4.2)
@@ -317,11 +315,11 @@ DEPENDENCIES
317 315
   fog
318 316
   font-awesome-rails
319 317
   friendly_id (~> 5.0.0)
318
+  geocoder
320 319
   gibbon
321 320
   jasny_bootstrap_extension_rails
322 321
   jbuilder (~> 1.2)
323 322
   jquery-rails
324
-  jquery-turbolinks
325 323
   less-rails
326 324
   letter_opener
327 325
   mini_magick

+ 0 - 1
app/assets/javascripts/application.js

@@ -11,7 +11,6 @@
11 11
 // about supported directives.
12 12
 //
13 13
 //= require jquery
14
-//= require jquery.turbolinks
15 14
 //= require jquery_ujs
16 15
 //= require twitter/bootstrap
17 16
 //= require bootstrap-switch

+ 20 - 12
app/controllers/application_controller.rb

@@ -17,22 +17,30 @@ class ApplicationController < ActionController::Base
17 17
     devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :email, :current_password, :avatar, :password, :password_confirmation, :avatar_tmp, :avatar_processing) }
18 18
     devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password) }
19 19
   end
20
-    
21
-  def config_site  
22
-    @config = Info.first
23
-    #I18n.config.enforce_available_locales = false
24
-    I18n.locale = @config.default_language
25
-  end
26 20
  
27 21
   def config_site
28 22
     @config = Info.first
29 23
     if cookies[:avalanche_locale] && I18n.available_locales.include?(cookies[:avalanche_locale].to_sym)
30
-      l = cookies[:avalanche_locale].to_sym
31
-    else
32
-      l = @config.default_language
33
-      cookies.permanent[:avalanche_locale] = l
34
-    end
35
-    I18n.locale = l
24
+        l = cookies[:avalanche_locale].to_sym
25
+      else
26
+        begin
27
+          country_code = request.location.country_code
28
+          http_accept_language = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
29
+          if http_accept_language != ''
30
+            [:"pt-BR", :pt, :br].include?(http_accept_language) ? l = :"pt-BR" : l = @config.default_language
31
+          elsif country_code != ''
32
+            country_code = country_code.downcase.to_sym
33
+            [:"pt-BR", :pt, :br].include?(country_code) ? l = :"pt-BR" : l = @config.default_language
34
+          else
35
+            l = @config.default_language # use default locale if cannot retrieve this info
36
+          end
37
+        rescue
38
+          l = @config.default_language
39
+        ensure
40
+          cookies.permanent[:avalanche_locale] = l
41
+        end
42
+      end
43
+      I18n.locale = l
36 44
   end
37 45
   
38 46
   def analytics

+ 1 - 0
app/views/layouts/landing_page.erb

@@ -65,6 +65,7 @@
65 65
 	              </li>
66 66
 	              <li>
67 67
 	                <%= link_to t('menu.languages.pt-BR'), change_locale_path(:"pt-BR") %>
68
+				 
68 69
 	              </li>
69 70
 	            </ul>
70 71
 	          </li>