trailblazer / cells

View components for Ruby and Rails.
https://trailblazer.to/2.1/docs/cells.html
3.07k stars 236 forks source link

i18n lazy lookup #272

Open mbajur opened 9 years ago

mbajur commented 9 years ago

Hello! I'm using cells for some time and i'm pretty glad with how it works. However, one thing bothers me - i'm not able to use shorter version (aka lazy lookup) of my i18n keys when translating a text inside my cells. It's actually pretty obvious because these shorter forms are meant to be used with regular controller/view/partial entities but i was thinking - wouldn't that be lovely to make it able to use it with cells using some generic scheme? For example, if user uses t('.label') inside a show.erb cell view of UserWidgetCell, it should display a translation from en.cells.user_widget.show.label.

That would be amazing to be able to use .label instead of that whole chain, just like in regular rails views. I would love to make a PR but i have searched everything and i'm not able to figure out how to configure such thing in rails.

apotonick commented 9 years ago

Cool idea - I don't think you have to configure that. You have to override the #t method in the cell class and then add this path.

class Cell::ViewModel < ..
  def t(name)
    super "en.bla.#{name}"
  end
end

I am not sure where to get all the information you need for this string (e.g. en) since I haven't used i18n so far but if you PR something we can merge this into Cells 4! :beers:

mbajur commented 9 years ago

Oh, that totally makes sense. I'm sure i'll send a PR soon :)

On 14 April 2015 at 09:11, Nick Sutterer notifications@github.com wrote:

Cool idea - I don't think you have to configure that. You have to override the #t method in the cell class and then add this path.

class Cell::ViewModel < .. def t(name) super "en.bla.#{name}" endend

I am not sure where to get all the information you need for this string (e.g. en) since I haven't used i18n so far but if you PR something we can merge this into Cells 4! [image: :beers:]

— Reply to this email directly or view it on GitHub https://github.com/apotonick/cells/issues/272#issuecomment-92666128.

mbajur commented 9 years ago

Ok so that's how that method should look like:

def t(*args)
  options   = args.last.is_a?(Hash) ? args.pop.dup : {}
  key       = args.shift
  cell_name = self.class.to_s.delete('Cell').underscore

  path = if key.starts_with?('.')
    "cells.#{cell_name}#{key}"
  else
    key
  end

  super(path, options)
end

but to be honest i have no idea where to put that. Cells code is a bit too complex for me so i probably don't understand something nor where that should be implemented. I've tried all probable places in Cell::ViewModel without any luck. Sorry for being dumb!

apotonick commented 9 years ago

It's really simple: every instance method in a cell class is available as a "helper" in the view. That means you can simply put it into Cell::ViewModel as I showed you earlier.

This is cool! I like it! Can you PR? Just put that code into lib/cell/i18n.rb into a module Cell::I18N. Thanks!!!