0

I'm trying to do a weird thing:

In a web with I18n I pretend to print the slug of current page.

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

It works, it renders not only the slug but any column I want.

But I need to print also the slug of each language in the same page.
What I pretend is to repeat 3 times that line of code, how can I set different locale for each line? in order to to get something like this:

  • Native slug
  • Native translated slug 1
  • Native translated slug 2
Ricardo Castañeda
  • 5,746
  • 6
  • 28
  • 41

1 Answers1

0

I18n supports different ways of setting the used locale for a certain call to the translate method.

This requires you to have set up a locale file, e.g. in config/locales/fr.yml for French:

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

And similarly for all other desired languages.

I18n.with_locale(locale_identifier)

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

Pass locale in the call to I18n.t

I18n.t("websites.slug", locale: :fr)

An implementation could look like this:

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

If you're looking for translations in the used routes and paths of your application like:

/models/mon-traduction-de-slug

have a look at https://github.com/enriclluelles/route_translator

And finally if you want to manage locale files online, we provide a tool for this. Free for open source projects: PhraseApp

fredostarr
  • 464
  • 1
  • 3
  • 9