Rails/I18n:默认范围 [英] Rails / I18n: default scope

查看:71
本文介绍了Rails/I18n:默认范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails的默认I18n模块在视图中转换我的字符串.

I'm using the default I18n module for Rails to translate my strings in views.

<%= t("registration.heading") %>

现在,当我进入注册视图时,我所有的字符串都以registration开头.我总是要写

Now, when I'm in the registration-view, all my strings start with registration. I always have to write

<%= t("registration.heading.notice") %>
// or
<%= t(:heading, :scope => :registration) %>

最好为该文件定义默认范围(甚至在控制器中),因此调用t会自动添加定义的范围

It would be nice to define a default scope for that file (maybe even in the controller), so a call to t automatically adds the defined scope

// controller
set_i18n_default_scope :registration

// view
<%= t(:heading) %>

// --> looks in "registration.heading"

这可能吗?

推荐答案

如果您在组织翻译时添加视图名称,例如:

If you organize your translations adding a view name, as in:

en:
  registration:
    index:
      heading: "Registration heading"

那么您可以使用此:

<%= t(".heading") %>

请注意,第一个字符是点.

Notice that the first character is a dot.

您可以在 Rails国际化(I18n)API指南

如果您的文本在众多视图中共享,并且您不想在每个部分的每个部分中复制相同的译文,则可以使用 YAML参考.在Wikipedia上对它们进行了很好的描述: http://en.wikipedia.org/wiki/YAML#Repeated_nodes

If you have texts which are shared amongst numerous views, and you don't want to copy the same translation in each section for each view, you may use YAML references. They are nicely described on wikipedia: http://en.wikipedia.org/wiki/YAML#Repeated_nodes

这篇关于Rails/I18n:默认范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