Remote-I18n-Extension

This gem is built on top of the popular I18n gem providing the ability to fetch translations from remotely hosted locale files. In case of a missing translation the gem fallbacks to serving translations from local files(present under config/locales) folder of your rails project.

Requirements

This gem requires ruby 3.1.3 and Rails 7.0.4.3

Installation

To get started paste the following inside your Gemfile and run bundle

gem 'remote_i18n_extension', '~> 1.0', '>= 1.0.9'

or install via terminal using the following command:

gem install remote_i18n_extension

After that run the following command to generate the config file

rails g remote_i18n_extension:install

This will create the following config file config/initializers/remote_i18n_extension.rb

Generated Configuration File

require 'remote_i18n_extension'

RemoteI18nExtension.config do |config|
  # All the configurations are mandatory

  # URL to your remote locales folder housing all the translations
  # For now the library expects all the locales to be stored in a single (root) folder
  # e.g. https://raw.githubusercontent.com/Gooner91/locales-repo/master/locales
  # config.remote_host = ENV['LOCALES_REMOTE_HOST']


  # Below configurations are for the ruby I18n module

  # List of available locales
  # config.available_locales = [:en, :de]


  # config.locale = :en

  # Paths to the translation files (in your local rails project for fallback)
  # e.g. Dir[Rails.root.join("config", "locales", "*.{rb,yml}")]
  # config.load_path = %w[]
end

Configurations values

remote_host

  • base URL/URL to the folder hosting the remote files
    • an example URL would be https://raw.githubusercontent.com/Gooner91/locales-repo/master/locales if all the translation files are uploaded in side the locales folder provided in the above URL #### available_locales
  • the standard I18n configuration option e.g. %i[:en :de] #### locale
  • the standard I18n configuration option e.g. :en #### load_path
  • the standard I18n configuration option e.g. Dir[Rails.root.join("config", "locales", "*.yml")]

Usage

Fire up the server and you should be able to reference translations using the following method:

RemoteI18nExtension.t('activerecord.attributes.user.confirmation_sent_at')

Demo Video

A demo of the working of this gem can be seen on the following loom link: https://www.loom.com/share/78825da1d74248f1b80949bf4e72b1cc

Constraints:

This gem facilitates fetching the locales that are hosted online. For that purpose the gem expects all the locales/translation files to be stored at one place.

Future Aspects:

Since this was a time bound activity, there are some improvments/fine tuning that can be done even further:

  • Better look up support, nested file structure support for remote locales
  • Increasing the overall test coverage of the gem, for now just a few basic unit test cases are added to check the behavior of the Remote backend, test coverage should be increased
  • During testing with a demo Rails project, sometimes the gem behaves inconsistently when it comes to fallback behavior, understanding the edge cases and consistencies better and handle those as well
  • Improving code quality, fixing offences pointed out by rubocop as well as properly setting up rubocop rules
  • Better error handling
  • Tools for measuring test coverage should be integrated i.e. simplecov
  • Enforcing validation on configurations, in case some configuration is missing (raising appropriate errors)