Created
October 21, 2010 10:21
-
-
Save coffeeaddict/638259 to your computer and use it in GitHub Desktop.
Scopes for I18n
This file contains 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
# = 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