Change language immediately after updating settings
authorJ Guthrie <jamie.guthrie@gmail.com>
Sun, 27 May 2018 14:12:34 +0000 (15:12 +0100)
committerTom Hughes <tom@compton.nu>
Sun, 27 May 2018 14:12:34 +0000 (15:12 +0100)
Closes #1883

app/controllers/application_controller.rb
app/controllers/user_controller.rb
app/models/user.rb

index 1625c81c15d4d188049c7307bbbaef8e1ca15f3b..3afb17f470afb5668a2c2be34e60696f4c57f952 100644 (file)
@@ -295,7 +295,8 @@ class ApplicationController < ActionController::Base
     end
   end
 
-  def preferred_languages
+  def preferred_languages(reset = false)
+    @preferred_languages = nil if reset
     @preferred_languages ||= if params[:locale]
                                Locale.list(params[:locale])
                              elsif current_user
@@ -307,13 +308,13 @@ class ApplicationController < ActionController::Base
 
   helper_method :preferred_languages
 
-  def set_locale
+  def set_locale(reset = false)
     if current_user && current_user.languages.empty? && !http_accept_language.user_preferred_languages.empty?
       current_user.languages = http_accept_language.user_preferred_languages
       current_user.save
     end
 
-    I18n.locale = Locale.available.preferred(preferred_languages)
+    I18n.locale = Locale.available.preferred(preferred_languages(reset))
 
     response.headers["Vary"] = "Accept-Language"
     response.headers["Content-Language"] = I18n.locale.to_s
index 54e2fa9c0bff45d3dffafba422bd74e81b77064e..63fad8c8378aa7f210ebe90b040badac88a3d0b1 100644 (file)
@@ -118,7 +118,6 @@ class UserController < ApplicationController
   end
 
   def account
-    @title = t "user.account.title"
     @tokens = current_user.oauth_tokens.authorized
 
     if params[:user] && params[:user][:display_name] && params[:user][:description]
@@ -135,6 +134,7 @@ class UserController < ApplicationController
         current_user.errors.add(attribute, error)
       end
     end
+    @title = t "user.account.title"
   end
 
   def go_public
@@ -712,7 +712,7 @@ class UserController < ApplicationController
     end
 
     if user.save
-      set_locale
+      set_locale(true)
 
       if user.new_email.blank? || user.new_email == user.email
         flash.now[:notice] = t "user.account.flash update success"
index da3964142fb60a3c65e4bca7704a0b72a9eca2c5..036f2fdc478692bc4f52b17ea8ed8f8aaf42d1f1 100644 (file)
@@ -112,6 +112,7 @@ class User < ActiveRecord::Base
   before_save :encrypt_password
   before_save :update_tile
   after_save :spam_check
+  after_save :reset_preferred_languages
 
   def to_param
     display_name
@@ -192,6 +193,10 @@ class User < ActiveRecord::Base
     @preferred_languages ||= Locale.list(languages)
   end
 
+  def reset_preferred_languages
+    @preferred_languages = nil
+  end
+
   def nearby(radius = NEARBY_RADIUS, num = NEARBY_USERS)
     if home_lon && home_lat
       gc = OSM::GreatCircle.new(home_lat, home_lon)