I'm developing a multilingual (EN/RU) website and I need a solution allowing me to translate database records?
Are there any third-party plugins allowing you to maintain a multilingual website? Ideally it should work with rails_admin (or scaffolding at the worst case) and and routing-filter.
I use globalize3 now and it creates a translation via rails_admin as well. But there's no way to specify the locale. It only creates a translation for your current locale, i.e. there's no way to translate records really because it just copies the original entry. And I see no way to create english translations.
Thanks in advance!
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.0'
gem 'sqlite3'
gem 'pg', :require => 'pg'
gem 'memcache-client'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'russian'
gem 'dynamic_form'
gem 'friendly_id', '~> 4.0.0'
gem 'routing-filter'
gem 'devise'
gem 'cancan'
gem 'paper_trail', '~> 2'
gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'
gem 'globalize3', '~> 0.2.0.beta6', :git => 'git://github.com/svenfuchs/globalize3.git'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
gem 'unicorn'
gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
config/application.rb
config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :ru
models/page.rb
class Page < ActiveRecord::Base
translates :title, :content
validates_presence_of :title
end
Rails console output:
Loading development environment (Rails 3.2.0)
1.9.3p0 :001 > p = Page.new(:title => 'Test 1')
Page::Translation Load (0.2ms) SELECT "page_translations".* FROM "page_translations" WHERE "page_translations"."page_id" IS NULL AND "page_translations"."locale" = 'ru' LIMIT 1
=> #<Page id: nil, title: "Test 1", slug: nil, content: nil, created_at: nil, updated_at: nil>
1.9.3p0 :002 > p.save
(0.1ms) begin transaction
SQL (13.9ms) INSERT INTO "pages" ("content", "created_at", "slug", "title", "updated_at") VALUES (?, ?, ?, ?, ?) [["content", nil], ["created_at", Tue, 24 Jan 2012 14:01:28 MSK +04:00], ["slug", nil], ["title", "Test 1"], ["updated_at", Tue, 24 Jan 2012 14:01:28 MSK +04:00]]
SQL (0.5ms) INSERT INTO "page_translations" ("content", "created_at", "locale", "page_id", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", nil], ["created_at", Tue, 24 Jan 2012 14:01:28 MSK +04:00], ["locale", "ru"], ["page_id", 5], ["title", nil], ["updated_at", Tue, 24 Jan 2012 14:01:28 MSK +04:00]]
Page::Translation Load (0.1ms) SELECT "page_translations".* FROM "page_translations" WHERE "page_translations"."page_id" = 5 AND "page_translations"."locale" = 'ru' LIMIT 1
(0.4ms) UPDATE "page_translations" SET "title" = 'Test 1', "updated_at" = '2012-01-24 10:01:28.298579' WHERE "page_translations"."id" = 5
Page::Translation Load (0.1ms) SELECT "page_translations".* FROM "page_translations" WHERE "page_translations"."id" = ? LIMIT 1 [["id", 5]]
(2.2ms) commit transaction
=> true
If you have only two languages, it's better to keep translations in the same table, e.g.:
create_table "articles" do |t|
...
t.column "title_ru", :string
t.column "title_en", :string
t.column "body_ru", :text
t.column "body_en", :text
...
end
And then extend ActiveRecord::Base
class to provide method for translation:
module Translate
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def translate *columns
columns.each do |column|
class_eval <<-EOV
def #{column}
unless self.send("#{column}_#{I18n.locale}")
self.send("#{column}_#{I18n.locale}")
else
#{column}_#{I18n.default_locale}
end
end
EOV
end
end
end
end
ActiveRecord::Base.send :include, Translate
After that, call that method from your model:
class Article < ActiveRecord::Base
...
translate :title, :body
...
end
And now you can edit both title_ru
and title_en
entries without globalize3
.