Skip to content

Instantly share code, notes, and snippets.

@amiel
Forked from adrianpike/helpers.rb
Created October 27, 2011 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiel/1321006 to your computer and use it in GitHub Desktop.
Save amiel/1321006 to your computer and use it in GitHub Desktop.
Page title helper for I18n with to_s for :show!
def page_title()
object_name = controller_name.singularize
if controller.instance_variables.include?(('@' + object_name).to_sym)
locals = {
object_name.to_sym => controller.instance_variable_get('@' + object_name).to_s
}
else
locals = {}
end
I18n.t([controller_name, action_name, 'title'].join('.'), locals)
end
### meanwhile, in en.yml
en:
foos:
index:
title: "showing foos"
show:
title: "check out this awesome %{foo}"
edit:
title: "editing %{foo}"
@adrianpike
Copy link

Translate with all the controller instance variables

def tv(key, opts)
locals_hash = controller.instance_variables.collect{|var|
unless var.to_s.match(/^@
/) then
[var.to_s.match(/^@(._)/)[1].to_sym, controller.instance_variable_get(var).to_s] rescue nil
end
}.compact

I18n.t(key, Hash[locals_hash].merge(opts))

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment