Created
January 23, 2014 14:12
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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