module DataMapper::Is::Translatable

In order to made the model Translatable, an additional fields should should be added first to it. Here is an example of it might be implemented:

Examples:

class TranslatedNews
  include DataMapper::Resource

  property :id,         Serial

  attr_accessible :title, :content
end

class News
  include DataMapper::Resource

  property :id,         Serial
  property :author_id,  Integer,  required: true

  is :translatable do
    translatable_property  :title,    String,   required: true, unique: true
    translatable_property  :content,  Text,     required: true
    translatable_model TranslatedNews
    translatable_origin :origin_id
  end

end

An example of application:

news = News.create :translations => [{title: "Resent News", content: "That is where the text goes", locale: "en"}]
news.translations.create title: "Заголовок", content: "Содержание",locale: "ru"

news.content
# => "That is where the text goes"

::I18n.locale = "ru"
news.content
# => "Сюди идет текст"

::I18n.locale = "de"
news.content
# => nil

::I18n.locale = ::I18n.default_locale
news.content
# => "That is where the text goes"

Public Instance Methods

is_translatable() { || ... } click to toggle source
# File lib/dm-translatable.rb, line 65
def is_translatable
  extend DataMapper::Is::Translatable::ClassMethods
  include DataMapper::Is::Translatable::InstanceMethods

  translatable_define_hash
  yield
  translatable_register
end