编写完全翻译的应用程序可能会变得乏味。有没有办法为当前上下文设置默认翻译范围?
示例:我在我的 ProjectsController
show.html.erb 操作中的部分 _deadlines.html.erb 中写入p>
现在因为我想成为一名优秀的程序员,所以我正在确定我所有翻译的范围。我想生成下面的树
projects:
deadlines:
now: "Hurry the deadline is today !"
....
我怎样才能让它比每次都写整个范围更不乏味?
projects/show.html.erb
...
<%= render 'projects/deadlines', project: @project %>
...
projects/_deadlines.html.erb 从 show.html.erb 调用
<p>Deadline : <%= t(:now, scope: [:projects, :deadlines]) %></p>
有没有办法为当前上下文设置默认范围(这里是整个 _deadlines.html.erb 文件)?
编辑
有些人建议使用 Rails Lazy lookup ,但这不会产生我正在寻找的范围。在我的例子中,我想跳过 action
默认范围(显示、索引等...)并为我正在渲染的当前部分添加一个范围(在我的例子中是 _deadlines.html.erb)
Rails 惰性查找:
t('.now')
<=> t(:now, scope: [:projects, :show]
但我想要:
t('.now')
<=> t(:now, scope: [:projects, :deadlines]
最佳答案
Rails 实现了一种在 View 中查找语言环境的简便方法。 当你有以下字典时:
es:
projects:
index: # in 'index.html.erb' template file
title: "Título"
deadlines: # in '_deadlines.html.erb' partial file
title: "Fecha límite"
您可以按如下方式查找这些值:
# app/views/projects/index.html.erb
<%= t '.title' %> # => "Título"
# app/views/projects/_deadlines.html.erb
<%= t '.title' %> # => "Fecha límite"
关于ruby-on-rails - Rails I18n 翻译范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28595913/