加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – i18n:t方法不起作用

发布时间:2020-12-17 04:36:11 所属栏目:百科 来源:网络整理
导读:在我的Rails 3.2.2应用程序中,我正在尝试使用i18n,但有些东西无法正常工作. 事实上,“t”方法不起作用,只有“i18n.t”有效. 所以,例如: t(:login)= login 代替: i18n.t(:login)= Provide the necessary login info 你能帮我搞清楚我做错了什么吗? 谢谢,
在我的Rails 3.2.2应用程序中,我正在尝试使用i18n,但有些东西无法正常工作.

事实上,“t”方法不起作用,只有“i18n.t”有效.

所以,例如:

t(:login)
=> login

代替:

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

你能帮我搞清楚我做错了什么吗?

谢谢,
奥古斯托

UPDATE

我用pry来显示t帮助器的来源并得到了这个:

来自:/Users/phishman/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.2/lib/action_view/helpers/translation_helper.rb @第46行:
行数:16
所有者:ActionView :: Helpers :: TranslationHelper
可见性:公开

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

解决方法

t方法是一个帮助程序,因此仅在视图和控制器中可用.

如果您尝试使用模型或rails控制台中的I18n,则应使用I18n.t

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读