views:

541

answers:

1

Hello,

Hoping some learned Rails developers here can recommend an existing Ruby on Rails plugin or gem that allows you to continue using the Simple I18n backend whilst allowing you to optionally specify translations in the database.

Here's why:

I have one Rails app used for many websites. For the example I'll just use 2 websites:

Website 1: Leprechauns R Us

Website 2: Unicorns R Us

Most translations are the same for both websites, but occassionally I want to override a translation. For example, in my en-US.yml file I have the following translation:

view_all: View all

And for most websites this translation is fine, including for website 1 (Leprechauns) where I'm happy to use "View all".

However, for website 2, I'd like to use "View all Unicorns" as the view_all translation and I'd like to specify this in the database. For maintenance reasons I don't want to specify this override in a YAML file.

Many thanks,

Eliot

A: 

In the end I opted to take advantage of Rails' I18n::Backend::Simple's ability to process both .yml files and .rb files as locale dictionaries.

Artifacts created:

  1. DB migration to create a translations table with columns: locale, key, text

  2. Translation model to map to the translations table

  3. Class method to_locale_hash on Translation model that returns a locale keyed hash as required by I18n::Backend::Simple.load_rb

  4. A single-line file located at config/translations.rb with the line 'Translation.to_locale_hash'

For the source code see the Spree extension (sorry not in a Rails plugin structure, it will be easy to move over to a plugin should you require it) here:

http://github.com/eliotsykes/spree-i18n-db/tree/master

Eliot Sykes