Search code examples
ruby-on-railslocale

Cannot access translate value in Class files (Rails)


I am using locale file to save the English text, and I am using these text in controller by calling it like t(:user_created_message).

But when I moved my logic to a class file under /lib/classes/users/user.rb and tried to access the translate, it shows error like undefined method t for Users::User

How should I load the translate variable when calling from custom classes?


Solution

  • I18n.t :user_created_message
    

    Please read the documentation here, the method you're using only works inside the views.