i18n-active_record. , locales database.
translation , relation domain, domain , gem .
== > , - , , thread env, .
# login-label-test.com domain = Domain.where(: name = > namespace.split( "-" ). last).first
gem, - translation, , .
, , .
locale db
require 'yaml'
namespace :locale do
desc "Copy translations from yml yo db. Non-overwriting."
task :yml_to_db, [:environment, :locale,:domain] => :environment do |t, args|
info = YAML::load(IO.read("#{Rails.root}/config/locales/#{args[:locale]}-#{args[:domain]}.yml"))
domain = Domain.where(:name => args[:domain]).first
info["#{args[:locale]}"].each do |key, value|
translation = domain.translations.where(:key => key)
domain.translations.create!(:key => key, :value => value, :locale => args[:locale]) if translation.blank?
end
end
end
gem:
require 'active_record'
module I18n
module Backend
class ActiveRecord
class Translation < ::ActiveRecord::Base
belongs_to :domain
TRUTHY_CHAR = "\001"
FALSY_CHAR = "\002"
self.table_name = 'translations'
serialize :value
serialize :interpolations, Array
def lookup(keys, *separator)
column_name = connection.quote_column_name('key')
keys = Array(keys).map! { |key| key.to_s }
unless separator.empty?
warn "[DEPRECATION] Giving a separator to Translation.lookup is deprecated. " <<
"You can change the internal separator by overwriting FLATTEN_SEPARATOR."
end
namespace = "#{keys.last}#{I18n::Backend::Flatten::FLATTEN_SEPARATOR}%"
domain = Domain.where(:name => namespace.split("-").last).first
where("#{column_name} IN (?) OR #{column_name} LIKE ?", keys, namespace).where(:id => domain.id)
end
end
end
end
, .