Skip to content

Commit

Permalink
fixes theforeman#8874 - rework POT/PO updates for gettext 3's edit.po
Browse files Browse the repository at this point in the history
3.1.13 adds an intermediate .edit.po file alongside each .po, which is meant
to be kept outside of SCM and updated by users, whereupon it's merged back into
the .po on the next rake gettext:find execution.

Previously we overwrote all gettext .po file changes with files directly from
Transifex, but now these are downloaded to .edit.po and gettext merges them
back in.  Fuzzy merges have been disabled as they were inaccurate.
  • Loading branch information
Dominic Cleal authored and lzap committed Feb 3, 2016
1 parent 45ed087 commit 4406f5a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ doc/apidoc*
.ruby-version*
.ruby-gemset*
locale/*.mo
locale/*/*.edit.po
locale/*/*.po.time_stamp
locale/*/*.pox
locale/*/LC_MESSAGES
coverage/
Expand Down
2 changes: 1 addition & 1 deletion .tx/config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
host = https://www.transifex.com

[foreman.foreman]
file_filter = locale/<lang>/foreman.po
file_filter = locale/<lang>/foreman.edit.po
source_file = locale/foreman.pot
source_lang = en
type = PO
2 changes: 1 addition & 1 deletion bundler.d/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
gem 'rubocop', '0.35.1'

# for generating i18n files
gem 'gettext', '~> 3.1', :require => false
gem 'gettext', '>= 3.2.1', '< 4.0.0', :require => false

# for generating foreign key migrations
gem 'immigrant', '~> 0.1'
Expand Down
5 changes: 5 additions & 0 deletions config/initializers/1_fast_gettext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
# work in all domains context by default (for plugins)
include FastGettext::TranslationMultidomain

# Keep TRANSLATORS comments
Rails.application.config.gettext_i18n_rails.xgettext = %w[--add-comments=TRANSLATORS:]
# Disable fuzzy .po merging
Rails.application.config.gettext_i18n_rails.msgmerge = %w[--no-fuzzy-matching]

# When mark_translated setting is set, we will wrap all translated strings
# which is useful when translating code base.
if SETTINGS[:mark_translated] and not Rails.env.test?
Expand Down
10 changes: 7 additions & 3 deletions lib/tasks/gettext.rake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ begin

desc 'Extract plugin strings - called via rake plugin:gettext[plugin_name]'
task 'plugin:gettext', :engine do |t, args|
@engine = "#{args[:engine].camelize}::Engine".constantize
@domain = args[:engine]
@engine = "#{@domain.camelize}::Engine".constantize
@engine_root = @engine.root

namespace :gettext do
Expand All @@ -31,10 +32,13 @@ begin
def files_to_translate
Dir.glob("#{@engine.root}/{app,db,lib,config,locale}/**/*.{rb,erb,haml,slim,rhtml,js}")
end

def text_domain
@domain
end
end

Foreman::Gettext::Support.add_text_domain args[:engine], "#{@engine_root}/locale"
ENV['TEXTDOMAIN'] = args[:engine]
Foreman::Gettext::Support.add_text_domain @domain, "#{@engine_root}/locale"

Rake::Task['gettext:find'].invoke
end
Expand Down
5 changes: 1 addition & 4 deletions lib/tasks/locale.rake
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ namespace :locale do
find_dependencies = [:find_model, :find_code]
find_dependencies.shift if ENV['SKIP_MODEL']
task :find => find_dependencies do
# do not commit PO string merge into git (we are using transifex.com)
`git checkout -- locale/*/*.po`

# find malformed strings
errors = File.open("locale/#{DOMAIN}.pot") {|f| f.grep /(%s.*%s|#\{)/}
if errors.count > 0
Expand All @@ -46,7 +43,7 @@ namespace :locale do
end

desc 'Alias for gettext:po_to_json'
task :po_to_json => "gettext:po_to_json"
task :po_to_json => Dir['locale/*/foreman.po'].push("gettext:po_to_json")

desc 'Alias for gettext:pack'
task :pack => "gettext:pack"
Expand Down
26 changes: 11 additions & 15 deletions locale/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
DOMAIN = foreman
POTFILE = $(DOMAIN).pot
MOFILE = $(DOMAIN).mo
POFILES = $(shell find . -name '*.po')
POFILES = $(shell find . -name '$(DOMAIN).po')
MOFILES = $(patsubst %.po,%.mo,$(POFILES))
POXFILES = $(patsubst %.po,%.pox,$(POFILES))
EDITFILES = $(patsubst %.po,%.edit.po,$(POFILES))

%.mo: %.po
mkdir -p $(shell dirname $@)/LC_MESSAGES
Expand All @@ -29,31 +30,26 @@ all-mo: $(MOFILES)
! grep -q msgid $@

check: $(POXFILES)
msgfmt -c ${POTFILE}

# Merge PO files
update-po:
for f in $(shell find ./ -name "*.po") ; do \
msgmerge -N --backup=none -U $$f ${POTFILE} ; \
done

# Unify duplicate translations
uniq-po:
for f in $(shell find ./ -name "*.po") ; do \
msguniq $$f -o $$f ; \
done

tx-pull:
tx-pull: $(EDITFILES)
tx pull -f
-git commit -a -m "i18n - extracting new, pulling from tx"

# Extract strings and update the .pot, prepare .edit.po files
extract-strings:
bundle exec rake locale:po_to_json locale:find DOMAIN=$(DOMAIN) SKIP_MODEL=1
bundle exec rake locale:find DOMAIN=$(DOMAIN) SKIP_MODEL=1

# Merge .edit.po into .po
update-po:
bundle exec rake locale:find locale:po_to_json DOMAIN=$(DOMAIN) SKIP_MODEL=1

tx-update: tx-pull extract-strings
# merging po files is unnecessary when using transifex.com (amend that)
git checkout -- ../locale/*/*po
git commit -a --amend -m "i18n - extracting new, pulling from tx"
tx-update: extract-strings tx-pull update-po
git commit -m "i18n - extracting new, pulling from tx" . ../app/assets/javascripts/locale
-echo Changes commited!

# Remove all MO files
Expand Down

0 comments on commit 4406f5a

Please sign in to comment.