perrotuerto.blog/config/build/pottopos

66 lines
1.6 KiB
Plaintext
Raw Normal View History

2019-01-28 21:39:10 -06:00
#!/usr/bin/env ruby
# encoding: UTF-8
# coding: UTF-8
require 'simple_po_parser'
require 'fileutils'
require 'time'
require 'json'
# Displays message when something goes wrong
def check condition, err_msg
if condition then puts err_msg + ' For help use -h or --help.'; abort end
end
# To the root directory
Dir.chdir(File.dirname(__FILE__) + '/../..')
# Variables
pot_dir = 'content/pot/'
pot_path = ''
po_dir = 'content/po/'
po_name = ''
locales = ['en', 'es']
# Displays help
if ARGV[0] =~ /-h/
puts "pottopos generates po files from a pot file."
puts "\nUse:"
puts " pottopos [pot file] [option]"
puts "\nOption:"
puts " -m Merges to an existing po file."
puts "\nExamples:"
puts " pottopos file.pot"
puts " pottopos file.pot -m"
puts "\nCurrent supported locales:"
puts " " + locales.join("\n ")
puts "\nThe pot file has to be in '#{pot_dir}' and the po files are going to be stored in '#{po_dir}/*', where '*' is the locales folders."
abort
end
# Checks if the user gave the path to the file
check(ARGV[0] == nil, "ERROR: File is required.")
# Cleans path
pot_path = pot_dir + File.basename(ARGV[0])
po_name = File.basename(ARGV[0], '.*') + '.po'
# Check if the file exists
check(File.exist?(pot_path) == false, "ERROR: File doesn't exist.")
# Creates locale directory if need it
locales.each do |l|
if File.exist?(po_dir + l) == false
Dir.mkdir(po_dir + l)
end
end
# Creates or merges po files
locales.each do |l|
if ARGV[1] == nil
system("msginit --input=#{pot_path} --locale=#{l} --no-translator --output=#{po_dir + l}/#{po_name}")
else
system("msgmerge --update #{po_dir + l}/#{po_name} #{pot_path}")
end
end