GithubHelp home page GithubHelp logo

nasmorn / hstore_translate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leadformance/hstore_translate

0.0 2.0 0.0 108 KB

Rails I18n library for ActiveRecord model/data translation using PostgreSQL's hstore datatype

License: MIT License

Ruby 100.00%

hstore_translate's Introduction

hstore_translate

Rails I18n library for ActiveRecord model/data translation using PostgreSQL's hstore datatype. It provides an interface inspired by Globalize3 but removes the need to maintain separate translation tables.

Build Status

Requirements

  • ActiveRecord > 3.1.0
  • I18n

Installation

gem install hstore_translate

When using bundler, put it in your Gemfile:

source 'https://rubygems.org'

gem 'pg'
gem 'activerecord'
gem 'activerecord-postgres-hstore', '~> 0.4.0' # only required for ActiveRecord 3.x
gem 'hstore_translate'

Model translations

Model translations allow you to translate your models' attribute values. E.g.

class Post < ActiveRecord::Base
  translates :title, :text
end

Allows you to translate the attributes :title and :text per locale:

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

I18n.locale = :he
post.title # => אתר זה טוב

You also have locale-specific convenience methods from easy_globalize3_accessors:

I18n.locale = :en
post.title # => This database rocks!
post.title_he # => אתר זה טוב

In order to make this work, you'll need to define an hstore column for each of your translated attributes, using the suffix "_translations":

class CreatePosts < ActiveRecord::Migration
  def up
    create_table :posts do |t|
      t.column :title_translations, 'hstore'
      t.column :text_translations,  'hstore'
      t.timestamps
    end
  end
  def down
    drop_table :posts
  end
end

I18n fallbacks for missing translations

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

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

Sven Fuchs wrote a detailed explanation of the fallback mechanism.

Temporary disable fallbacks

If you've enabled fallbacks for missing translations, you probably want to disable them in the admin interface to display which translations the user still has to fill in.

From:

I18n.locale = :en
post.title # => This database rocks!
post.title_nl # => This database rocks!

To:

I18n.locale = :en
post.title # => This database rocks!
post.disable_fallback
post.title_nl # => nil

hstore_translate's People

Contributors

robworley avatar tompesman avatar

Watchers

Roman Almeida avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.