Use helper methods inside a locales file?

902 Views Asked by At

I'm trying to get Gitorious up and running using Rails 3, but I've come a cross a problem.

I've this line in the view.

<p><%= t("views.commits.message").call(self, tree_path(@commit.id)) %></p>

The corresponding locales line looks like this [config/locales/en.rb]

 :message => lambda { |this, path| "This is the initial commit in this repository, " +
      this.link_to( "browse the initial tree state", path ) + "." }

The problem here is that the lambda method isn't being called using #callin the view, it's being called by someone else, which means that this isn't self that is being passed to it.

this contains views.commits.message and path contains {:rescue_format=>:html}. The Gitorious team has used this approach all over the application, which means that I can't just move the logic into a helper method without taking a day of form work.

I did some research and found this thread about the exact line.

This was the answare to the problem.

This seems to indicate you have the i18n gem installed on your system; this gem is incompatible with Gitorious. Uninstalling it with Rubygems should resolve the issue.

I've tried to uninstall i18n, but running bundle install just installs it again.

How should I solve this problem without refactor the 700 line locales file?

1

There are 1 best solutions below

1
On

This is a common problem, how to break up complex nested pieces of text.

Using markdown to simplify it

This is the initial commit in this repository
[browse the initial tree state](http://example.com/some/path)
.

Perhaps in Chinese you'd instead say

这是第一个提交在这个知识库
[看初始状态](http://example.com/some/path)
。

We have to consider 3 things;

  1. The outer text
  2. The link text
  3. The order and position of these two

If the position of the link relative to the text doesnt need to change, then @WattsInABox correct.

views.commits.message: "This is the initial commit in this repository"
views.commits.browse:  "browse the initial tree state"

We then just compose

<p>
  <%= t "views.commits.message" %>
  <%= link_to t("views.commits.browse"), tree_path(@commit.id) %>
  .
</p>

But sometimes the order and position does matter, in which case we can try to be more clever.

views.commits.message: "This is the initial commit in this repository %{link}"
views.commits.browse:  "browse the initial tree state"

Then we can interpolate the link in the right place

<p>
  <%= t "views.commits.message", link: link_to(t("views.commits.browse"), tree_path(@commit.id)) %>
</p>