Writing a fully translated app can become tedious. Is there a way to set a default translation scope for the current context ?
Example : I am writing inside a partial _deadlines.html.erb in the show.html.erb action of my ProjectsController
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 called from show.html.erb
<p>Deadline : <%= t(:now, scope: [:projects, :deadlines]) %></p>
Is there a way to set a default scope for the current context (here the whole _deadlines.html.erb file) ?
EDIT
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 lazy lookup :
t('.now')
<=> t(:now, scope: [:projects, :show]
But I wanted :
t('.now')
<=> t(:now, scope: [:projects, :deadlines]
Rails implements a convenient way to look up the locale inside views. When you have the following dictionary:
you can look up these values as below:
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.
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 anymoreMy code addition
helpers/application_helper.rb
What you can do with it
projects/show.html.erb
views/projects/_tasks.html.erb
en.yml
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