在语言环境文件中使用辅助方法?

发布于 2025-01-01 08:14:12 字数 1146 浏览 3 评论 0原文

我正在尝试使用 Rails 3 启动并运行 Gitorious,但遇到了一个问题。

我在视图中看到了这条线。

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

相应的语言环境行看起来像这样 [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 ) + "." }

这里的问题是 lambda 方法没有在视图中使用 #call 调用,它被其他人调用,这意味着 this 不是传递给它的 self

this 包含 views.commits.messagepath 包含 {:rescue_format=>:html}。 Gitorious 团队在整个应用程序中都使用了这种方法,这意味着我不能在不花一天的表单工作的情况下将逻辑转移到辅助方法中。

我做了一些研究,发现这个 围绕确切的线进行线程。

这就是问题的答案。

这似乎表明您的系统上安装了 i18n gem;此宝石与 Gitorious 不兼容。使用 Rubygems 卸载它应该可以解决该问题。

我尝试卸载 i18n,但运行 bundle install 只是再次安装它。

在不重构 700 行 locales 文件的情况下,我应该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2025-01-08 08:14:12

这是一个常见问题,如何分解复杂的嵌套文本片段。

使用 markdown 来简化它

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

也许在中文中你会说

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

我们必须考虑 3 件事;

  1. 外部文本
  2. 链接文本
  3. 这两者的顺序和位置

如果链接相对于文本的位置不需要更改,则 @WattsInABox 正确。

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

然后我们就作曲

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

但有时顺序和位置确实很重要,
在这种情况下,我们可以尝试变得更聪明。

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

然后我们可以将链接插入到正确的位置

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文