Rails I18n翻译范围 [英] Rails I18n translations scoping

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

问题描述

编写完全翻译的应用程序可能会变得乏味。有没有办法为当前上下文设置默认的翻译范围?

Writing a fully translated app can become tedious. Is there a way to set a default translation scope for the current context ?

示例:我在部分 _deadlines.html.erb 中编写在我的 ProjectsController

Example : I am writing inside a partial _deadlines.html.erb in the show.html.erb action of my ProjectsController

现在的 show.html.erb 操作中为了成为一名优秀的程序员,我对所有翻译进行了范围界定。我想生成以下树

Now because I am trying to be a good programmer, I am scoping all my translations. I would like to produce the following tree

projects:
  deadlines:
    now: "Hurry the deadline is today !"
    ....

我怎样才能使它比每次完整的书本写得那么乏味范围?

How can I make it less tedious than writing each time the full scope ?

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 文件)?

Is there a way to set a default scope for the current context (here the whole _deadlines.html.erb file) ?

编辑

有些人们建议使用Rails 惰性查找,但这不会产生作用域I'我在寻找。就我而言,我想跳过 action 默认范围(显示,索引等),并为我正在渲染的当前部分添加一个范围(就我而言) _deadlines.html.erb)

Some people suggested to use Rails Lazy lookup, but this does not produce the scoping I'm looking for. In my case, I want to skip the action default scope (show, index, etc...) and add a scope for the current partial I am rendering (in my case _deadlines.html.erb)

Rails惰性查找:

Rails lazy lookup :

t('.now')
<=> t(:now, scope: [:projects, :show]

但是我想要:

t('.now')
<=> t(:now, scope: [:projects, :deadlines]


推荐答案

好吧,我实际上对此仍然不满意。当您要在不同位置翻译相同内容时,默认的懒惰查找范围完全是废话。假设我有两个不同的部分,其中包含处理同一模型的信息。使用懒惰查找,我需要两次进行相同的翻译在我的yml文件中。

Okay I was actuall still not happy with this. This default 'lazy lookup' scope is totally krap when you want to translate the same thing at different places. Say I have two different partials that contain information dealing with the same model. Using lazy lookup, I would need to have the same translation twice in my yml file.

这是一小段代码,您可以在应用程序助手中放入它。它基本上是对默认I18n.t的覆盖,它将设置范围定义为 @t_scope 时,您就不必担心范围了

Here's a little piece of code that you can put in your application helper. It's basically an override of the default I18n.t that will set the scope to @t_scope when it is defined, and you don't need to worry about the scope anymore

我的代码添加

helpers / 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

您可以使用它做什么

projects / 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 %>
...

views / projects / _tasks.html.erb

<%= t_scope([:projects, :tasks]) %>
  <% tasks.each do | task| %>
  <h2><%= t(:person_in_charge) %></h2>
  ...
<% pop_t_scope %>  

en.yml

en:
  projects:
    deadlines:
      hurry: "Hurry man !"
      you_have_time: "Relax, there's still time"
    tasks:
      person_in_charge: 'The Boss is %{name}'

现在我看到的唯一问题是,当从视图渲染多个部分时,@ t_scope将被传输并且可能会引起问题。但是,将@t_scope在每个文件的开头设置为nil不会有问题

Now the only problem that I see, is that when rendering multiple partials from a view, the @t_scope will be transferred and could potentiall cause problems. However wouldn't be a problem is @t_scope is set to nil at the beginning of each file

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

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