Repository is archived
No commit activity in last 3 years
No release in over 3 years
Translate mongoid models.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.6

Runtime

 Project Readme

Mongoid Translate

Installation

Ruby 1.9.2 is required.

Install it with rubygems:

gem install mongoid_translate

With bundler, add it to your Gemfile:

gem "mongoid_translate", "~>1.0.0"

Usage

Exemple :

class Article
  include Mongoid::Document

  # add Translate module
  include Mongoid::Translate

  # declare fields to be translate
  translate :title, :content

end

Create a Namespace model. You can add callbacks validation and so on.

class Translation::Article
  include Mongoid::Document

  # add Translation module
  include Mongoid::Translation
end

That'all folks.

Display translation :

article = Article.first
# return title according to I18n.locale, or main_translation.
article.title

# return list of existing translation for this resource
article.languages

# return main_translation
article.main_translation

# return english translation
article.en

Persist translation. It's just an embeds_many relation.

article = Article.new
article.translations << Translation::Article.new(:title => 'My title',
                                                 :language => :en)

Slug

Slug are generated on translation creation. No change are made after.

Slug feature can be added to translated model:

class Article
  include Mongoid::Document

  # add Translate module
  include Mongoid::Translate

  # add Slug module
  include Mongoid::Translate::Slug

  # declare fields to be translate
  translate :title, :content

  # Define field on which slug will be build.
  slug_field :title

end

class Translation::Article
  include Mongoid::Document

  # add Translation module
  include Mongoid::Translation

  # add Slug module
  include Mongoid::Translation::Slug

end

Examples

Controller with InheritedResources

require 'mongoid/translate/resources'
include Mongoid::Translate::Resources

Use slug on views with to_slug

= link_to e.title, event_path(e.to_slug)

Use by_slug in controller with InheritedResources

def resource
  @event ||= Event.by_slug(params[:id]).one || Event.find(params[:id])
end

Copyright

Copyright (c) 2011-2013 af83

Released under the MIT license