Change language buttons and locale cookie

jamesperet 10 年之前
父节点
当前提交
1113889215

+ 2 - 1
Gemfile

@@ -58,12 +58,13 @@ gem "mini_magick"
58 58
 gem 'fog'
59 59
 gem "carrierwave"
60 60
 gem 'carrierwave_backgrounder'
61
-gem 'i18n'
61
+gem 'rails-i18n', '~> 4.0.0'
62 62
 gem 'rails_12factor', group: :production
63 63
 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 68
 
68 69
 group :development do
69 70
   gem "rename"

+ 8 - 1
Gemfile.lock

@@ -138,6 +138,9 @@ GEM
138 138
     jquery-rails (3.1.2)
139 139
       railties (>= 3.0, < 5.0)
140 140
       thor (>= 0.14, < 2.0)
141
+    jquery-turbolinks (2.1.0)
142
+      railties (>= 3.1.0)
143
+      turbolinks
141 144
     json (1.8.1)
142 145
     kgio (2.9.2)
143 146
     launchy (2.4.2)
@@ -183,6 +186,9 @@ GEM
183 186
       bundler (>= 1.3.0, < 2.0)
184 187
       railties (= 4.0.4)
185 188
       sprockets-rails (~> 2.0.0)
189
+    rails-i18n (4.0.3)
190
+      i18n (~> 0.6)
191
+      railties (~> 4.0)
186 192
     rails_12factor (0.0.2)
187 193
       rails_serve_static_assets
188 194
       rails_stdout_logging
@@ -312,16 +318,17 @@ DEPENDENCIES
312 318
   font-awesome-rails
313 319
   friendly_id (~> 5.0.0)
314 320
   gibbon
315
-  i18n
316 321
   jasny_bootstrap_extension_rails
317 322
   jbuilder (~> 1.2)
318 323
   jquery-rails
324
+  jquery-turbolinks
319 325
   less-rails
320 326
   letter_opener
321 327
   mini_magick
322 328
   mixpanel-ruby
323 329
   pg
324 330
   rails (= 4.0.4)
331
+  rails-i18n (~> 4.0.0)
325 332
   rails_12factor
326 333
   redcarpet
327 334
   redis

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

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

+ 11 - 0
app/controllers/application_controller.rb

@@ -23,6 +23,17 @@ class ApplicationController < ActionController::Base
23 23
     #I18n.config.enforce_available_locales = false
24 24
     I18n.locale = @config.default_language
25 25
   end
26
+ 
27
+  def config_site
28
+    @config = Info.first
29
+    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
36
+  end
26 37
   
27 38
   def analytics
28 39
     if current_user != nil

+ 10 - 0
app/controllers/settings_controller.rb

@@ -0,0 +1,10 @@
1
+class SettingsController < ApplicationController
2
+  
3
+  def change_locale
4
+    l = params[:locale].to_s.strip.to_sym
5
+    l = I18n.default_locale unless I18n.available_locales.include?(l)
6
+    cookies.permanent[:avalanche_locale] = l
7
+    redirect_to :back || root_url
8
+  end
9
+
10
+end

+ 32 - 12
app/views/layouts/landing_page.erb

@@ -40,7 +40,7 @@
40 40
     <script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.js'></script>
41 41
     <link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.4/mapbox.css' rel='stylesheet' />
42 42
   </head>
43
-  <body>
43
+  <body data-no-turbolink='true'>
44 44
 
45 45
     <nav class="navbar navbar-fixed-top">
46 46
       <div class="navbar-inner">
@@ -53,24 +53,44 @@
53 53
           <a class="nav-responsive-link btn-logo hidden-phone" href="/"><%= image_tag "logo_avalanche.png", class: 'brand', style: 'width: 250px;' %></a>
54 54
 		<a class="nav-responsive-link btn-logo visible-phone" href="/"><%= image_tag "logo_avalanche_mobile.png", class: 'brand', style: 'height: 36px;' %></a>
55 55
           
