Skip to content

Instantly share code, notes, and snippets.

@pavanjoshi914
Last active April 13, 2021 10:09
Show Gist options
  • Save pavanjoshi914/d5523248a3e51cea0c07a4e47c080d1e to your computer and use it in GitHub Desktop.
Save pavanjoshi914/d5523248a3e51cea0c07a4e47c080d1e to your computer and use it in GitHub Desktop.
Comparison between different backends for model translations

Difference between different Backends

Column Backend

  • Creation of localized version of each attribute
  • eg: title_en, title_fr, title_ja,
  • mobility supports this using :column backend

Disadvantages

  • not scalable when we support large number of locales due to increase in number of columns in the database
  • Every time migration is required to introduce new attrbiute or locale

Table Backend

  • Each model will have its separate translation model
  • eg: Translated attrbiutes of User model will be stored in user_translations model
  • user_translation model will contain columns such as
  • user_id - to join translation model with its corresponding original model
  • locale - locale column to store value of locale
  • attributes to be translated - columns for attributes to be translated
  • normal association between model and translation model

Advantages

  • No wastage of database capacity
  • No need of migration while adding translation of attribute for any new locale.
  • more modular

Disadvantage

  • hard querying of translated attrbiutes
  • requires migration when we introduce new translated attribute to the existing model

Key-Value Backend

  • A single translation model for all models
  • creation of shared tables which will hold translations for all the model attributes
  • shared tables will be created according to the data types
  • data stored in form of key value pair
  • eg: all string attributes of all models will be in one table, all text will be in another table
  • Polymorphic association between shared tables and models

Advantages

  • No extra migration for everytime we introduce translation for new locale or introduce new attribute in existing model
  • Relatively space efficient
  • Mobility promotes key-value backend as a default strategy for model translation

Disadvantages

  • less modularity
  • Complex Joining because of polymorphic association

Serialized Backend

  • Stores serialized data in form of hash in a single column
  • querying in serialized data not possilbe

    not considering to be used

.hstore and .jsonb Backend

  • used for postgres database only

    not considering to be used

Easy Model translations with mobility

  • Mobility promotes use of key-value backend and uses it as a default backend strategy
  • Supports ActiveRecord
  • Makes Basic operations such as caching, fallbacks, locale accessors, dirty tracking etc. way too easy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment