Translating the StoreFront
Explore how to implement internationalization in a Rails 6 application by adding multi-language support through I18n methods. Understand locale configurations, translation keys, and currency formatting to provide a localized storefront experience in English and Spanish. This lesson guides you through translating layouts, buttons, and dynamic content while managing locale parameters and formatting conventions.
Now it’s time to provide the translated text. Let’s start with the layout because it’s pretty visible. We replace any text that needs to be translated with calls to I18n.translate. Not only is this method conveniently aliased as I18n.t, but a helper named t is provided.
Adding translation for the store front
The parameter to the translate function is a unique dot-qualified name. We can choose any name we like, but if we use the t helper function provided, names that start with a dot will first be expanded using the name of the template. So, let’s do that:
Since this view is named layouts/application.html.erb, the English mappings will expand to en.layouts.application. Here’s the corresponding locale file:
Here it is in Spanish: