sachin
sachin

Reputation: 163

i18 translate link_to text rails

Hi I need to design a application running in English and Spanish .Is there any option in rails like button for translation of link_to tag text I am looking for option similar to this

helpers:
submit:
  # This will be the default ones, will take effect if no other
  # are specifically defined for the models.
  create: "Créer %{model}"
  update: "Modifier %{model}"

Upvotes: 0

Views: 1673

Answers (3)

Kh Ammad
Kh Ammad

Reputation: 1085

Create two files: config/locales/es.yml & config/locales/en.yml and define the Spenish and English content in their own files.

Then define your links like:

<% link_to t('path.to.key'), your_url %>

I have done similar work in my app for switching English & Swahilli.

Switching language code:

View:

<div class="pull-right">
  <% [:en, :swa].each do |language| %>
    <%= link_to language.to_s.upcase, change_language_path(language), class: "btn btn-info #{'disabled' if language == I18n.locale}" %>
  <% end %>
</div>

Controller:

def change_language
  cookies[:language] = params[:language]

  redirect_to :back
end

Route.rb

match 'change_language/:language' => 'my_controller#change_language', as: 'change_language'

Upvotes: 0

rlarcombe
rlarcombe

Reputation: 2986

It's all described here

You will need to save your Spanish and English translations in: config/locales/es.yml and config/locales/en.yml respectively.

For link texts you can do something like:

es:
  links:
    home: "Casa"

Then you can call:

<%= link_to t('links.home'), root_path %>

Upvotes: 4

Yury Lebedev
Yury Lebedev

Reputation: 4005

You just need to fix your locales file (the indentation was incorrect):

helpers:
  submit:
    # This will be the default ones, will take effect if no other
    # are specifically defined for the models.
    create: "Créer %{model}"
    update: "Modifier %{model}"

Upvotes: 0

Related Questions