All Ruby on Rails Node JS Android iOS React Native Frontend

TIL #1: Multi-domain Translations with Rails and Gettext

Welcome to brand new series of content where we will share short tips and pieces of knowledge.

This time Ruby on Rails!

Recently I was working on separating gettext translations in our project into separate domains. It's very useful in cases when some strings should be translated differently in different contexts. I will explain shortly how to use multiple domains for Gettext translations in your Rails app. Enjoy!

Ingredients:
- Rails app

- grosser/fast_gettext - it's one of the commonly used alternatives for I18n.

By default, gettext is using single default domain. To have more than one domain we will need to overwrite gettext:setup task using our app configuration. Here are some examples which can show you how you can achieve that:

Now rails gettext:setup and rails gettext:find should create files called app.pot, frontend.pot and new_design.pot. Every call of domain-based translation method (e.g. d_("new_design", "Some string from new design goes here")) will be translated based on the values in the domain pot file. Voila, problem solved!


TIL, or Today I Learned, is where our developers share the best tech stuff they found every day. You can find smart solutions for some issues, useful advices and anything which will make your developer life easier.

Photo by Patrick Tomasso on Unsplash

Subscribe Netguru Codestories
Join over 10.000 specialists

Code stories CTA
READ ALSO FROM Ruby/Ruby on Rails
Read also
Need a successful project?
Estimate project or contact us