Class: HammerCLI::I18n::FindTask

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/hammer_cli/i18n/find_task.rb

Constant Summary collapse

MIN_TRANSLATION_PERC =
50

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain, version) ⇒ FindTask

Returns a new instance of FindTask.



11
12
13
14
# File 'lib/hammer_cli/i18n/find_task.rb', line 11

def initialize(domain, version)
  @domain = domain
  @version = version
end

Class Method Details

.define(domain, version) ⇒ Object



124
125
126
# File 'lib/hammer_cli/i18n/find_task.rb', line 124

def self.define(domain, version)
  new(domain, version).define
end

Instance Method Details

#defineObject



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
# File 'lib/hammer_cli/i18n/find_task.rb', line 16

def define
  namespace :gettext do
    task :setup do
      require 'gettext/tools/task'

      GetText::Tools::Task.define do |task|
        task.package_name = @domain.domain_name
        task.package_version = @version.to_s
        task.domain = @domain.domain_name
        task.mo_base_directory = @domain.locale_dir
        task.po_base_directory = @domain.locale_dir
        task.files = @domain.translated_files
        task.msgmerge_options='--no-fuzzy-matching'
      end
    end

    desc "Update pot file"
    task :find => [:setup] do
      Rake::Task["gettext:po:update"].invoke
    end

    desc 'Check languages with 50% or more coverage and create needed files'
    task :find_new do
      client = HammerCLI::TaskHelper::I18n::TxApiClient.new(domain: @domain)
      stats = client.language_stats_collection
      lang_percentages = stats['data'].each_with_object({}) do |lang, res|
        res[lang['id'].split(':').last] = (lang['attributes']['translated_strings'] * 100.0 / lang['attributes']['total_strings']).round
      end
      lang_percentages.select { |_, v| v >= MIN_TRANSLATION_PERC }.each_key do |lang|
        lang_dir = File.join(@domain.locale_dir, lang)
        FileUtils.mkpath(lang_dir)
        FileUtils.cp(File.join(@domain.locale_dir, "#{@domain.domain_name}.pot"), File.join(lang_dir, "#{@domain.domain_name}.po"))
      end
    end
  end

  namespace :tx do
    desc 'Pull translations from transifex'
    task :pull do
      raise 'Command tx not found. Make sure you have transifex-client installed and configured.' unless system("command -v tx >/dev/null 2>&1")

      sh "tx pull -f"
      edit_files = Dir.glob(File.join(@domain.locale_dir, '**', '*.edit.po'))
      edit_files.each do |edit_file|
        `sed -i 's/^\\("Project-Id-Version: \\).*$/\\1#{@domain.domain_name} #{@version}\\\\n"/' #{edit_file};`
      end
    end

    desc 'Merge .edit.po into .po'
    task :update_po do
      edit_files = Dir.glob(File.join(@domain.locale_dir, '**', '*.edit.po'))
      edit_files.each do |edit_file|
        po_file = edit_file.gsub('.edit.po', '.po')
        sh "msgcat --use-first --no-location #{edit_file} #{po_file} --output-file #{po_file}"
      end
    end

    desc 'Generate MO files from PO files'
    task :all_mo do
      po_files = Dir.glob(File.join(@domain.locale_dir, '**', "#{@domain.domain_name}.po"))
      po_files.each do |po_file|
        dir = File.dirname(po_file) + '/LC_MESSAGES'
        FileUtils.mkdir_p(dir)
        sh "msgfmt -o #{dir}/#{@domain.domain_name}.mo #{po_file}"
      end
    end

    desc 'Download and merge translations from Transifex'
    task :update do
      Rake::Task['gettext:find_new'].invoke
      Rake::Task['gettext:find'].invoke
      Rake::Task['tx:pull'].invoke
      Rake::Task['tx:update_po'].invoke
      Rake::Task['tx:all_mo'].invoke
      locale_dir = File.expand_path(@domain.locale_dir, __dir__)
      sh "git add #{locale_dir}"
      sh 'git commit -m "i18n - extracting new, pulling from tx"'
      puts 'Changes commited!'
    end

    desc 'Check for malformed strings'
    task :check do
      raise 'Command pofilter not found. Make sure you have translate-toolkit installed.' unless system("command -v pofilter >/dev/null 2>&1")

      po_files = Dir.glob(File.join(@domain.locale_dir, '**',"#{@domain.domain_name}.po"))
      po_files.each do |po_file|
        pox_file = po_file.gsub('.po', '.pox')
        sh "msgfmt -c #{po_file}"
        sh "pofilter --nofuzzy -t variables -t blank -t urls -t emails -t long -t newlines -t endwhitespace -t endpunc \
          -t puncspacing -t options -t printf -t validchars --gnome #{po_file} > #{pox_file};"
        sh("! grep -q msgid #{pox_file}") do |ok, _|
          sh "cat #{pox_file}" unless ok
          abort "See errors above for #{pox_file}."
        end
      end
    end

    desc 'Clean everything, removes *.edit.po, *.po.timestamp and *.pox files'
    task :clean do
      edit_files = Dir.glob(File.join(@domain.locale_dir, '**', '*.edit.po'))
      timestamp_files = Dir.glob(File.join(@domain.locale_dir, '**', '*.po.time_stamp'))
      pox_files = Dir.glob(File.join(@domain.locale_dir, '**', '*.pox'))
      messages_file = Dir.glob(File.join(@domain.locale_dir, '..', 'messages.mo'))
      FileUtils.rm_f(edit_files + timestamp_files + pox_files + messages_file)
    end
  end
end