Skip to content

Instantly share code, notes, and snippets.

@suweller
Forked from remvee/translation_scope.rb
Created January 12, 2010 11:43
Show Gist options
  • Save suweller/275129 to your computer and use it in GitHub Desktop.
Save suweller/275129 to your computer and use it in GitHub Desktop.
module ActionView
module Helpers
module TranslationHelper
# Override translate method to consider translate_scope.
def translate(key, options = {})
I18n.translate(key, {:raise => I18n.raise_on_missing, :scope => (translate_scope.dup << options.delete(:add_scope)).compact}.merge(options))
end
alias :t :translate
# Append scopes to the current translation scope.
def with_translate_scope(*scopes)
before = translate_scope.dup
options = scopes.extract_options!
if options[:absolute]
@translate_scope = scopes
else
@translate_scope += scopes
end
yield
ensure
@translate_scope = before
end
# The current translation scope, default to the current controller name.
def translate_scope
@translate_scope ||= [(controller.controller_name rescue controller.mailer_name)]
end
end
end
end
module MailerTranslationScope
private
def t(*args)
args << {:scope => self.class.name.underscore}.merge(args.extract_options!)
I18n.translate(*args)
end
end
module I18n
class << self
attr_accessor :raise_on_missing
end
end
ActionMailer::Base.send(:include, MailerTranslationScope)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment