require 'find'
excludes = [".svn", "lib", "modules", "feathers"]
exclude_files = []
strings = []
lines = []
output = ""
Find.find(".") do |path|
if FileTest.directory?(path)
if excludes.include?(File.basename(path))
Find.prune
else
next
end
else
filename = File.basename(path)
if filename =~ /\.php/ and not exclude_files.include?(filename)
cleaned = path.sub("./", "")
contents = File.read(path)
if contents =~ /sprintf\(__\("(.*?)"\), ([^\)]+)\)/
counter = 1
File.open(path, "r") do |infile|
while (line = infile.gets)
line.gsub(/sprintf\(__\("(.*?)"\), ([^\)]+)\)/) do
text = $1
unless strings.include?(text)
output << '#: '+cleaned+':'+counter.to_s+"\n"
output << '#, php-format'+"\n"
output << 'msgid "'+text+'"'+"\n"
output << 'msgstr ""'+"\n\n"
strings << text
lines << cleaned+":"+counter.to_s
else
output = output.gsub("#, php-format\nmsgid \""+text+"\"\nmsgstr \"\"\n\n",
"#: "+cleaned+":"+counter.to_s+"\n#, php-format\nmsgid \""+text+"\"\nmsgstr \"\"\n\n")
end
end
counter = counter + 1
end
end
end
if contents =~ /__\("(.*?)"\)/
counter = 1
File.open(path, "r") do |infile|
while (line = infile.gets)
line.gsub(/__\("(.*?)"\)/) do
text = $1
unless strings.include?(text)
output << '#: '+cleaned+':'+counter.to_s+"\n"
output << 'msgid "'+text+'"'+"\n"
output << 'msgstr ""'+"\n\n"
strings << text
else
unless lines.include?(cleaned+":"+counter.to_s)
output = output.gsub("msgid \""+text+"\"\nmsgstr \"\"\n\n",
"#: "+cleaned+":"+counter.to_s+"\nmsgid \""+text+"\"\nmsgstr \"\"\n\n")
end
end
end
counter = counter + 1
end
end
end
end
end
end
puts '# Chyrp Translation File.'
puts '# Copyright (C) 2007 Alex Suraci'
puts '# This file is distributed under the same license as the Chyrp package.'
puts '# Alex Suraci <(snipped)>, 2007.'
puts '#'
puts '#, fuzzy'
puts 'msgid ""'
puts 'msgstr ""'
puts '"Project-Id-Version: Chyrp v1.0 Beta\n"'
puts '"Report-Msgid-Bugs-To: (snipped)\n"'
puts '"POT-Creation-Date: 2007-08-03 00:29-0500\n"'
puts '"PO-Revision-Date: '+Time.now.strftime("%Y-%m-%d %H:%M")+'-0500\n"'
puts '"Last-Translator: Alex Suraci <(snipped)>\n"'
puts '"Language-Team: English (en) <(snipped)>\n"'
puts '"MIME-Version: 1.0\n"'
puts '"Content-Type: text/plain; charset=UTF-8\n"'
puts '"Content-Transfer-Encoding: 8bit\n"'
puts ''
puts output
Refactorings
No refactoring yet !
Alex Suraci
December 10, 2007, December 10, 2007 06:08, permalink
Added support for domains and base directory specifying, made the regexp searching less generous.
require 'find'
excludes = [".svn", "lib", "feathers"]
exclude_files = []
strings = []
lines = []
output = ""
basedir = ARGV[0] || "."
Find.find(basedir) do |path|
if FileTest.directory?(path)
if excludes.include?(File.basename(path))
Find.prune
else
next
end
else
filename = File.basename(path)
if filename =~ /\.php/ and not exclude_files.include?(filename)
cleaned = path.sub("./", "")
contents = File.read(path)
if contents =~ /sprintf\(__\(([^\)]+)\), ([^\)]+)\)/
counter = 1
File.open(path, "r") do |infile|
while (line = infile.gets)
line.gsub!("\\\"", "{QUOTE}")
line.gsub(/sprintf\(__\("([^"]+)"(, "[^"]+")?\), ([^\)]+)\)/) do
text = $1.gsub("{QUOTE}", "\\\"")
unless strings.include?(text)
output << '#: '+cleaned+':'+counter.to_s+"\n"
output << '#, php-format'+"\n"
output << 'msgid "'+text+'"'+"\n"
output << 'msgstr ""'+"\n\n"
strings << text
lines << cleaned+":"+counter.to_s
else
output = output.gsub("#, php-format\nmsgid \""+text+"\"\nmsgstr \"\"\n\n",
"#: "+cleaned+":"+counter.to_s+"\n#, php-format\nmsgid \""+text+"\"\nmsgstr \"\"\n\n")
end
end
counter = counter + 1
end
end
end
if contents =~ /__\((.*?)\)/
counter = 1
File.open(path, "r") do |infile|
while (line = infile.gets)
line.gsub!("\\\"", "{QUOTE}")
line.gsub(/__\("([^"]+)"(, "[^"]+")?\)/) do
text = $1.gsub("{QUOTE}", "\\\"")
unless strings.include?(text)
output << '#: '+cleaned+':'+counter.to_s+"\n"
output << 'msgid "'+text+'"'+"\n"
output << 'msgstr ""'+"\n\n"
strings << text
else
unless lines.include?(cleaned+":"+counter.to_s)
output = output.gsub("msgid \""+text+"\"\nmsgstr \"\"\n\n",
"#: "+cleaned+":"+counter.to_s+"\nmsgid \""+text+"\"\nmsgstr \"\"\n\n")
end
end
end
counter = counter + 1
end
end
end
end
end
end
puts '# Chyrp Translation File.'
puts '# Copyright (C) 2007 Alex Suraci'
puts '# This file is distributed under the same license as the Chyrp package.'
puts '# Alex Suraci <suracil.icio.us@gmail.com>, 2007.'
puts '#'
puts '#, fuzzy'
puts 'msgid ""'
puts 'msgstr ""'
puts '"Project-Id-Version: Chyrp v1.0 Beta\n"'
puts '"Report-Msgid-Bugs-To: suracil.icio.us@gmail.com\n"'
puts '"POT-Creation-Date: 2007-08-03 00:29-0500\n"'
puts '"PO-Revision-Date: '+Time.now.strftime("%Y-%m-%d %H:%M")+'-0500\n"'
puts '"Last-Translator: Alex Suraci <suracil.icio.us@gmail.com>\n"'
puts '"Language-Team: English (en) <suracil.icio.us@gmail.com>\n"'
puts '"MIME-Version: 1.0\n"'
puts '"Content-Type: text/plain; charset=UTF-8\n"'
puts '"Content-Transfer-Encoding: 8bit\n"'
puts ''
puts output
Eiso
July 29, 2009, July 29, 2009 15:28, permalink
Thank you very much! I was able to quickly edit the first version of your code to work generate/create PO files from Wordpress templates. You saved me a lot of time, thanks again, it's much appreciated. I have added the code below.
require 'find'
system(`rm -f template.po`)
po_file = File.open("template.po", 'a')
excludes = [".svn", "lib", "modules", "feathers"]
exclude_files = []
strings = []
lines = []
output = ""
Find.find(".") do |path|
if FileTest.directory?(path)
if excludes.include?(File.basename(path))
Find.prune
else
next
end
else
filename = File.basename(path)
if filename =~ /\.php/ and not exclude_files.include?(filename)
cleaned = path.sub("./", "")
contents = File.read(path)
if contents =~ /_e\(['|"](.*?)['|"], ['|"]twollars['|"]/
counter = 1
File.open(path, "r") do |infile|
while (line = infile.gets)
line.gsub(/_e\(['|"](.*?)['|"], ['|"]twollars['|"]/) do
text = $1
unless strings.include?(text)
output << '#: '+cleaned+':'+counter.to_s+"\n"
output << 'msgid "'+text+'"'+"\n"
output << 'msgstr ""'+"\n\n"
strings << text
else
unless lines.include?(cleaned+":"+counter.to_s)
output = output.gsub("msgid \""+text+"\"\nmsgstr \"\"\n\n",
"#: "+cleaned+":"+counter.to_s+"\nmsgid \""+text+"\"\nmsgstr \"\"\n\n")
end
end
end
counter = counter + 1
end
end
end
end
end
end
po_file.puts '# Chyrp Translation File.'
po_file.puts '# Copyright (C) 2007 Alex Suraci'
po_file.puts '# This file is distributed under the same license as the Chyrp package.'
po_file.puts '# Alex Suraci <(snipped)>, 2007.'
po_file.puts '#'
po_file.puts '#, fuzzy'
po_file.puts 'msgid ""'
po_file.puts 'msgstr ""'
po_file.puts '"Project-Id-Version: Chyrp v1.0 Beta\n"'
po_file.puts '"Report-Msgid-Bugs-To: (snipped)\n"'
po_file.puts '"POT-Creation-Date: 2007-08-03 00:29-0500\n"'
po_file.puts '"PO-Revision-Date: '+Time.now.strftime("%Y-%m-%d %H:%M")+'-0500\n"'
po_file.puts '"Last-Translator: Alex Suraci <(snipped)>\n"'
po_file.puts '"Language-Team: English (en) <(snipped)>\n"'
po_file.puts '"MIME-Version: 1.0\n"'
po_file.puts '"Content-Type: text/plain; charset=UTF-8\n"'
po_file.puts '"Content-Transfer-Encoding: 8bit\n"'
po_file.puts ''
po_file.puts output
Eiso
July 29, 2009, July 29, 2009 15:30, permalink
Sorry I forgot to add that this is designed to work for
_e('Text here', 'twollars');
Where one should replace the word Twollars with the name of the template file.
This recursively goes through any folders that aren't excluded, starting at the base directory. It opens each PHP file, searches for gettext functions (__("text")), and outputs the resulting .po code. It has support for multiple occurrences and sprintf gettext translations.
I'm a novice at Ruby so this is probably loaded with bad practice and redundancy, but I want to see how it can be made better.