1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
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
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.