#!/usr/bin/env ruby require 'optparse' require 'wca_i18n' require 'colorize' def main verbose = false OptionParser.new do |opts| opts.banner = "Usage: #{__FILE__} [options] " opts.on("-v", "--verbose", "Run verbosely") do |v| verbose = v end end.parse! base_translation_file = ARGV.shift throw "Please pass a base_translation_file" unless base_translation_file throw "base_translation_file not found: #{base_translation_file}" unless File.exist?(base_translation_file) translation_files = ARGV throw "Please pass a list of translation_files" unless translation_files base_translation = load_translation(base_translation_file) translations = translation_files.map { |f| load_translation(f) }.reject { |t| t.locale == base_translation.locale } perfect = compare_base_with_translations(base_translation, translations.sort_by!(&:locale), verbose) exit (perfect ? 0 : 1) end def load_translation(filename) locale = File.basename(filename, ".*") WcaI18n::Translation.new(locale, File.read(filename)) end def compare_base_with_translations(base_translation, translations, verbose) total_issue_count = 0 translations.each do |translation| diff = translation.compare_to(base_translation) translation_issue_count = diff.values.map(&:length).sum total_issue_count += translation_issue_count prefix = (translation_issue_count == 0 ? "✔".green : "✗".red) puts "#{prefix} #{translation.locale}:#{indent(format_diff(diff, verbose), 1)}" end total_issue_count == 0 end def format_diff(diff, verbose=false) types = [:missing, :outdated, :unused] total = diff.values.map(&:length).sum if total == 0 "Up to date" elsif verbose pretty_counts = types.map do |type| contexts = diff[type] pretty = "• #{contexts.length} #{type.to_s}" if contexts.length > 0 pretty += ":\n#{indent(format_contexts(contexts), 1)}" end pretty end "#{total} total\n" + pretty_counts.join("\n") else pretty_counts = types.map { |type| "#{diff[type].length} #{type.to_s}" } "#{total} total = #{pretty_counts.join(" + ")}" end end def format_contexts(contexts) contexts.map { |context| context.join(" > ") }.join("\n") end def indent(str, level) str.split("\n").map { |line| "\t"*level + line }.join("\n") end main