Skip to content

Instantly share code, notes, and snippets.

@nicolasblanco
Created May 4, 2011 13:06
Show Gist options
  • Save nicolasblanco/955175 to your computer and use it in GitHub Desktop.
Save nicolasblanco/955175 to your computer and use it in GitHub Desktop.
# Sends the translated version of a given object attribute.
#
# Example:
# Given that locale is :fr
# t_attribute(@post, :description)
# will return @post.description_fr or fallback to @post.description.
def t_attribute(object, attribute)
localized_attribute = "#{attribute}_#{I18n.locale}"
object.respond_to?(localized_attribute) ? object.send(localized_attribute) : object.send(attribute)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment