Skip to content

Instantly share code, notes, and snippets.

@karmi
Created August 5, 2008 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save karmi/4068 to your computer and use it in GitHub Desktop.
Save karmi/4068 to your computer and use it in GitHub Desktop.
Rake task to extract texts from Ruby/ERb source in your application for the Gibberish plugin dictionary
# Rake task to extract texts from Ruby/ERb source in your application
# Scans patterns like "Hello World"[:hello_world] and dumps them into RAILS_ROOT/lang/new_language.yml
# TODO: Process incrementally, ie. dump newly added strings into existing localization files
require 'fileutils'
namespace :localize do
desc "Extract all texts prepared to be translated from Ruby source"
task :extract do
count, keys, out = 0, [], "# Localization dictionary for the 'Gibberish' plugin (#{RAILS_ROOT.split('/').last})\n\n"
Dir["#{RAILS_ROOT}/app/**/*"].sort.each do |path|
unless ( matches = File.new(path).read.scan(/['"]([^'"]*)['"]\[\:([a-z1-9\_]*)\]/) ).empty?
print "."
out << "# -- #{File.basename(path)}:\n"
matches.each do |m|
out << "#{m[1]}: #{m[0]}\n" unless keys.include? m[1]
keys << m[1]
end
out << "\n"
count +=1
end if FileTest.file? path
end
FileUtils.mkdir_p File.join(RAILS_ROOT, 'lang') # Ensure we have lang dir
File.open( File.join(RAILS_ROOT, 'lang', 'new_language.yml'), "w") { |file| file << out }
puts "\nProcessed #{count} files and dumped YAML into #{RAILS_ROOT}/lang/new_language.yml"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment