Skip to content

Instantly share code, notes, and snippets.

@remvee
Created January 12, 2010 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save remvee/275127 to your computer and use it in GitHub Desktop.
Save remvee/275127 to your computer and use it in GitHub Desktop.
Some scoping for Rails I18n
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