Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n lazy lookup #272

Open
mbajur opened this issue Apr 4, 2015 · 4 comments
Open

i18n lazy lookup #272

mbajur opened this issue Apr 4, 2015 · 4 comments

Comments

@mbajur
Copy link

mbajur commented Apr 4, 2015

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.

@mbajur mbajur changed the title i18n path for cells i18n lazy lookup Apr 4, 2015
@apotonick
Copy link
Member

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! 🍻

@mbajur
Copy link
Author

mbajur commented Apr 14, 2015

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: 🍻]


Reply to this email directly or view it on GitHub
#272 (comment).

@mbajur
Copy link
Author

mbajur commented Apr 14, 2015

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
Copy link
Member

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!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants