跳到内容 跳到搜索
方法
L
T

实例公共方法

l(object, **options)

别名:localize

localize(object, **options)

委托给 I18n.localize

还别名为:l
# File actionpack/lib/abstract_controller/translation.rb, line 36
def localize(object, **options)
  I18n.localize(object, **options)
end

t(key, **options)

别名:translate

translate(key, **options)

委托给 I18n.translate

当给定的键以句点开头时,它将由当前控制器和操作限定。因此,如果你从 PeopleController#index 调用 translate(".foo"),它将把调用转换为 I18n.translate("people.index.foo")。这使得在同一控制器/操作中翻译多个键时不会重复,并为你提供了一个简单框架,以便一致地限定它们。

还别名为:t
# File actionpack/lib/abstract_controller/translation.rb, line 15
def translate(key, **options)
  if key&.start_with?(".")
    path = controller_path.tr("/", ".")
    defaults = [:"#{path}#{key}"]
    defaults << options[:default] if options[:default]
    options[:default] = defaults.flatten
    key = "#{path}.#{action_name}#{key}"
  end

  if options[:default]
    options[:default] = [options[:default]] unless options[:default].is_a?(Array)
    options[:default] = options[:default].map do |value|
      value.is_a?(String) ? ERB::Util.html_escape(value) : value
    end
  end

  ActiveSupport::HtmlSafeTranslation.translate(key, **options)
end