56
+	     <ul class="nav navbar-nav navbar-right pull-right">
57
+	          <li class="dropdown">
58
+	            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
59
+	              <%= t('menu.languages.lang') %>
60
+	              <span class="caret"></span>
61
+	            </a>
62
+	            <ul class="dropdown-menu" role="menu">
63
+	              <li>
64
+	                <%= link_to t('menu.languages.en'), change_locale_path(:en) %>
65
+	              </li>
66
+	              <li>
67
+	                <%= link_to t('menu.languages.pt-BR'), change_locale_path(:"pt-BR") %>
68
+	              </li>
69
+	            </ul>
70
+	          </li>
71
+	        </ul>
72
+		
56 73
 		<div id="close-responsive-menu" class="container nav-collapse ">
57 74
 		  <ul class="nav pull-right">
58 75
 		    <li><a class="nav-responsive-link btn-about" href="#about"><%= (t "landing_page.about").html_safe %></a></li>
59 76
 		    <li><a class="nav-responsive-link btn-features" href="#features"><%= (t "landing_page.features").html_safe %></a></li>
60 77
 		    <li><a class="nav-responsive-link btn-subscribe" href="#subscribe"><%= (t "landing_page.subscribe").html_safe %></a></li>
61
-		    <li><%= link_to (t "registration.sign_in"), new_user_session_path, class: "btn-login" %></li>
78
+		    <% registrations = false %>
79
+		    <% if registrations == true %>
80
+			    <li><%= link_to (t "registration.sign_in"), new_user_session_path, class: "btn-login" %></li>
62 81
 			    
63
-	   		<% if user_signed_in? %>
64
-				<% if current_user.admin %>
65
-					<li><%= link_to ('<i class="fa fa-cogs"></i>').html_safe, admin_dashboard_path %></li>
82
+		   		<% if user_signed_in? %>
83
+					<% if current_user.admin %>
84
+						<li><%= link_to ('<i class="fa fa-cogs"></i>').html_safe, admin_dashboard_path %></li>
85
+					<% end %>
86
+				<% end %>
87
+				<% if user_signed_in? %>
88
+		 			<li><%= link_to ('<i class="fa fa-user"></i>').html_safe, edit_user_registration_path %></li>
89
+		 			<li><%= link_to ('<i class="fa fa-sign-out"></i>').html_safe, destroy_user_session_path, method: :delete %></li>
90
+				<% else %>
91
+					<li><% link_to ('<i class="fa fa-sign-in"></i>').html_safe, new_user_session_path  %></li>
92
+					<li><% link_to (t "nav.signup"), new_user_registration_path  %></li>
66 93
 				<% end %>
67
-			<% end %>
68
-			<% if user_signed_in? %>
69
-	 			<li><%= link_to ('<i class="fa fa-user"></i>').html_safe, edit_user_registration_path %></li>
70
-	 			<li><%= link_to ('<i class="fa fa-sign-out"></i>').html_safe, destroy_user_session_path, method: :delete %></li>
71
-			<% else %>
72
-				<li><% link_to ('<i class="fa fa-sign-in"></i>').html_safe, new_user_session_path  %></li>
73
-				<li><% link_to (t "nav.signup"), new_user_registration_path  %></li>
74 94
 			<% end %>
75 95
 			
76 96
 		</ul>

+ 3 - 2
config/application.rb

@@ -15,10 +15,11 @@ module Avalanche2
15 15
 
16 16
     # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
17 17
     # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
18
-    # config.time_zone = 'Central Time (US & Canada)'
18
+    config.time_zone = 'Central Time (US & Canada)'
19 19
 
20 20
     # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
21 21
     # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
22
-    # config.i18n.default_locale = :de
22
+    config.i18n.default_locale = :en
23
+    I18n.available_locales = [:en, :"pt-BR"]
23 24
   end
24 25
 end

+ 2 - 0
config/routes.rb

@@ -2,6 +2,8 @@ Avalanche2::Application.routes.draw do
2 2
   
3 3
   root 'start#index'
4 4
   
5
+  get '/change_locale/:locale', to: 'settings#change_locale', as: :change_locale
6
+  
5 7
   mount Resque::Server.new, :at => "/resque" 
6 8
   
7 9
   get "subscription/create"