Skip to content

Instantly share code, notes, and snippets.

@costa
Created January 23, 2014 14:12
Show Gist options
  • Select an option

  • Save costa/8579008 to your computer and use it in GitHub Desktop.

Select an option

Save costa/8579008 to your computer and use it in GitHub Desktop.
Less stupid translation helper (with blocks for interpolation) until https://github.com/rails/rails/pull/13808 makes it into a version (and if at all)
module LessStupidTranslationHelper
include ActionView::Helpers::TranslationHelper
class Interpolator
attr_reader :variables
def initialize(context)
@variables = HashWithIndifferentAccess.new
@context = context
end
private
# XXX an alternative solution would be to define methods per interpolation variables
def method_missing(meth, *args, &block)
variables[meth] = args[0] || @context.capture(&block)
end
end
def translate(key, options = {})
if block_given?
i = Interpolator.new(self)
yield i
options = i.variables.merge(options)
end
super key, options
end
alias_method :t, :translate
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment