Adding Translations to Your Rails Model: A Guide with Mobility Gem
The Problem: You have a Rails application with a model containing numerous records. Now you need to add multilingual support, but you don't want to manually translate all the existing data.
The Solution: The Mobility gem offers a streamlined way to add translations to your existing Rails models without having to manually translate each record. It automatically handles the creation and management of translation tables, keeping your code clean and concise.
Scenario: Imagine you have a Product
model with a name
attribute. You now want to support multiple languages for the product names.
Original Code:
# app/models/product.rb
class Product < ApplicationRecord
# ...
validates :name, presence: true
end
Using Mobility:
-
Install the Mobility gem:
gem install mobility
-
Add the
translates
method to your model:# app/models/product.rb class Product < ApplicationRecord translates :name, fallbacks_for_empty_translations: true # ... validates :name, presence: true end
Explanation:
translates :name
: This tells Mobility to create a translation table for thename
attribute.fallbacks_for_empty_translations: true
: This ensures that if a translation is missing, it will fallback to the default language's value.
Now you can access translations like this:
product = Product.find(1)
product.name # English version
product.name(:es) # Spanish version (if available)
Key Advantages of Mobility:
- Automatic Translation Table Creation: Mobility handles the setup of separate translation tables, simplifying database schema management.
- Simplified Access to Translations: Access translations using intuitive methods like
product.name(:es)
. - Fallbacks for Missing Translations: Mobility provides fallback mechanisms to ensure data availability even when translations are incomplete.
- Flexible Customization: Mobility offers various customization options for managing translations, such as specifying translation types, defining custom fallback strategies, and more.
Adding Translations to Existing Records:
- Using Migration: For large datasets, consider using a migration to populate the translations.
# db/migrate/add_translations_to_products.rb class AddTranslationsToProducts < ActiveRecord::Migration[6.1] def change create_table :product_translations do |t| t.references :product, null: false, foreign_key: true t.string :locale, null: false t.string :name t.timestamps end add_index :product_translations, [:product_id, :locale], unique: true reversible do |dir| dir.up do Product.find_each do |product| product.update_attributes({ name: { en: product.name, # Add other languages if you have them } }) end end # dir.down ... end end end
- Using a Script: For more control over the translation process, create a script that iterates through your records and populates translations based on your needs.
Remember:
- Consider using a translation service like Google Translate for initial translation: While not perfect, it can give you a starting point for manual editing.
- Prioritize quality over speed: Make sure your translations are accurate and consistent, as this will significantly impact user experience.
By using Mobility, you can easily add multilingual support to your existing Rails models, enabling you to deliver a truly internationalized application experience.
Additional Resources:
- Mobility Gem Documentation: https://mobility.gitlab.io/
- Rails Internationalization Guide: https://guides.rubyonrails.org/i18n.html
Let me know if you have any other questions or need help with specific implementation details. Happy coding!