Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@stephan-buckmaster
Created September 27, 2018 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephan-buckmaster/b51072ed733487a450be2434964e9d37 to your computer and use it in GitHub Desktop.
Save stephan-buckmaster/b51072ed733487a450be2434964e9d37 to your computer and use it in GitHub Desktop.
Collapse po files
#!/usr/bin/ruby
# Collapses Gettext po files. for easy diff'ing
# Drops comments, and collapses quotes into one line.
# Example of what a po-file is: https://docs.transifex.com/formats/gettext
# Expanding the script by Raphaël Kolm at https://medium.com/@rkz_io/making-a-clutter-free-git-diff-on-gettext-translation-files-9c0c1bb1d8aa
# Sample output:
# 00001 msgid "is english def"
# 00001 msgstr "est anglais"
# 00002 msgid "good"
# 00002 msgstr "bien"
# 00003 msgstr "nice job"
# 00003 msgid "bon travail"
# Gettext: https://www.gnu.org/software/gettext/
lines = []
f = File.open(ARGV.first, "r:UTF-8") # you might be working with other kinds of encodings.
preamble_passed = false
f.readlines.each do |line|
next unless preamble_passed || line =~ /^msg/
preamble_passed = true
lines << line if line =~ /^\"|^msg/
end
f.close
collapsed_lines = []
collect_quoted = false
lines.each do |l|
if l =~ /^msg/
collect_quoted = false
collapsed_lines << l.chomp
else
collect_quoted = true
collapsed_lines.last << l.chomp
end
end
translations = {}
(collapsed_lines.length/2).times do |n|
key = collapsed_lines[2*n]
value = collapsed_lines[2*n+1]
translations[key] = value
end
index = 1
translations.sort.each do |k,v|
puts '%05d %s' % [index, k]
puts '%05d %s' % [index, v]
index += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment