下面是一个法语菜单的例子,我还会有另外五种语言的菜单:
<a href="/<%= @lang %>/">Jeux</a>
<a href="/<%= @lang %>/get-the-player">Obtenir le lecteur</a>
<a href="/<%= @lang %>/about">À propos</a>
<a href="/<%= @lang %>/faq">FAQ</a>
<a href="/<%= @lang %>/contact">Contact</a>
目前菜单位于相关页面 (.html.erb) 中,使用下面列出的代码
<%= content_for :menu do %>
<a href="/<%= @lang %>/">Jeux</a>
<a href="/<%= @lang %>/get-the-player">Obtenir le lecteur</a>
<a href="/<%= @lang %>/about">À propos</a>
<a href="/<%= @lang %>/faq">FAQ</a>
<a href="/<%= @lang %>/contact">Contact</a>
<% end %>
完成上述操作后,我就获得了 application.html.erb 的引用
<%= yield :menu %>
对于其他语言,它可以以完全相同的方式完成,但是,有没有一种方法可以避免重复代码。也许让 Controller 去寻找它并将其呈现在确切的位置?请帮忙,
提前致谢!
============================================= ============================
**CHECK BELOW FOR SOLUTIONS THAT I HAVE PROVIDED**
最佳答案
看看内置的 internationalization (i18n) API .
编辑:另一种方法是使用本地化 View ,如所述in the ruby on rails i18n guides .例如,您可以将文件命名为 index.fr.html.erb
和 index.en.html.erb
。然后,根据您的 I18n.locale
设置,呈现适当的 View 。
关于ruby-on-rails - 您将如何提供用不同(语言环境)语言编写的菜单而不是在每个页面中重复它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13255037/