Rails I18n in validation .rb verification method not working?

I use the internationnalization I18n plugin, but not translating 1 part of the information:

In one of my controllers, I have a verification method as follows:

  # Verify user is authenticated
  verify :only => [ :destroy, :create, :update, :new, :comment ],
     :session => :user_id,
     :add_flash => { :error => I18n.t(:'Exceptions.not_logged_in') },
     :redirect_to => { :controller => 'main' , :action => 'index' }

However, using I18n.t (: 'Exceptions.not_logged_in') always displays default_locale, in this case, English.

In my application, Application_Controller has a before_filter parameter that sets the locale.

Can someone help me understand and help me find a workaround?

Thank!

PS: I tried to add a call to set_locale before this validation method without success (in my controller)

+2
source share
4 answers

verify - , , , I18n.t, . I18n.t , . verify .

before:

before_filter :verify_session, :only => [:destroy, :create, :update, :new, :comment]

def verify_session
  unless session[:user_id]
    flash[:error] = I18n.t('Exceptions.not_logged_in')
    redirect_to :controller => 'main', :action => 'index'
  end
end

, verify Rails 3, .

+4

; , . FlashHash , , :

verify :only => [ :destroy, :create, :update, :new, :comment ],
     :session => :user_id,
     :add_flash => { :error => 'Exceptions.not_logged_in' },
     :redirect_to => { :controller => 'main' , :action => 'index' }

, ERB - :

<% if flash[:error] %>
    <p class="error"><%= I18n.t(flash[:error]) %></p>
<% end %>

REST JavaScript, :

if((flash.error || '') == 'Exceptions.not_logged_in')
    sammy.setLocation('#/sign_in');

, , - , , .

+1

I18n.t parens

add_flash => { :error => (I18n.t(:'Exceptions.not_logged_in')) },
0

I think this may have something to do with the fact that the I18n.t call is launched only once, when the file is initially loaded when the application starts. You can try wrapping it in Proc or lambda, or perhaps in a separate method. for example something like:

:add_flash => lambda { { :error => I18n.t(:'Exceptions.not_logged_in') } }

I don't know if this will work, but it may come closer to you.

-1
source

Source: https://habr.com/ru/post/1793731/


All Articles