Skip to content

Commit

Permalink
Merge branch 'tasks-for-translation-cleanup' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
crowbot committed May 5, 2016
2 parents d296f54 + 1cc6584 commit 5c61faf
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/initializers/alaveteli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
require 'alaveteli_geoip'
require 'default_late_calculator'
require 'analytics_event'
require 'alaveteli_gettext/fuzzy_cleaner'

AlaveteliLocalization.set_locales(AlaveteliConfiguration::available_locales,
AlaveteliConfiguration::default_locale)
Expand Down
39 changes: 39 additions & 0 deletions lib/alaveteli_gettext/fuzzy_cleaner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- encoding : utf-8 -*-
module AlaveteliGetText
class FuzzyCleaner
def clean_po(input)
lines = input.split("\n")

lines.each_with_index do |line, index|
match = /^msgid "(.*)"/.match(line)
if $1
if /^#, fuzzy/.match(lines[index-1])
# one line msgstr
if /^msgstr "(.+)"/.match(lines[index+1])
lines[index+1] = "msgstr \"\""
lines.delete_at(index-1)
end
# multiline msgstr
if /^msgstr ""/.match(lines[index+1])
while /^".+"/.match(lines[index+2])
lines.delete_at(index+2)
end
lines.delete_at(index-1)
end
# plural msgstr
if /^msgid_plural "(.*)"/.match(lines[index+1])
offset = 0
while /^msgstr\[#{offset}\] "(.*)"/.match(lines[index+2+offset])
lines[index+2+offset] = "msgstr[#{offset}] \"\""
offset += 1
end
lines.delete_at(index-1)
end
end
end
end

lines.join("\n") << "\n"
end
end
end
11 changes: 11 additions & 0 deletions lib/tasks/gettext.rake
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ namespace :gettext do
end
end

desc 'Remove fuzzy translations'
task :remove_fuzzy do
fuzzy_cleaner = GetText::FuzzyCleaner.new

Dir.glob("locale/**/app.po").each do |po_file|
lines = File.read(po_file)
output = fuzzy_cleaner.clean_po(lines)
File.open(po_file, "w") { |f| f.puts(output) }
end
end

desc 'Update locale files with slightly changed English msgids using a csv file of old to new strings'
task :update_msgids_from_csv do
mapping_file = find_mapping_file(ENV['MAPPING_FILE'])
Expand Down
100 changes: 100 additions & 0 deletions spec/lib/alaveteli_gettext/fuzzy_cleaner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# -*- encoding : utf-8 -*-
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe AlaveteliGetText::FuzzyCleaner do

describe '#clean_po' do

it 'removes the fuzzy marker and msgstr from a single-line msgstr' do
input = <<-EOF.strip_heredoc
msgid "Some msgid"
msgstr "Translated msgstr"
#, fuzzy
msgid "Fuzzy msgid"
msgstr "Fuzzy translation"
msgid "Another msgid"
msgstr "Another translated msgstr"
EOF

expected = <<-EOF.strip_heredoc
msgid "Some msgid"
msgstr "Translated msgstr"
msgid "Fuzzy msgid"
msgstr ""
msgid "Another msgid"
msgstr "Another translated msgstr"
EOF

expect(subject.clean_po(input)).to eq(expected)
end

it 'removes the fuzzy marker and msgstr from a multi-line msgstr' do
input = <<-EOF.strip_heredoc
msgid "Some msgid"
msgstr "Translated msgstr"
#, fuzzy
msgid "Multi-line fuzzy"
msgstr ""
"Fuzzy translation…"
"…over several "
"lines"
msgid "Another msgid"
msgstr "Another translated msgstr"
EOF

expected = <<-EOF.strip_heredoc
msgid "Some msgid"
msgstr "Translated msgstr"
msgid "Multi-line fuzzy"
msgstr ""
msgid "Another msgid"
msgstr "Another translated msgstr"
EOF

expect(subject.clean_po(input)).to eq(expected)
end

it 'removes the fuzzy marker and msgstrs from a plural msgstr' do
input = <<-EOF.strip_heredoc
msgid "Some msgid"
msgstr "Translated msgstr"
#, fuzzy
msgid "Fuzzy msgid"
msgid_plural "Plural fuzzy"
msgstr[0] "Fuzzy translation"
msgstr[1] "Plural translation"
msgstr[2] "Further plural"
msgid "Another msgid"
msgstr "Another translated msgstr"
EOF

expected = <<-EOF.strip_heredoc
msgid "Some msgid"
msgstr "Translated msgstr"
msgid "Fuzzy msgid"
msgid_plural "Plural fuzzy"
msgstr[0] ""
msgstr[1] ""
msgstr[2] ""
msgid "Another msgid"
msgstr "Another translated msgstr"
EOF

expect(subject.clean_po(input)).to eq(expected)
end

end

end

0 comments on commit 5c61faf

Please sign in to comment.