Skip to content

Instantly share code, notes, and snippets.

@coffeeaddict
Created October 21, 2010 10:21
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 coffeeaddict/638259 to your computer and use it in GitHub Desktop.
Save coffeeaddict/638259 to your computer and use it in GitHub Desktop.
Scopes for I18n
# = I18n scoped
#
# Set a temporal default scope for I18n
#
# == Synopsis
#
# Given a locale YAML that looks like this:
#
# customers:
# firstname: Customer firstname
# lastname: Customer lastname
#
# firstname: Firstname
# lastname: Lastname
#
# Then:
# I18n.scope :customers do
# puts I18n.t :firstname
# puts I18n.t :lastname
# end
#
# Will result in:
# Customer firstname
# Customer lastname
#
# Where as:
# puts I18n.t :firstname
# puts I18n.t :lastname
#
# Will result in:
# Firstname
# Lastname
#
module I18n
class << self
def scope(scope, &block)
@scope = scope
yield block
@scope = nil
end
alias_method :scope_less_translate, :translate
def translate(*args)
options = {}
options = args.pop if args.last.is_a? Hash
if @scope and !options[:scope]
options[:scope] = @scope
end
scope_less_translate args, options
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment