gpt4 book ai didi

ruby-on-rails - i18n : t method not working

转载 作者:行者123 更新时间:2023-12-04 05:39:57 32 4
gpt4 key购买 nike

在我的 Rails 3.2.2 应用程序中,我尝试使用 i18n,但有些东西无法正常工作。

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

因此,例如:

t(:login)
=> login

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

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

谢谢,
奥古斯托

更新

我用 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方法是一个助手,因此只在 View 和 Controller 中可用。

如果您尝试从模型或 rails 控制台使用 I18n,您应该使用 I18n.t

关于ruby-on-rails - i18n : t method not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11724831/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com