Rails i18n pluralization

Sometimes we need to show different text based on some count. Like for example: if box_count == 0 message = "No boxes present" elsif box_count == 1 message = "Only 1 box present" else message = "There are #{box_count} boxes" end You can refactor this to make use of i18n locales power. In en.yml you can do - en: boxes: message: zero: No boxes present one: Only 1 box present other: There are %{count} boxes And in view we can use: <%= t('boxes.message', count: 2) %>

Access Rails locale in Javascript

For displaying flash messages and other informative messages, we generally use Internationalization (I18n) in rails. Sometimes we also need to write similar messages again in javascript/coffeescripts which can be difficult to manage if these message changes. And also it doesnt follow the concept of DRY. To follow DRY, we need to make rails locales available in javascript. One way to do it is to declare a global variable and have all locales saved to it as Javascript Object. This is what we did in our project: Added following code in application layout: For HAML - window.I18n = #{I18n.backend.

Translations using gettext_i18n_rails gem

Using as Gem: gem 'gettext_i18n_rails' Using as plugin: gem 'fast_gettext', '>=0.4.8' Add "gettext" if you want to find translations or build .mo files Add "ruby_parser" if you want to find translations inside haml/slim files gem 'gettext', '>=3.0.2', :require => false, :group => :development gem 'ruby_parser', :require => false, :group => :development Rakes: rake gettext:add_language[XX] This will also create the locale directory (where the translations are being stored) where XX is the ISO 639-1 2-letter code for the language you