Jumpstart docs logo Back to the app

Internationalization (I18n)

Jumpstart Pro comes preconfigured with internationalization support allowing you to translate your app to any locale easily.

I18n Settings

Inside config/application.rb you will find several settings for I18n:

  • available_locales - An array of locales your app supports
  • default_locale - The default locale to use
  • fallbacks - Whether or not to use the default locale as a fallback
  • load_path - Folders to look for translation files

Requests

The SetLocale concern sets the locale at the beginning of each request. It checks for a locale from several places:

  • params[:locale]
  • HTTP_ACCEPT_LANGUAGE header
  • The current user's preferred_language attribute
  • I18n.default_locale

You can customize this concern to change the order or look for the locale in other places such as the domain or subdomain.

Devise I18n

The devise-i18n gem comes preinstalled to provide translations for most languages.

These translations don't include every key since Devise has been customized. You will need to customize the translations using the following command:

rails g devise:i18n:locale es

I18n Examples

Jumpstart Pro uses the standard Rails I18n functionality. See the Rails I18n guide for details on how Rails implements I18n.

Translations are automatically scoped by specifying a period . at the beginning of the key. Without the period (.) at the beginning, it will start lookup at the top level.

t("hello") in app/views/dashboard/show.html.erb will look for en.hello

t(".hello") in app/views/dashboard/show.html.erb will look for en.dashboard.show.hello

Relative Translations

# app/views/dashboard/show.html.erb
<%= t ".hello" %>
# config/locales/en.yml
en:
  dashboard:
    show:
      hello: "Your placeholder text here"

With Arguments

# app/views/dashboard/show.html.erb
<%= t ".welcome", name: "Chris" %>
# config/locales/en.yml
en:
  dashboard:
    show:
      hello: "Welcome, %{name}"

With HTML

Keys ending in _html will render as HTML.

# app/views/dashboard/show.html.erb
<%= t ".welcome_html", name: "Chris" %>
en:
  dashboard:
    show:
      hello_html: "Welcome, <strong>%{name}</strong>"

Controllers

# app/controllers/messages_controller.rb
MessagesController < BaseController
       def update
       # Some business logic

       return redirect_to:index, notice: t(".notice") if @resource.save
       render :edit, alert: t(".alert")
       end
       end
en:
  messages:
    update:
      notice: "Message was successfully updated."
      alert: "Unable to update message."

Placeholders

<%= f.input :name, placeholder: true %>
en:
  helpers:
    placeholder:
      message:
        name: "Your placeholder text here"

Model Names

en:
  activerecord:
    models:
      accounts: "Teams"

Model Attributes

en:
  activerecord:
    attributes:
      api_token:
        last_used_at: "Last Used"

Model Validation Errors

en:
  activerecord:
    errors:
      models:
        account:
          attributes:
            domain:
              reserved: "%{value} is reserved."