How to use i18n conversion method with gem Draper?

I use the Draper gem to decorate my models. Here I have pretty classic settings:

# app/decorators/subject_decorator.rb class SubjectDecorator < ApplicationDecorator decorates :subject def edit_link h.link_to(ht('.edit'), '#') end end 

I use i18n for internationalization. But when I run this, I get:

 Cannot use t(".edit") shortcut because path is not available 

So I was wondering if anyone had done this before? This should be pretty straight forward.

+4
source share
3 answers

Inside your code, you should use:

 I18n.t('mylabelkey') 

try ... it should work

+6
source

The problem is that you cannot use lazy search in decorators because they have no context for determining the level of the presentation file (index, show, edit, etc.). Therefore, out of the box you just need to describe anything you like, for example t('subjects.show.edit') or something else.

Here is what I did to make it work for me.

 class ApplicationDecorator < Draper::Base def translate(key, options={}) if key.to_s[0] == '.' key = model_class.to_s.downcase.pluralize + key end I18n.translate(key, options) end alias :t :translate end 

This will not give you the full link of subjects.show.edit , you just get subjects.edit , but it seemed to me better than nothing.

+7
source

All you have to do is add this to the top of your decorator class ...

 include Draper::LazyHelpers 

... and it handles many of the most common helper methods, including i18n. You can also write all these "h" in your class.

+2
source

All Articles