gettext.rake
3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#
# Added for Ruby-GetText-Package
#
require 'pathname'
makemo_stamp = 'tmp/makemo.stamp'
desc "Create mo-files for L10n"
task :makemo => makemo_stamp
file makemo_stamp => Dir.glob('po/*/noosfero.po') do
  Rake::Task['symlinkmo'].invoke
  require 'gettext'
  require 'gettext/tools'
  GetText.create_mofiles(
    verbose: true,
    po_root: 'po',
    mo_root: 'locale',
  )
  Dir.glob('plugins/*').each do |plugindir|
    GetText.create_mofiles(
      verbose: true,
      po_root: File.join(plugindir, 'po'),
      mo_root: File.join(plugindir, 'locale'),
    )
  end
  FileUtils.mkdir_p 'tmp'
  FileUtils.touch makemo_stamp
end
task :cleanmo do
  rm_f makemo_stamp
end
task :clean => 'cleanmo'
task :symlinkmo do
  langmap = {
    'pt' => 'pt_BR',
  }
  root = Pathname.new(File.dirname(__FILE__) + '/../..').expand_path
  mkdir_p(root.join('locale'))
  Dir.glob(root.join('po/*/')).each do |dir|
    lang = File.basename(dir)
    orig_lang = langmap[lang] || lang
    mkdir_p(root.join('locale', "#{lang}", 'LC_MESSAGES'))
    ['iso_3166'].each do |domain|
      origin = "/usr/share/locale/#{orig_lang}/LC_MESSAGES/#{domain}.mo"
      target = root.join('locale', "#{lang}", 'LC_MESSAGES', "#{domain}.mo")
      if !File.symlink?(target)
        ln_s origin, target
      end
    end
  end
end
desc "Update pot/po files to match new version."
task :updatepo do
  puts 'Extracting strings from source. This may take a while ...'
  # XXX this list is duplicated in test/unit/i18n_test.rb; if you change it
  # here, please also update it there.
  files_to_translate = [
    "{app,lib}/**/*.{rb,rhtml,erb}",
    'config/initializers/*.rb',
    'public/*.html.erb',
    'public/designs/themes/{base,noosfero,profile-base}/*.{rhtml,html.erb}',
  ].map { |pattern| Dir.glob(pattern) }.flatten
  require 'gettext'
  require 'gettext/tools'
  GetText.update_pofiles(
    'noosfero',
    files_to_translate,
    Noosfero::VERSION,
    {
      po_root: 'po',
    }
  )
end
Dir.glob('plugins/*').each do |plugindir|
  plugin = File.basename(plugindir)
  task :updatepo => "updatepo:plugin:#{plugin}"
  desc "Extract strings from #{plugin} plugin"
  task "updatepo:plugin:#{plugin}" do
    files = Dir.glob("#{plugindir}/**/*.{rb,html.erb}")
    po_root = File.join(plugindir, 'po')
    require 'gettext'
    require 'gettext/tools'
    GetText.update_pofiles(
      plugin,
      files,
      Noosfero::VERSION,
      {
        po_root: po_root,
      }
    )
    plugin_pot = File.join(po_root, "#{plugin}.pot")
    if File.exists?(plugin_pot) && system("LANG=C msgfmt --statistics --output /dev/null #{plugin_pot} 2>&1 | grep -q '^0 translated messages.$'")
      rm_f plugin_pot
    end
    sh 'find', po_root, '-type', 'd', '-empty', '-delete'
    puts
  end
end
def checkpo(po_files)
  max = po_files.map(&:size).max
  po_files.each do |po|
    printf "%#{max}s: ", po
    system "msgfmt --statistics --output /dev/null " + po
  end
end
desc "checks core translation files"
task :checkpo do
  checkpo(Dir.glob('po/*/noosfero.po'))
end
languages = Dir.glob('po/*').select { |d| File.directory?(d) }.map { |d| File.basename(d) }
languages.each do |lang|
  desc "checks #{lang} translation files"
  task "checkpo:#{lang}" do
    checkpo(Dir.glob("po/#{lang}/*.po") + Dir.glob("plugins/*/po/#{lang}/*.po"))
  end
end
# vim: ft=ruby