Module: GettextSetup::Pot
- Defined in:
- lib/gettext-setup/pot.rb
Class Method Summary collapse
- .files_to_translate ⇒ Object
- .generate_new_po(language, locales_path = GettextSetup.locales_path, pot_file = nil, po_file = nil) ⇒ Object
- .generate_new_pot(opts = {}) ⇒ Object
- .merge(opts = {}) ⇒ Object
- .po_file_path(language) ⇒ Object
- .pot_file_path ⇒ Object
- .string_changes?(old_pot, new_pot) ⇒ Boolean
- .text_domain ⇒ Object
- .update_pot(locales_path = GettextSetup.locales_path, path = nil) ⇒ Object
Class Method Details
.files_to_translate ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/gettext-setup/pot.rb', line 11 def self.files_to_translate files = (GettextSetup.config['source_files'] || []).map do |p| Dir.glob(p) end.flatten # check for optional list of files to exclude from string # extraction exclusions = (GettextSetup.config['exclude_files'] || []).map do |p| Dir.glob(p) end.flatten # if file is a directory, take it out of the array. directories # cause rxgettext to error out. (files - exclusions).reject { |file| File.directory?(file) } end |
.generate_new_po(language, locales_path = GettextSetup.locales_path, pot_file = nil, po_file = nil) ⇒ Object
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 |
# File 'lib/gettext-setup/pot.rb', line 85 def self.generate_new_po(language, locales_path = GettextSetup.locales_path, pot_file = nil, po_file = nil) GettextSetup.initialize_config(locales_path) language ||= ENV['LANGUAGE'] pot_file ||= GettextSetup::Pot.pot_file_path po_file ||= GettextSetup::Pot.po_file_path(language) # Let's do some pre-verification of the environment. if language.nil? puts "You need to specify the language to add. Either 'LANGUAGE=eo rake gettext:po' or 'rake gettext:po[LANGUAGE]'" return end language_path = File.dirname(po_file) FileUtils.mkdir_p(language_path) if File.exist?(po_file) cmd = "msgmerge -U #{po_file} #{pot_file}" _, _, _, wait = Open3.popen3(cmd) exitstatus = wait.value if exitstatus.success? puts "PO file #{po_file} merged" true else puts 'PO file merge failed' false end else cmd = "msginit --no-translator -l #{language} -o #{po_file} -i #{pot_file}" _, _, _, wait = Open3.popen3(cmd) exitstatus = wait.value if exitstatus.success? puts "PO file #{po_file} created" true else puts 'PO file creation failed' false end end end |
.generate_new_pot(opts = {}) ⇒ Object
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 |
# File 'lib/gettext-setup/pot.rb', line 58 def self.generate_new_pot(opts = {}) locales_path = opts[:locales_path] || GettextSetup.locales_path GettextSetup.initialize_config(locales_path) target_path = opts[:target_path] || pot_file_path input_files = if opts[:header_only] tmpfile = Tempfile.new('gettext-setup.tmp') tmpfile.path else files_to_translate.join(' ') end config = GettextSetup.config package_name = config['package_name'] bugs_address = config['bugs_address'] copyright_holder = config['copyright_holder'] # Done this way to allow the user to enter an empty string in the config. comments_tag = config.key?('comments_tag') ? config['comments_tag'] : 'TRANSLATORS' version = `git describe` system("rxgettext -o #{target_path} --no-wrap --sort-by-file " \ "--add-comments#{comments_tag.to_s == '' ? '' : '=' + comments_tag} --msgid-bugs-address '#{bugs_address}' " \ "--package-name '#{package_name}' " \ "--package-version '#{version}' " \ "--copyright-holder='#{copyright_holder}' --copyright-year=#{Time.now.year} " \ "#{input_files}") tmpfile.unlink if tmpfile $CHILD_STATUS.success? end |
.merge(opts = {}) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/gettext-setup/pot.rb', line 156 def self.merge(opts = {}) locales_path = opts[:locales_path] || GettextSetup.locales_path GettextSetup.initialize_config(locales_path) target_filename = GettextSetup.config['project_name'] + '.pot' target_path = File.(target_filename, locales_path) oldpot_dir = File.('oldpot', locales_path) oldpot_path = File.("oldpot/old_#{target_filename}", locales_path) if File.exist? target_path FileUtils.mkdir_p(oldpot_dir) begin FileUtils.cp(target_path, oldpot_path) rescue Errno::ENOENT => e raise "There was a problem creating .pot backup #{oldpot_path}, merge failed: #{e.}" end puts "Warning - #{target_filename} already exists and will be relocated to oldpot/old_#{target_filename}." end locales_glob = Dir.glob("#{locales_path}/*.pot") cmd = "msgcat #{locales_glob.join(' ')} -o #{target_path}" _, _, _, wait = Open3.popen3(cmd) exitstatus = wait.value raise 'PO files failed to merge' unless exitstatus.success? puts "PO files have been successfully merged, #{target_filename} has been created." exitstatus end |
.po_file_path(language) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/gettext-setup/pot.rb', line 32 def self.po_file_path(language) return if GettextSetup.locales_path.nil? return if GettextSetup.config['project_name'].nil? return if language.nil? File.join(GettextSetup.locales_path, language, GettextSetup.config['project_name'] + '.po') end |
.pot_file_path ⇒ Object
26 27 28 29 30 |
# File 'lib/gettext-setup/pot.rb', line 26 def self.pot_file_path return if GettextSetup.locales_path.nil? return if GettextSetup.config['project_name'].nil? File.join(GettextSetup.locales_path, GettextSetup.config['project_name'] + '.pot') end |
.string_changes?(old_pot, new_pot) ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gettext-setup/pot.rb', line 39 def self.string_changes?(old_pot, new_pot) # Warnings will be in another language if locale is not set to en_US _, stderr, status = Open3.capture3("LANG=en_US msgcmp --use-untranslated '#{old_pot}' '#{new_pot}'") if status.exitstatus == 1 || /this message is not used/.match(stderr) || /this message is used but not defined/.match(stderr) return true end return false rescue IOError # probably means msgcmp is not present on the system # so return true to be on the safe side return true end |
.text_domain ⇒ Object
7 8 9 |
# File 'lib/gettext-setup/pot.rb', line 7 def self.text_domain FastGettext.text_domain end |
.update_pot(locales_path = GettextSetup.locales_path, path = nil) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/gettext-setup/pot.rb', line 126 def self.update_pot(locales_path = GettextSetup.locales_path, path = nil) GettextSetup.initialize_config(locales_path) path ||= pot_file_path if !File.exist? path puts 'No existing POT file, generating new' result = GettextSetup::Pot.generate_new_pot(locales_path: locales_path, target_path: path) puts "POT file #{path} has been generated" if result result else old_pot = path + '.old' File.rename(path, old_pot) result = GettextSetup::Pot.generate_new_pot(locales_path: locales_path, target_path: path) if !result puts 'POT creation failed' result elsif GettextSetup::Pot.string_changes?(old_pot, path) puts 'String changes detected, replacing with updated POT file' File.delete(old_pot) true else puts 'No string changes detected, keeping old POT file' File.rename(old_pot, path) true end end end |