Class: I18nTemplate::Extractor::Gettext

Inherits:
Base
  • Object
show all
Defined in:
lib/i18n_template/extractor/gettext.rb

Overview

Extract phrases to gettext format. E.g. pot/po files

Extractor creates and updates:

* {po_root}/{textdomain}.pot
* {pr_root}/{lang}/{textdomain}.po

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Gettext

Returns a new instance of Gettext.


26
27
28
29
30
31
32
# File 'lib/i18n_template/extractor/gettext.rb', line 26

def initialize(options)
  super(options)

  @version = 'version 0.0.1'
  @pot_file = File.join(@options[:po_root], "#{@options[:textdomain]}.pot")
  @pot_file_tmp = "#{@pot_file}.tmp"
end

Class Method Details

.default_optionsObject


18
19
20
21
22
23
# File 'lib/i18n_template/extractor/gettext.rb', line 18

def default_options
  super.merge({
    :po_root    => 'po',
    :textdomain => 'phrases'
  })
end

.formatObject


14
15
16
# File 'lib/i18n_template/extractor/gettext.rb', line 14

def format
  'gettext'
end

Instance Method Details

#call(paths) ⇒ Object


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
# File 'lib/i18n_template/extractor/gettext.rb', line 34

def call(paths)
  # ensure root directory exists
  FileUtils.mkdir_p(@options[:po_root])

  # generate new temporary pot file
  File.open(@pot_file_tmp, "w") do |f|
    f.puts generate_pot_header
    f.puts ""
    f.puts generate_pot_body(paths)
  end

  # merge pot file
  log("Merging #{@pot_file}")
  if File.exist?(@pot_file)
    merge_po_files(@pot_file, @pot_file_tmp)
  else
    FileUtils.cp(@pot_file_tmp, @pot_file)
  end

  # merge po files
  Dir.glob("#{@options[:po_root]}/*/#{@options[:textdomain]}.po") do |po_file|
    log("Merging #{po_file}")
    concate_po_files(po_file, @pot_file_tmp)
  end

  # remove temporary pot file
  FileUtils.rm(@pot_file_tmp)
end