Skip to content

Instantly share code, notes, and snippets.

@galetahub
Last active August 29, 2015 13:57
Show Gist options
  • Save galetahub/9911105 to your computer and use it in GitHub Desktop.
Save galetahub/9911105 to your computer and use it in GitHub Desktop.
MongoId translates
module Adriano
module Translate
extend ::ActiveSupport::Concern
module ClassMethods
attr_accessor :translated_fields
def translates(*args)
self.translated_fields = *args
I18n.available_locales.each do |locale|
translated_fields.each do |field|
define_method "#{field}_#{locale}" do
read_translation(field, locale)
end
define_method "#{field}_#{locale}=" do |value|
write_translation(field, value, locale)
end
end
end
end
end
protected
def read_translation(field, locale)
hash = send("#{field}_translations")
hash[field.to_s][locale.to_s] if hash && hash[field.to_s]
end
def write_translation(field, value, locale)
hash = send("#{field}_translations")
if hash && hash[field.to_s]
hash[field.to_s][locale.to_s] = value
send("#{field}_translations=", hash)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment