Coder Social home page Coder Social logo

trasto's Introduction

Trasto

Build Status

Translatable columns for Rails 4, directly stored in a postgres hstore in the model table.

Inspired by Barsoom's traco.

To store translations outside the model, see Sven Fuchs' globalize3.

Usage

Say you want Post#title and Post#body to support both English and Swedish values.

Write a migration to get hstore database columns with i18n suffixes, e.g. title_i18n and body_i18n, like:

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.hstore :title_i18n
      t.hstore :body_i18n

      t.timestamps
    end
  end
end

Don't create title or body columns without the _i18n suffix, Trasto will define a method with that name.

Declare these columns in the model:

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

You can still use your accessors for title_i18n and title_i18= in forms, validations and other code, but you also get:

#title: Shows the title in the current locale. If blank, falls back to default locale, then to any locale.

#title=: Assigns the title to the column for the current locale, if present.

.locales_for_column(:title): Returns an array like [:de, :en] sorted with default locale first and then all currently available locals sorted alphabetically.

Installation

Add this to your Gemfile:

  gem 'trasto'

You might need to enable the hstore extension for your database. Create a migration containing the necessary instruction:

class EnableHstore < ActiveRecord::Migration
  def change
    enable_extension 'hstore'
  end
end

Running the tests

    bundle exec appraisal install
    bundle exec appraisal rake

To run only one appraisal/gemfile:

bundle exec appraisal 4.2 rake

Credits and license

By Morton Jonuschat under the MIT license:

Copyright (c) 2012-2015 Morton Jonuschat

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

trasto's People

Contributors

joallard avatar mjonuschat avatar mkamensky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

trasto's Issues

Validation Methods

It would be great to have some convenience methods to validate that there is a translation, to mimic validated_presence_of

Im thinking if you only need a translation:

validates_presence_of_translation :title

and if you need to have a translation in a fixed amount of locales:

validates_presence_of_translation :title, locales: [:en, :fr]

Specify Fallback Locale

Hi,

Is there anyway to define fallback locales incase there isn't a value defined for the current locale?

Add Travis hook

As we are now Travis-ready, I think it would be nice to activate the setting in Github. I thought I could, but it appears even my Collaborator badge doesn't allow me to touch the settings. On to you, @yabawock!

pg version

Is there a good reason for keeping the pg version below 1.0? I removed it and found no issues yet

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.