title | section | order | description |
---|---|---|---|
Internationalization |
customization |
8 |
This guide covers how Spree uses Rails' internationalization features, and how you can leverage and extend these features in your Spree contributions and extensions. |
Each Store has their own default_locale
attribute. Alongside that, you can also set supported_locales
which will enable multi-language capabilities for that Store.
For locales, we use symbols such as en
,es-MX
etc - full list of supported locales is available in the Spree I18n GitHub repository.
Each Store has their own default_currency
attribute. Alongside that, you can also set supported_currencies
which will enable multi-currency capabilities for that Store.
For currencies, we use ISO 4217 symbols, eg. USD
, CAD
, EUR
Like mentioned before to translate models you will need to install Spree Globalize extension which uses Globalize library under the hood.
This gem will allow you to translate:
- Products
- Promotions
- Option Types
- Taxonomies
- Taxons
- Properties
- Shipping Methods
Spree now stores all of the translation information in a separate GitHub project known as Spree I18n. This is a stand alone project with a large number of volunteer committers who maintain the locale files. This is basically the same approach followed by the Rails project which keeps their localizations in rails-i18n.
The project is actually a Spree extension. This extension contains translations files. To translate models (provide translations for Products, Taxons, etc) you will need to install also Spree Globalize.
Each language is stored in a YAML file located in config/locales
. Each YAML file contains one top level key which is the language code for the translations contained within that file. The following is a snippet showing the basic layout of a locale file:
pt-BR:
spree:
say_no: "Não"
say_yes: "Sim"
All translations for Spree are "namespaced" within the spree
key so that they don't conflict with translations from other parts of the parent application.
Please submit Pull Requests or issues directly to Spree I18n for missing translations.
Spree maintains its localization information in a YAML file using a naming convention similar to that of the Rails project. Each of the localization filenames contains a prefix representing the language code of the locale. For example, the Russian translation is contained in config/locales/ru.yml
.
Spree has over 43 locale files and counting. See the [GitHub Repository](https://github.com/spree/spree\_i18n/tree/master/config/locales\) for a complete list.
Each locale that you wish to support will require both a Rails and Spree translation. The required Spree translation files are available automatically when you install the spree_i18n
gem.
You don't need to copy any files from spree_i18n
or rails-i18n
for their translations to be available within your application. They are made available automatically, because both spree_i18n
and rails-i18n
are railties.
{% hint style="info" %}
This section is only applicable for optionalspree_frontend
and spree_backend
gems
{% endhint %}
When reviewing the source of any view in Spree you'll notice that all text is rendered by passing a string to a helper method similar to:
<%= Spree.t(:price) %>
The Spree.t() helper method looks up the currently configured locale and retrieves the translated value from the relevant locale YAML file. Assuming a default locale, this translation would be fetched from the en translations collated from the application, spree_i18n
and rails-i18n
. Its relative key within those translation files would need to be this:
en:
spree:
price: Price
Spree extensions can contain their own config/locales
directory where developers can include YAML files for each language they wish to support.
We strongly urge all extension developers to ensure all customer facing text is rendered via the Spree.t()
helper method even if they only include a single default language locale file (as other users can simply include the required YAML file and translations in their site extension).