Giter Club home page Giter Club logo

translatable_columns's Introduction

TranslatableColumns

TranslatableColumns is a Ruby on Rails plugin, providing localization for model attributes.

Installing

1. First make sure you’re running Rails >= 2.2 or edge:

rake rails:freeze:edge

2. Install the plugin:

./script/plugin install git://github.com/iain/translatable_columns.git

3. Create or modify a model to have multiple columns for one attribute:

./script/generate model Topic title_en:string title_nl:string title_de:string title_fr:string

Usage

Identify the columns you want to translate:

class Topic < ActiveRecord::Base translatable_columns :title end

And you’re done!

Create a form like this:

<% form_for(@topic) do |f| %> <%= f.text_field :title %> <% end %>

And it will save to whatever locale is set in I18n. No hard feelings, nothing to worry about.

Validating

Validation is of course built in. If you want to validate the presence of at least one of the translations, just call validates_translation_of:

class Topic < ActiveRecord::Base translatable_columns :title validates_translation_of :title end

This will make your record invalid when none of the translated columns exist. It works exactly as validates_presence_of, including all its options!

Customizing

You can change the settings of translatable_columns on both a global level and at individual attribute level. There are two configuration options at the moment, called full_locale and use_default.

Set the global configuration in your environment file. These are the defaults of translatable_columns:

ActiveRecord::Base.translatable_columns_config.full_locale = false ActiveRecord::Base.translatable_columns_config.use_default = true

full_locale

With this option you can change which part of the locale is used in the columns. Default is false, so only the first part of the locale is expected in the column. So a title for en-US is called title_en and a title for en-GB is also called title_en. When you set full_locale to true, it uses the entire locale, substituting the hyphen with an underscore. This way a title for en-US is called title_en_us and a title for en-GB is called title_en_gb.

full_locale cannot be set per attribute just now.

use_default

With this option you can specify which value will be returned automatically if no proper value has been found. Default is true, so it will try harder to find a value. It might even be a value in another language.

You can set this option per attribute if you’d like, to override the global config.

class Topic < ActiveRecord::Base translatable_columns :title, :use_default => false end

Some extras

What if the user has selected a locale which you don’t have in the database? In this case it’ll get the column belonging to the I18n.default_locale. Make sure you have a column for this locale, because you’ll be serving a nasty error if even this one isn’t present!

You might want to provide multiple languages for a user to fill in at once. This is one way to do it:

<% form_for(@topic) do |f| %> <% Topic.available_translatable_columns_of(:title).each do |attribute| %> <%= f.text_field attribute %> <% end %> <% end %>

Copyright © 2008 Iain Hecker, released under the MIT license

translatable_columns's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

translatable_columns's Issues

error with validate_translation_of

When I'm trying to use the above I get an exception:

irb(main):005:0> f=Faculty.first
NoMethodError: undefined method validation_method' for #<Class:0xb70b12d0> from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/base.rb:1016:inmethod_missing_without_paginate'
from /usr/lib/ruby/gems/1.8/gems/will_paginate-2.3.15/lib/will_paginate/finder.rb:170:in method_missing' from /home/warden/rails_apps/pirate/vendor/plugins/translatable_columns/lib/translatable_columns.rb:141:invalidates_translation_of'
from /home/warden/rails_apps/pirate/app/models/faculty.rb:5
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:454:in load' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:454:inload_file'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:591:in new_constants_in' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:453:inload_file'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:340:in require_or_load' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:491:inload_missing_constant'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:183:in const_missing' from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:181:ineach'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0/lib/active_support/dependencies.rb:181:in `const_missing'

Model:
class Faculty < ActiveRecord::Base
belongs_to :university

translatable_columns :short, :name
validates_translation_of :short, :name

end

Why do you think this solution is outdated?

Hello,

on your blog where you introduce translateable_columns, you also mention that you think this solution is outdated.
I am in the process of internationalizing a Rails 3 application where about 50 tables contain translatable columns, and I like your solution a lot, better than Globalize3's solution of creating a second table for each translatable table.

What alternatives are there to your solution? Does this not work in Rails 3 any more?

Thanks,
Jens

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.