Skip to content

Instantly share code, notes, and snippets.

@karmi
Created August 11, 2008 10:06
Show Gist options
  • Save karmi/4837 to your computer and use it in GitHub Desktop.
Save karmi/4837 to your computer and use it in GitHub Desktop.
module ClassMethods
# Declare in ActiveRecord or ActiveResource class with localized attributes
# You must include the module "manually" in your own classes (ie. put <tt>include HasLocalizedAttributes</tt> in your class)
# See README for example
def has_localized_atributes
include InstanceMethods unless included_modules.include?(InstanceMethods)
end
end
module InstanceMethods
# Define <tt>method missing</tt> to intercept calls to non-localized methods (eg. +name+ instead of +name_cz+)
def method_missing(method_name, *arguments)
language = (defined?(Gibberish) ? Gibberish.current_language.to_s : HasLocalizedAttributes.default_language).sub(/cz/, 'cs')
# puts "Trying to send '#{method_name}_#{language}' to #{self}" # uncomment for easy debugging in script/console
return self.send(:"#{method_name}_#{language}") if self.respond_to?(:"#{method_name}_#{language}")
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment