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!
- Rails app
- grosser/fast_gettext - it's one of the commonly used alternatives for I18n.
By default,
# config/application.rb
# Add configuration constant with list of translation domains you need
module MyApp
class Application < Rails::Application
...
TEXT_DOMAINS = %w(app frontend new_design).freeze
...
end
end
# lib/tasks/gettext.rake
# Remove the provided gettext setup task
Rake::Task["gettext:setup"].clear
# Use your custom translation repositories list
namespace :gettext do
task :setup => [:environment] do
FastGettext.translation_repositories.each_key do |domain|
GetText::Tools::Task.define do |task|
task.package_name = domain
task.package_version = "1.0.0"
task.domain = domain
task.po_base_directory = locale_path
task.mo_base_directory = locale_path
task.files = files_to_translate
task.enable_description = false
task.msgmerge_options = gettext_msgmerge_options
task.msgcat_options = gettext_msgcat_options
task.xgettext_options = gettext_xgettext_options
end
end
end
end
# config/initializers/locale.rb
# Add gettext domain's based on constant from your app configuration
MyApp::Application::TEXT_DOMAINS.each do |domain|
FastGettext.add_text_domain domain,
path: File.join(Rails.root, "locale"),
type: :po,
ignore_fuzzy: true,
report_warning: true
end
FastGettext.default_text_domain = "app"
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
Photo by Patrick Tomasso on Unsplash