Quick Start Guide
This guide will walk you through the basic steps to add rails-i18n
to a new Rails application and see it in action.
1. Create a New Rails App
If you don't have one already, create a new Rails application.
rails new my_i18n_app
cd my_i18n_app
2. Add rails-i18n
to your Gemfile
Open your Gemfile
and add the gem. For a Rails 8 app, this would be:
gem 'rails-i18n', '~> 8.0.0'
Then, run the bundler from your terminal:
bundle install
3. Configure the Default Locale
Let's set the application's default language to German (de
). In config/application.rb
, add the following line inside the Application
class:
module MyI18nApp
class Application < Rails::Application
# ... other configurations ...
# Set the default locale to German
config.i18n.default_locale = :de
end
end
4. Use a Translated Helper in a View
Generate a simple controller and view to see the translations.
rails generate controller welcome index
Now, open app/views/welcome/index.html.erb
and add a Rails helper that uses a translated string. For example, let's display the text for a "Create" button.
<h1>Willkommen!</h1>
<p>
<strong>English Button Text:</strong>
<%= I18n.with_locale(:en) { t('helpers.submit.create', model: 'Article') } %>
</p>
<p>
<strong>German Button Text (Default Locale):</strong>
<%= t('helpers.submit.create', model: 'Artikel') %>
</p>
5. Start the Server and View the Results
Start your Rails server:
rails server
Navigate to http://localhost:3000/welcome/index
in your browser. You should see the following output:
English Button Text: Create Article
German Button Text (Default Locale): Artikel erstellen
This confirms that rails-i18n
is successfully providing the German translations for your application.