2013-08-07 42 views
0

我有一個在config/locales/post.en.yml稱爲my_message如下消息中定義的提示信息:在控制器的方法的閃存my_message:如何調用的語言環境

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

我怎麼叫呢?

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

嘗試'噸( 'post.show.my_message')'。 [Rails查找翻譯指南](http://guides.rubyonrails.org/i18n.html#looking-up-translations)。 –

+0

做出答案,我會刪除我的:) –

回答

1

在應用控制器軌道4,5發送您的區域設置參數,使它通過params [:locale]得到控制器

before_action :set_locale 

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

然後

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

2

嘗試I18n.t幫手

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

或者,如果不行,用全路徑:

flash[:error] = t('post.show.my_message') 
相關問題