39331

Rails I18n Cascading - how to get it to work

Question:

I read in <a href="http://svenfuchs.com/2011/2/11/organizing-translations-with-i18n-cascade-and-i18n-missingtranslations" rel="nofollow">this blogpost from Sven Fuchs</a> about the cascading possibility by I18n, but I am unable to get it to work.

I tried to put the code mentioned in the blogpost into the application controller and into an initializer, I also tried to pass the cascade option, like it is mentioned in the comments in the <a href="https://github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/cascade.rb" rel="nofollow">module itself</a>, but nothing seems to work.

Does anybody have any hints or a working example about how to get I18n cascading in a Rails 4 App up and running?

Answer1:

So, finally got this working.

The problem is, that the cascading module isn't included by default, so you have to include it yourself. Unfortunately, there doesn't seem to be a predefined config-option (like with config.i18n.fallbacks, but please correct me if I am wrong), so you have to include it manually:

# config/application.rb module NameOfMyApp class Application < Rails::Application # some config code here I18n.backend.class.send(:include, I18n::Backend::Cascade) # more config code end end

After that, if you pass the cascade: true option into the translate-helper, it will work.

<h2>An example</h2>

Consider that you have the following lines in your en.yml file:

# config/locales/en.yml en: title: 'Default application title' subtitle: 'Default application subtitle' footertext: 'Default text for footer' accounts: title: 'Default accounts title' subtitle: 'Default accounts subtitle' index: title: 'Accounts index title'

In your views you use it like this:

# app/views/account/index.html.erb <%= t('accounts.index.title', cascade: true) %> # => "Accounts index title" <%= t('accounts.index.subtitle', cascade: true) %> # => "Default accounts subtitle" <%= t('accounts.index.footertext', cascade: true) %> # => "Default text for footer"

It is also working with <a href="http://guides.rubyonrails.org/i18n.html#looking-up-translations" rel="nofollow">lazy lookups</a>:

# app/views/account/index.html.erb <%= t('.title', cascade: true) %> # => "Accounts index title" <%= t('.subtitle', cascade: true) %> # => "Default accounts subtitle" <%= t('.footertext', cascade: true) %> # => "Default text for footer"

Answer2:

Some additional information to the accepted answer (that helped me quite well to get in the right direction).

If you also want the cascading lookup to be the default behaviour of the translate helper, one way to go is by overriding the translate method. Doing this you manipulate as little as possible to get this to work. Most probably this solution should be upwards compatible, too - as long as the translate helper isn't changed too much regarding method signature etc.

# e.g. in config/initializers/i18n.rb # monkey-patch to let I18n.translate (and its alias 't') cascade for keys by default ActionView::Base.class_eval do def translate(key, options = {}) super(key, options.merge({ cascade: { skip_root: false } })) end alias t translate end

This solution is as well the <a href="http://svenfuchs.com/2011/2/11/organizing-translations-with-i18n-cascade-and-i18n-missingtranslations" rel="nofollow">recommended way by Sven Fuchs</a> and is tested with Rails 4.0.4.

Recommend

  • Attribute name showing on Devise Error
  • Rails and RSpec: Testing controllers with the same name in different namespace (module)
  • Using ActionCable with multiple identification methods
  • Adding Numeric Range Filtering to ActiveAdmin
  • How I can modularize Rails model?
  • nodeJS util.format passing an array
  • Positioning children objects in scene (car wheels hierarchy)
  • Plotting densities in R
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • msbuild create itemgroup from property group
  • how does System.Web.HttpRequest::PathInfo work?
  • Eloquent paginate function in Slim 3 project using twig
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • custom UITableViewCell with image for highlighting
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Test if a set exists before trying to drop it
  • Chrome doesn't support silverlight anymore? How to solve this?
  • $wpdb not working in file of WordPress plugin
  • Meteor: Do Something On Email Verification Confirmation
  • Meteor helpers not available in Angular template
  • Spring Data JPA custom method causing PropertyReferenceException
  • C# - Serializing and deserializing static member
  • Sending data from AppleScript to FileMaker records
  • Where to put my custom functions in Wordpress?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • 0x202A in filename: Why?
  • RestKit - RKRequestDelegate does not exist
  • Cannot Parse HTML Data Using Android / JSOUP
  • How to include full .NET prerequisite for Wix Burn installer
  • Buffer size for converting unsigned long to string
  • WPF Applying a trigger on binding failure
  • Hits per day in Google Big Query
  • Java static initializers and reflection
  • File not found error Google Drive API
  • How to get Windows thread pool to call class member function?
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app