Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoid_globalize

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoid_globalize

  • 0.2.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

h1. "!https://secure.travis-ci.org/Mik-die/mongoid_globalize.png!":http://travis-ci.org/Mik-die/mongoid_globalize Mongoid::Globalize

Mongoid::Globalize is based on Globalize3, but targeted at Mongoid. As Globalize3, it is compatible with and builds on the new "I18n API in Ruby on Rails":http://guides.rubyonrails.org/i18n.html and adds model translations to Mongoid::Document.

Mongoid::Globalize has to work with Rails3+ and other frameworks, that supports Mongoid (Sinatra, Padrino, and others).

h2. Requirements

Mongoid 2.3. Mongoid 2.4 break some specs, so it's not supported yet. Though you can use master branch of gem.

h2. Installation

To install Mongoid::Globalize just use:


$ gem install mongoid_globalize

or, with bundler, in your Gemfile


gem 'mongoid_globalize'

h2. Document translations

Document translations allow you to translate your document' field values. In order to make this work, you'll need to add +include Mongoid::Globalize+ into your document class and define translatable fields in block of +translates+ method, as you define usual fields. E.g.


class Post
  include Mongoid::Document
  include Mongoid::Globalize
  translates do
    field :title
    field :content
    field :published, type: Boolean
  end
end

This allows you to translate the fields :title, :text and :published per locale:


I18n.locale = :en
post.title # => Globalize rocks!

I18n.locale = :ru
post.title # => Глобалайз рулит!

h2. I18n fallbacks for empty translations

It is possible to enable fallbacks for empty translations. It will depend on the configuration setting you have set for I18n translations in your config.

In Rails applications, for example, you can enable them by adding the next line to @config/application.rb@ (or only @config/environments/production.rb@ if you only want them in production)

config.i18n.fallbacks = true

By default, Mongoid::Globalize will only use fallbacks when your translation model does not exist or the translation value for the item you've requested is @nil@. However it is possible to also use fallbacks for @blank@ translations by adding @fallbacks_for_empty_translations!@ to the @translates@ block.


  class Post
    include Mongoid::Document
    include Mongoid::Globalize
    translates do
      field :title
      field :content
    end
  end

  puts post.translations.inspect
  # => [#, #]

  I18n.locale = :en
  post.title # => 'Globalize rocks!'
  post.name  # => 'Globalize'

  I18n.locale = :nl
  post.title # => ''
  post.name  # => 'Globalize'

  class Post
    include Mongoid::Document
    include Mongoid::Globalize
    translates do
      field :title
      field :content
      fallbacks_for_empty_translations!
    end
  end

  puts post.translations.inspect
  # => [#, #]

  I18n.locale = :en
  post.title # => 'Globalize rocks!'
  post.name  # => 'Globalize'

  I18n.locale = :nl
  post.title # => 'Globalize rocks!'
  post.name  # => 'Globalize'

h2. Scoping objects by those with translations

To only return objects that have a translation for the given locale we can use the with_translations scope. This will only return records that have a translations for the passed in locale.



Post.with_translations('en') # => [#, #]

Post.with_translations(I18n.locale) # => [#, #]

Post.with_translations('de') # => []

FAQs

Package last updated on 23 Feb 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc