Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/i18n_converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'yaml'
require 'active_support/core_ext/hash/conversions'

class I18nConverter
def initialize(stdin:, stdout:)
@stdin = stdin
@stdout = stdout
end

def xml_to_yml
return if bad_usage?(in_format: :xml, out_format: :yml)

data = Hash.from_xml(stdin.read)
data_hash = data['hash']
data = data_hash if data_hash
stdout.puts YAML.dump(data)
end

def yml_to_xml
return if bad_usage?(in_format: :yml, out_format: :xml)

data = YAML.safe_load(stdin.read)
stdout.puts data.to_xml
end

private

attr_reader :stdin, :stdout

def bad_usage?(in_format:, out_format:)
return false unless stdin.tty?

stdout.puts "Usage: cat en.#{in_format} | #{$PROGRAM_NAME} > output.#{out_format}"
# rubocop:disable Rails/Exit
exit 1
# rubocop:enable Rails/Exit
true
end
end
5 changes: 5 additions & 0 deletions scripts/i18n-xml-to-yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
require 'i18n_converter'

I18nConverter.new(stdin: STDIN, stdout: STDOUT).xml_to_yml
5 changes: 5 additions & 0 deletions scripts/i18n-yml-to-xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
require 'i18n_converter'

I18nConverter.new(stdin: STDIN, stdout: STDOUT).yml_to_xml
81 changes: 81 additions & 0 deletions spec/lib/i18n_converter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'spec_helper'
require 'i18n_converter'

RSpec.describe I18nConverter do
let(:translation_yml) do
<<~YAML
---
en:
test:
key: Some string
other_test:
other_key: Other key
some_key: Some key
YAML
end

let(:translation_xml) do
<<~XML
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<en>
<test>
<key>Some string</key>
</test>
<other-test>
<other-key>Other key</other-key>
<some-key>Some key</some-key>
</other-test>
</en>
</hash>
XML
end

subject(:converter) { I18nConverter.new(stdin: stdin, stdout: stdout) }

describe '.yml_to_xml' do
let(:stdout) { StringIO.new }
let(:stdin) { StringIO.new(translation_yml) }

context 'with a TTY on STDIN' do
let(:stdin) { instance_double('IO', tty?: true) }

it 'prints an error and exits' do
expect(converter).to receive(:exit).with(1)

converter.yml_to_xml

expect(stdout.string.chomp).to eq("Usage: cat en.yml | #{$PROGRAM_NAME} > output.xml")
end
end

it 'outputs XML' do
converter.yml_to_xml

expect(stdout.string).to eq(translation_xml)
end
end

describe '.xml_to_yml' do
let(:stdout) { StringIO.new }
let(:stdin) { StringIO.new(translation_xml) }

context 'with a TTY on STDIN' do
let(:stdin) { instance_double('IO', tty?: true) }

it 'prints an error and exits' do
expect(converter).to receive(:exit).with(1)

converter.xml_to_yml

expect(stdout.string.chomp).to eq("Usage: cat en.xml | #{$PROGRAM_NAME} > output.yml")
end
end

it 'outputs YML' do
converter.xml_to_yml

expect(stdout.string).to eq(translation_yml)
end
end
end