Tutorial by Examples

Assuming you have this YAML locale file: # config/locales/en.yml en: header: title: "My header title" and you want to display your title string, you can do this # in ERB files <%= t('header.title') %> # in SLIM files = t('header.title')
You can pass parameters to I18n t method: # Example config/locales/en.yml en: page: users: "%{users_count} users currently online" # In models, controller, etc... I18n.t('page.users', users_count: 12) # In views # ERB <%= t('page.users', users_count: 12) %> #S...
You can let I18n handle pluralization for you, just use count argument. You need to set up your locale file like this: # config/locales/en.yml en: online_users: one: "1 user is online" other: "%{count} users are online" And then use the key you just created by ...
In most cases, you may want to set I18n locale. One might want to set the locale for the current session, the current user, or based on a URL parameter This is easily achievable by implementing a before_action in one of your controllers, or in ApplicationController to have it in all of your controll...
Sometimes it can be useful to set your application locale based upon the request IP. You can easily achieve this using Geocoder. Among the many things Geocoder does, it can also tell the location of a request. First, add Geocoder to your Gemfile # Gemfile gem 'geocoder' Geocoder adds location...
globalize gem is a great solution to add translations to your ActiveRecord models. You can install it adding this to your Gemfile: gem 'globalize', '~> 5.0.0' If you're using Rails 5 you will also need to add activemodel-serializers-xml gem 'activemodel-serializers-xml' Model translations...
# config/locales/en.yml en: stackoverflow: header: title_html: "Use <strong>I18n</strong> with Tags & Symbols" Note the addition of extra _html after the name title. And in Views, # ERB <h2><%= t(:title_html, scope: [:stackoverflow, :heade...

Page 1 of 1