Skip to content

Instantly share code, notes, and snippets.

@p1nox
Last active December 12, 2015 09:58
Show Gist options
  • Save p1nox/4755262 to your computer and use it in GitHub Desktop.
Save p1nox/4755262 to your computer and use it in GitHub Desktop.
Copycopter Offline: using copycopter only on development environment
namespace :ccopter do
desc "Split copycopter yml file"
task :split_ccopter_file => :environment do
CCOPTER_FILE = "#{Rails.root}/config/locales/copycopter.yml"
created_files = 0
if File.exists?(CCOPTER_FILE)
CCOPTER = YAML.load_file(File.open(CCOPTER_FILE))
CCOPTER.each do |lang|
LANG_FILE = "#{Rails.root}/config/locales/#{lang[0]}.yml"
puts LANG_FILE
lang_file = File.new(LANG_FILE, "w")
lang_file.puts("#{lang[0]}:")
lang_file.puts(lang[1].to_yaml)
lang_file.close
created_files = created_files+1
end
else
puts "#{CCOPTER_FILE} have not been found, you should run"
end
puts "*** copycopter.yml splited into #{created_files} files"
end
desc "Delete generated yml files"
task :delete_lang_files => :environment do
CCOPTER_FILE = "#{Rails.root}/config/locales/copycopter.yml"
deleted_files = 0
if File.exists?(CCOPTER_FILE)
CCOPTER = YAML.load_file(File.open(CCOPTER_FILE))
CCOPTER.each do |lang|
LANG_FILE = "#{Rails.root}/config/locales/#{lang[0]}.yml"
if File.exists?(LANG_FILE)
puts LANG_FILE
File.delete(LANG_FILE)
deleted_files = deleted_files+1
else
puts " #{LANG_FILE} have not been found"
end
end
else
puts "#{CCOPTER_FILE} have not been found"
end
puts "*** #{deleted_files} yml files have been deleted"
end
end

Abstract

Using copycopter only for development environment and standard I18n translations files inside your application. Limit copycopter client only for development environment, edit whatever you want to edit through your copycopter application, download all cached blurbs to a yml file (copycopter.yml), split it and push all the generated yml files to your production application.

  • This method is intended for applications that don't need to be upgrading languages too often.

Pros

Forget latency between your application and copycopter application.

Cons

Updating translations require to push to your application environment all translations files.

Configuration

Put your copycopter_client gem on development group

#/Gemfile
group :development do
  gem 'copycopter_client', '~> 2.0.1'
end

Limit Copycopter initialization to development environment

#/config/initializers/copycopter.rb
if Rails.env.development?
  CopycopterClient.configure do |config|
    config.api_key = 'YOUR_API_KEY'
    config.host = 'YOUR_HOST'
  end
end

Paste ccopter.rake in your tasks directory lib/tasks/ccopter.rake

Usage

If you have your copycopter application up and with all blurbs working fine and published (rake copycopter:deploy) for your application on development environment, proceed with the process described below.

  • Export all cached blurbs to a yml file ( config/locales/copycopter.yml )

      rake copycopter:export
    
  • Split downloaded file in as many ymls as languages you have on your application

      rake ccopter:split_ccopter_file
    
  • Push all generated files to your production application and done, now you have your application running with out copycopter client, and taking all translations from your language files on config/locales

If you have any problem just delete all translations files and return to your previous configuration.

rake ccopter:delete_lang_files --trace
@kwerle
Copy link

kwerle commented Feb 11, 2013

Why?

@p1nox
Copy link
Author

p1nox commented Feb 11, 2013

Obtaining advantages of copycopter managing translations while using standard Rails I18n configuration on production application.

No latency between production application and copycopter application for blurbs downloading, there's no communication with copycopter application, all blurbs remains inside production application.

Of course, this is intended for applications that don't need to be upgrading languages too often.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment