Skip to content

Instantly share code, notes, and snippets.

@krunal
Last active August 29, 2015 14:17
Show Gist options
  • Save krunal/f3c8a543982aba01dd7b to your computer and use it in GitHub Desktop.
Save krunal/f3c8a543982aba01dd7b to your computer and use it in GitHub Desktop.
Import/Export translations - Tr8n Translation Engine
Steps to copy translations from Server A to Server B
------------------------------------------------------------------------
1) ssh to Server A
2) Execute -> rake tr8n:export_translations
1) Rake will nullify synced_at column
2) Rake will restart Memcached
3) Rake will prompt you to enter a filename
4) Rake will create a new filename.yml file inside your root directory.
3) scp this filename.yml from 'Server A' to 'Server B'
4) ssh to Server B
5) Execute -> rake tr8n:import_translations
1) Rake will restart Memcached
2) Rake will prompt you enter a filename
3) Translations will be imported from the specified file
6) Done!
------------------------------------------------------------------------
namespace :tr8n do
desc "Export translations into a file."
task :export_translations => :environment do
Tr8n::TranslationKey.update_all("synced_at = NULL")
system("/etc/init.d/memcached restart")
puts "Cleared cache data.."
STDOUT.puts "Please enter filename(don't include extension) -> "
filename = STDIN.gets.strip
payload = []
begin
Tr8n::TranslationKey.where("synced_at is null or updated_at > synced_at")
.find_in_batches( :batch_size => 1000 ) do | changed_keys |
changed_keys.each do |tkey|
tkey_hash = tkey.to_sync_hash(:languages => Tr8n::Language.enabled_languages)
payload << tkey_hash
tkey.mark_as_synced!
print "."
end
end
ensure
puts "Writing to file..."
File.write("#{filename}.yml", payload.to_yaml)
puts "Exported '#{Rails.root}/#{filename}.yml'"
end
puts "Done!"
end
desc "Import translations from a file."
task :import_translations => :environment do
system("sudo /etc/init.d/memcached restart")
puts "Cleared cache data.."
STDOUT.puts "Please enter filename(don't include extension) -> "
filename = STDIN.gets.strip
if File.exist?("#{filename}.yml")
translations = YAML::load_file "#{filename}.yml"
translations.each do |tkey_hash|
tkey, translations = Tr8n::TranslationKey.create_from_sync_hash(tkey_hash, Tr8n::Config.system_translator)
tkey.mark_as_synced! if tkey.present?
print "."
end
puts "Import completed."
puts "Done!"
else
puts "File is not exist in the '#{Rails.root}/#{filename}.yml'"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment