Created
August 19, 2013 21:44
-
-
Save ershad/6274547 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
# shell$ irb | |
# 1.9.3p194 :001 > require '~/Desktop/Po' | |
# 1.9.3p194 :002 > Po.new('~/code/translation/evolution-data-server.master.ml.po').split(3, 'evolution.po') | |
# Files named 1_evolution.po, 2_evolution.po, 3_evolution.po will be written to your current working directory. | |
class Po | |
attr_accessor :po_file_array | |
def initialize(file) | |
@po_file_array = File.open(file, 'r') { |f| f.read }.split("\n\n") | |
end | |
def split(n, file) | |
slice_count = empty_or_fuzzy_translations.count / n | |
sliced = empty_or_fuzzy_translations.each_slice(slice_count).to_a | |
sliced.each_with_index do |slice, i| | |
slice += sliced.delete(sliced.last) if i == (sliced.count-1) && slice_count != sliced.count | |
File.open("#{i+1}_#{file}", 'w') { |f| f.write ([header] + slice).join("\n"*2).gsub("\n"*3, "\n"*2) } | |
end | |
end | |
def header | |
po_file_array.first | |
end | |
private | |
def empty_or_fuzzy_translations | |
po_file_array[1..-1].select do |po_line| | |
po_line.include?('msgstr ""') or po_line.include?('#, fuzzy') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment