Skip to content

Instantly share code, notes, and snippets.

@DAddYE
Created February 19, 2010 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DAddYE/308915 to your computer and use it in GitHub Desktop.
Save DAddYE/308915 to your computer and use it in GitHub Desktop.
##
# This is an extension for ActiveRecord where if I had:
#
# post.description_ru = "I'm Russian"
# post.description_en = "I'm English"
# post.description_it = "I'm Italian"
#
# with this extension if I had set:
#
# I18n.locale = :it
#
# calling directly:
#
# post.description => post.description_it => "I'm Italian"
#
module Locale
module ClassMethods
def has_locale
include InstanceMethods
end
end # ClassMethods
module InstanceMethods
def method_missing(method_name, *arguments)
attribute = "#{method_name}_#{I18n.locale}".to_sym
return self.send(attribute) if I18n.locale.present? && self.respond_to?(attribute)
super
end
end # InstanceMethods
end
ActiveRecord::Base.extend(Locale::ClassMethods)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment