我正在开发一个多语言(EN / RU)网站,我需要一个允许我翻译数据库记录的解决方案?
是否有任何第三方插件可以让您维护多语种网站?理想情况下,它应该与rails_admin(或最坏情况下的脚手架)和routing-filter一起使用。
我现在使用globalize3,它也会通过rails_admin创建翻译。但是没有办法指定语言环境。它只为您当前的语言环境创建翻译,即无法真正翻译记录,因为它只是复制原始条目。我认为无法创建英文翻译。
提前致谢!
的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'
配置/ application.rb中
config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :ru
模型/ page.rb
class Page < ActiveRecord::Base
translates :title, :content
validates_presence_of :title
end
Rails控制台输出:
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
答案 0 :(得分:4)
如果您只有两种语言,最好将翻译保留在同一个表格中,例如:
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
然后扩展ActiveRecord::Base
类以提供翻译方法:
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
之后,从您的模型中调用该方法:
class Article < ActiveRecord::Base
...
translate :title, :body
...
end
现在,您可以在不title_ru
的情况下修改title_en
和globalize3
条目。
答案 1 :(得分:1)
Globalize3会覆盖following methods,允许您在保存属性时指定区域设置。
product.write_attribute(:title, 'Hows going', locale:'ua')
product.save!
同样适用于read_attribute
。由于这是没有记录的,我不确定使用它是否会产生任何不良后果。
答案 2 :(得分:1)