Commit dfabf7fb844f3f481556c73412ceeeeebb831172

Authored by Antonio Terceiro
1 parent e9bffd38

Support plugin translations in the plugins themselves

When this is merged, the script at
script/move-translations-to-plugins.rb must be executed to move the
translations that are currently in the core to their respective plugins,
i.e. to initialize each plugin .po file with their existing translated
strings, excluding translations that already exist in the core.

Going forward those duplicated translations should be replaced by calls
to c_(), which will do the same as _() but won't be extracted for
translation.
lib/noosfero/i18n.rb
... ... @@ -4,6 +4,8 @@ class Object
4 4 include FastGettext::Translation
5 5 alias :gettext :_
6 6 alias :ngettext :n_
  7 + alias :c_ :_
  8 + alias :cN_ :N_
7 9 end
8 10  
9 11  
... ... @@ -13,6 +15,11 @@ if File.exists?(custom_locale_dir)
13 15 repos << FastGettext::TranslationRepository.build('environment', :type => 'po', :path => custom_locale_dir)
14 16 end
15 17  
  18 +Dir.glob('{baseplugins,config/plugins}/*/locale') do |plugin_locale_dir|
  19 + plugin = File.basename(File.dirname(plugin_locale_dir))
  20 + repos << FastGettext::TranslationRepository.build(plugin, :type => 'mo', :path => plugin_locale_dir)
  21 +end
  22 +
16 23 # translations in place?
17 24 locale_dir = Rails.root.join('locale')
18 25 if File.exists?(locale_dir)
... ...
lib/tasks/gettext.rake
... ... @@ -16,6 +16,14 @@ file makemo_stamp =&gt; Dir.glob(&#39;po/*/noosfero.po&#39;) do
16 16 mo_root: 'locale',
17 17 )
18 18  
  19 + Dir.glob('plugins/*').each do |plugindir|
  20 + GetText.create_mofiles(
  21 + verbose: true,
  22 + po_root: File.join(plugindir, 'po'),
  23 + mo_root: File.join(plugindir, 'locale'),
  24 + )
  25 + end
  26 +
19 27 FileUtils.mkdir_p 'tmp'
20 28 FileUtils.touch makemo_stamp
21 29 end
... ... @@ -54,7 +62,6 @@ task :updatepo do
54 62 'config/initializers/*.rb',
55 63 'public/*.html.erb',
56 64 'public/designs/themes/{base,noosfero,profile-base}/*.{rhtml,html.erb}',
57   - 'plugins/**/{controllers,models,lib,views}/**/*.{rhtml,html.erb,rb}',
58 65 ].map { |pattern| Dir.glob(pattern) }.flatten
59 66  
60 67 require 'gettext'
... ... @@ -67,7 +74,31 @@ task :updatepo do
67 74 po_root: 'po',
68 75 }
69 76 )
  77 +end
70 78  
  79 +Dir.glob('plugins/*').each do |plugindir|
  80 + plugin = File.basename(plugindir)
  81 + task :updatepo => "updatepo:plugin:#{plugin}"
  82 + task "updatepo:plugin:#{plugin}" do
  83 + files = Dir.glob("#{plugindir}/**/*.{rb,html.erb}")
  84 + po_root = File.join(plugindir, 'po')
  85 + require 'gettext'
  86 + require 'gettext/tools'
  87 + GetText.update_pofiles(
  88 + plugin,
  89 + files,
  90 + Noosfero::VERSION,
  91 + {
  92 + po_root: po_root,
  93 + }
  94 + )
  95 + plugin_pot = File.join(po_root, "#{plugin}.pot")
  96 + if File.exists?(plugin_pot) && system("LANG=C msgfmt --statistics --output /dev/null #{plugin_pot} 2>&1 | grep -q '^0 translated messages.'")
  97 + rm_f plugin_pot
  98 + end
  99 + sh 'find', po_root, '-type', 'd', '-empty', '-delete'
  100 + puts
  101 + end
71 102 end
72 103  
73 104 task :checkpo do
... ...
script/move-translations-to-plugins.rb 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +languages = Dir.glob('po/*').reject { |f| f =~ /pot$/ }.map { |f| File.basename(f) }
  2 +
  3 +core_files = `grep '#:' po/noosfero.pot | cut -d ':' -f 2 | sed 's/^\s*//' | grep -v '^plugins' | sort -u`.split.map { |f| [ '-N', f] }.flatten
  4 +
  5 +languages.each do |lang|
  6 +
  7 + lang_plugins_po = "tmp/#{lang}_plugins.po"
  8 + system('msggrep', '-v', *core_files, '--output-file', lang_plugins_po, "po/#{lang}/noosfero.po")
  9 +
  10 + Dir.glob('plugins/*').each do |plugindir|
  11 + plugin = File.basename(plugindir)
  12 + po = File.join(plugindir, 'po', lang, plugin + '.po')
  13 +
  14 + files = []
  15 + Dir.glob("#{plugindir}/**/*.{rb,html.erb}").each do |f|
  16 + files << '-N' << f
  17 + end
  18 +
  19 + system('mkdir', '-p', File.dirname(po))
  20 + system('msggrep', *files, '--output-file', po, lang_plugins_po)
  21 +
  22 + if system("msgfmt --statistics -o /dev/null #{po} 2>&1 | grep -q '^0 translated message'")
  23 + # empty .po
  24 + system('rm', '-f', po)
  25 + puts "[#{lang}] #{plugin}: PO file empty, deleted"
  26 + else
  27 + puts "[#{lang}] #{plugin}"
  28 + end
  29 +
  30 + end
  31 +
  32 + system('rm', '-f', lang_plugins_po)
  33 + system('find plugins/*/po -type d -empty -delete')
  34 +end
... ...