Created
August 11, 2008 10:06
-
-
Save karmi/4837 to your computer and use it in GitHub Desktop.
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
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