¿Usar métodos auxiliares dentro de un archivo de configuraciones regionales?

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 no es self that is being passed to it.

this contiene views.commits.message y path contiene {: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.

Investigué un poco y encontré este 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, pero corriendo bundle install just installs it again.

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

preguntado el 01 de febrero de 12 a las 22:02

Are you able to change the view so that it makes a more "standard" I18n.t call? Passing in the current context to a translation just so that it can use link_to is kinda crazy! -

Changing all t-calls for the application isn't realistic. -

Not thinking todos t calls, just the one with a link in it. Could be as simple as this : <%= t :'views.commits.message, :link => link_to(t(:'views.commits.link'), :path => tree_path(@commit.id))) %> and then in your locale file : en: views: commits: message: "This is the initial commit in this repository, %{link}" link: "browse the initial tree state" -

I sorta agree with @tigrish. In my opinion, link_to should be called from the view since it's a view helper and you should call t again for the string to pass to t. Example: <p><%= t "views.commits.message" %><%= link_to t("views.commits.browse"), tree_path(@commit.id) %></p> Doing things the right way is worth the time investment. Think about a year from now if you had to change something. It would take you an hour to even recognize what was going on in the code. -

1 Respuestas

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>

Respondido el 28 de enero de 13 a las 04:01

I know this question is a year old, but it felt worth answering. - Matthew Rudy

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.