Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i18n: t method not working

in my Rails 3.2.2 app I'm trying to use i18n but something is not working correctly.

In fact the "t" method does not work, only "i18n.t" works.

So, for example:

t(:login)
=> login

Instead:

i18n.t(:login)
=> Provide the necessary login info

Can you help me to figure out what I'm doing wrong?

Thanks, Augusto

UPDATE

I used pry to show the source for the t helper and got this:

From: /Users/phishman/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.2/lib/action_view/helpers/translation_helper.rb @ line 46: Number of lines: 16 Owner: ActionView::Helpers::TranslationHelper Visibility: public

    def translate(key, options = {})
      options.merge!(:rescue_format => :html) unless options.key?(:rescue_format)
      if html_safe_translation_key?(key)
        html_safe_options = options.dup
        options.except(*I18n::RESERVED_KEYS).each do |name, value|
          unless name == :count && value.is_a?(Numeric)
            html_safe_options[name] = ERB::Util.html_escape(value.to_s)
          end
        end
        translation = I18n.translate(scope_key_by_partial(key), html_safe_options)

        translation.respond_to?(:html_safe) ? translation.html_safe : translation
      else
        I18n.translate(scope_key_by_partial(key), options)
      end
    end

3] pry(main)> show-source helper.t

From: /Users/phishman/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.2/lib/action_view/helpers/translation_helper.rb @ line 46:
Number of lines: 16
Owner: ActionView::Helpers::TranslationHelper
Visibility: public

def translate(key, options = {})
  options.merge!(:rescue_format => :html) unless options.key?(:rescue_format)
  if html_safe_translation_key?(key)
    html_safe_options = options.dup
    options.except(*I18n::RESERVED_KEYS).each do |name, value|
      unless name == :count && value.is_a?(Numeric)
        html_safe_options[name] = ERB::Util.html_escape(value.to_s)
      end
    end
    translation = I18n.translate(scope_key_by_partial(key), html_safe_options)

    translation.respond_to?(:html_safe) ? translation.html_safe : translation
  else
    I18n.translate(scope_key_by_partial(key), options)
  end
end
like image 318
Augusto Avatar asked Jul 30 '12 15:07

Augusto


1 Answers

the t method is a helper and therefore only available in views and controllers.

If you try to use I18n from models or the rails console, you should use I18n.t

like image 181
Anthony Alberto Avatar answered Oct 13 '22 21:10

Anthony Alberto