AdamNYC
AdamNYC

Reputation: 20425

How to invoke flash messages defined in locales

I have a message called my_message in config/locales/post.en.yml as follow:

en:
  post:
    show:
      my_message: "Post was successfully saved. And Boom!"

How do I call this :my_message for a flash in a controller's method?

class PostsController < ApplicationController
  def show
     flash[:error] = my_message
  end
end

Upvotes: 0

Views: 466

Answers (2)

Rajarshi Das
Rajarshi Das

Reputation: 12340

In application controller rails 4 send your locale in parameters so that it get it in contoller by params[:locale]

 before_action :set_locale  

 def set_locale  
   I18n.locale = params[:locale] || I18n.default_locale
 end

Then

flash[:error] = t('post.show.my_message')

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230521

Try the I18n.t helper

flash[:error] = t('.my_message')

Or, if that doesn't work, use full path:

flash[:error] = t('post.show.my_message')

Upvotes: 2

Related Questions