Sha256: 687ef9125b9cd8fdc8cdde0f091b452dda5abe90c5933b63317b42c6e7cae2e6

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require 'thor'

module BabelI18n
  class Cli < Thor

    desc 'translate_text', 'translate text --from some_languague --to other_language --key xpto'
    option :from
    option :to, required: true
    option :key
    def translate_text(text)
      begin
        key = options[:key] || ENV['GOOGLE_API_KEY']
        raise KeyRequiredError if key.nil?
        base = BabelI18n::Base.new(text, key)
        base.from(options[:from]) if options[:from]
        base.to(options[:to])
        puts base.translate
      rescue ArgumentError => e
        puts e.message
      end
    end

    desc 'translate_file', 'translate file --to other_language --key xpto'
    option :to, required: true
    option :key
    def translate_file(file)
      begin
        key = options[:key] || ENV['GOOGLE_API_KEY']
        raise KeyRequiredError if key.nil?
        raise FileParserError if !File.exist?(file)
        adapter = BabelI18n::Adapter.new(file, options[:to], key)
        adapter.write_file
        puts "File '#{adapter.from}.yml' was translated to '#{adapter.file_name}'"
      rescue ArgumentError => e
        puts e.message
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
babel_i18n-0.0.5 lib/babel_i18n/cli.rb
babel_i18n-0.0.4 lib/babel_i18n/cli.rb