Cyr*_*ris 5 ruby scope ruby-on-rails erb rails-i18n
编写一个完全翻译的应用程序可能会变得乏味。有没有办法为当前上下文设置默认翻译范围?
示例:我在我的show.html.erb操作中的部分_deadlines.html.erb中写入ProjectsController
现在,因为我正在努力成为一名优秀的程序员,所以我正在确定我所有的翻译。我想生成以下树
projects:
deadlines:
now: "Hurry the deadline is today !"
....
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它比每次写全范围更不乏味?
项目/show.html.erb
...
<%= render 'projects/deadlines', project: @project %>
...
Run Code Online (Sandbox Code Playgroud)
从show.html.erb调用的projects/ _deadlines.html.erb
<p>Deadline : <%= t(:now, scope: [:projects, :deadlines]) %></p>
Run Code Online (Sandbox Code Playgroud)
有没有办法为当前上下文设置默认范围(这里是整个_deadlines.html.erb文件)?
编辑
有些人建议使用 Rails Lazy lookup,但这不会产生我正在寻找的范围。就我而言,我想跳过action
默认范围(显示、索引等...)并为我正在渲染的当前部分添加一个范围(在我的情况下为 _deadlines.html.erb)
Rails 懒惰查找:
t('.now')
<=> t(:now, scope: [:projects, :show]
Run Code Online (Sandbox Code Playgroud)
但我想要:
t('.now')
<=> t(:now, scope: [:projects, :deadlines]
Run Code Online (Sandbox Code Playgroud)
Rails 实现了一种在视图中查找语言环境的便捷方法。当您有以下字典时:
es:
projects:
index: # in 'index.html.erb' template file
title: "Título"
deadlines: # in '_deadlines.html.erb' partial file
title: "Fecha límite"
Run Code Online (Sandbox Code Playgroud)
您可以按如下方式查找这些值:
# app/views/projects/index.html.erb
<%= t '.title' %> # => "Título"
# app/views/projects/_deadlines.html.erb
<%= t '.title' %> # => "Fecha límite"
Run Code Online (Sandbox Code Playgroud)
好吧,我实际上对此仍然不满意。当你想在不同的地方翻译相同的东西时,这个默认的“惰性查找”范围完全是错误的。假设我有两个不同的部分,其中包含处理同一模型的信息。使用惰性查找,我需要在 yml 文件中进行两次相同的翻译。
这里有一小段代码,您可以将其放入应用程序助手中。它基本上是默认 I18n.t 的覆盖,将范围设置为@t_scope
定义时的范围,您无需再担心范围
我的代码添加
助手/application_helper.rb
def t(*args)
# If there is just one param and we have defined a translation scope in the view
# only using symbols right now, need extended version to handle strings
if args.size == 1 and args.first.is_a?(Symbol) and @t_scope
super(args.shift, @t_scope)
else
super(*args)
end
end
def set_t_scope(scope)
push_t_scope(@t_scope ||= {})
replace_t_scope(scope)
end
alias :t_scope :set_t_scope
def replace_t_scope(scope)
@t_scope = {scope: scope}
end
def push_t_scope(scope)
(@tscope_stack ||= []) << scope
end
def pop_t_scope
@t_scope = @tscope_stack.pop
end
Run Code Online (Sandbox Code Playgroud)
你可以用它做什么
项目/show.html.erb
<%= t_scope([:projects, :deadlines]) %>
<fieldset>
<legend>Deadlines</legend>
<% if Time.now > @project.deadline.expected_finish_date %>
<p><%= t(:hurry) %></p>
<% else %>
<p><%= t(:you_have_time) %>
</fieldset>
<fieldset>
<legend>Deadlines</legend>
<%= render 'tasks', tasks: @project.tasks %>
...
Run Code Online (Sandbox Code Playgroud)
视图/项目/_tasks.html.erb
<%= t_scope([:projects, :tasks]) %>
<% tasks.each do | task| %>
<h2><%= t(:person_in_charge) %></h2>
...
<% pop_t_scope %>
Run Code Online (Sandbox Code Playgroud)
en.yml
en:
projects:
deadlines:
hurry: "Hurry man !"
you_have_time: "Relax, there's still time"
tasks:
person_in_charge: 'The Boss is %{name}'
Run Code Online (Sandbox Code Playgroud)
现在我看到的唯一问题是,当从视图渲染多个部分时,@t_scope 将被传输,并可能导致问题。不过,在每个文件的开头将 @t_scope 设置为 nil 不会有问题