在相同的Rails Show Action中打印列的所有翻译

时间:2015-01-13 02:26:33

标签: ruby-on-rails slug rails-i18n

我试图做一件奇怪的事情:

在有I18n的网页中,我假装打印当前页面的slu ..

ModelName.friendly.find(params[:id]).slug.to_s

它有效,它不仅呈现了slug,而且呈现了我想要的任何列。

但我需要在同一页面上打印每种语言的slu .. 我假装重复该行代码的3倍,如何为每行设置不同的语言环境?为了得到这样的东西:

  • Native slug
  • 原住民翻译slug 1
  • 原生翻译slug 2

1 个答案:

答案 0 :(得分:0)

I18n支持为特定调用translate方法设置使用区域设置的不同方法。

这要求您设置一个区域设置文件,例如在config / locales / fr.yml中为法语:

fr:
  websites:
    slug: "ma-traduction-de-slug"

同样适用于所有其他所需语言。

<强> I18n.with_locale(locale_identifier)

I18n.with_locale(:en) do
  I18n.t("websites.slug")
end

在调用I18n.t

时传递语言环境
I18n.t("websites.slug", locale: :fr)

实现可能如下所示:

I18n.available_locales.each do |locale|
  puts I18n.t("websites.slug", locale: locale)
end

如果您正在寻找应用程序的使用路线和路径中的翻译,例如:

/models/mon-traduction-de-slug

查看https://github.com/enriclluelles/route_translator

最后,如果您想要manage locale files online,我们会为此提供一个工具。免费开源项目:PhraseApp