Ruby rails: translation

From wikinotes

NOTE:

The t() function translates code (internationalization)
The l() function formats datetime objects.

See https://guides.rubyonrails.org/i18n.html#abstracting-localized-code

Documentation

internationalization docs https://guides.rubyonrails.org/i18n.html#internationalization-and-localization

Locations

{project}/config/locales/{locale}.yml A dictionary of localized (native language) words. These are used by the t() function.


Internationalization

  • words/sentences to be internationalized are stored in a {project}/config/locales/{locale}.yml file.

Locales

I18n.locale  # get/set current locale

Translate

t('key')                               # translate {locale.yml}['key']
t('key', scope: 'general.network')     # translate {locale.yml}['general']['network']['key']
t('key', scope: [:general, :network])  # translate {locale.yml}['general']['network']['key']

Locale-Specific Grammar

# variable interpolation
t('key', price: "${price}")  # alters substitutions of 'price'. use a variable for regional
t('key', price: "{price}$")

Dates/Times

See #l method (localize).