author avatar

nitturu.baba

Tue Oct 08 2024

How to Create a Multi-Language Website in Rails

Ruby on Rails comes with an integrated internationalization (I18n) framework that makes it simple to add multi-language support to your website.

Create Locale Files

You can define your translations using .yml files located in the config/locales directory.
For example, if your website supports English and Hindi, you would create two files: en.yml and hi.yml.

en.yml file:


en:
    hello: "Hello"
    good_morning: "Good Morning %{name}"  # %{name} is used to pass dynamic parameters.
    rails: "Rails"


hi.yml file:


hi:
    hello: "नमस्ते"
    good_morning: "शुभ प्रभात  %{name}"
    rails: "रेल"


Use Translations in Your Views

You can use the t helper method to access translations in your views.



<%= t("hello") %>
// normal usage
<%= t("good_morning", name: t("rails")) %>
// Passing Dynamic Parameters.


Language Switching via URL

The language displayed will depend on the locale set in the URL. For example:
• If the URL is , the English translation will be used.
• If the URL is , the Hindi translation will be used.
#CU6U0R822 #multi-language