mo_translation 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/mo_translation +25 -0
  2. data/lib/mo_translation.rb +80 -0
  3. metadata +63 -0
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/mo_translation.rb'
3
+
4
+ def print_usage
5
+ $stderr.puts <<-EOF
6
+ usage: mo_translation <subcommand>
7
+
8
+ Available subcommands are:
9
+ diff file_1 file_2 - Differences in I18n keys between two files
10
+ EOF
11
+ exit 1
12
+ end
13
+
14
+ print_usage unless ARGV[0] == 'diff' and ARGV.size == 3
15
+
16
+ file_a = ARGV[1]
17
+ file_b = ARGV[2]
18
+ diff = FileDiff.new(file_a, file_b).as_hash
19
+ if diff.empty?
20
+ puts "Files are identical"
21
+ exit 0
22
+ else
23
+ puts "Missing keys:#{missing_keys(diff)}"
24
+ exit 1
25
+ end
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env ruby
2
+ require 'yaml'
3
+
4
+ class TranslationFile
5
+ attr_reader :path
6
+ def initialize(path)
7
+ @path = path
8
+ @hash = YAML.load_file(path)
9
+ raise %{Expecting exactly one toplevel key (e.g. "en:" or "pl:")} unless @hash.keys.size == 1
10
+ end
11
+
12
+ def without_top
13
+ @hash[@hash.keys.first]
14
+ end
15
+
16
+ def toplevel_key
17
+ @hash.keys.first
18
+ end
19
+
20
+ def flat_translation_keys
21
+ flatten_out_keys(without_top)
22
+ end
23
+
24
+ def full_key(key)
25
+ [toplevel_key, key].join(".")
26
+ end
27
+
28
+ private
29
+ def flatten_out_keys(hash)
30
+ flatten_helper(hash,[]).flatten
31
+ end
32
+
33
+ def flatten_helper(hash, prefix)
34
+ if hash.is_a? Hash
35
+ hash.keys.map do |key|
36
+ flatten_helper(hash[key], prefix + [key.to_s])
37
+ end
38
+ else
39
+ prefix.join(".")
40
+ end
41
+ end
42
+ end
43
+
44
+ class FileDiff
45
+ def initialize(file_a, file_b)
46
+ @file_a = TranslationFile.new(file_a)
47
+ @file_b = TranslationFile.new(file_b)
48
+ end
49
+ def as_hash
50
+ result = {}
51
+ result[@file_a] = keys_in(@file_a).that_are_not_in(@file_b)
52
+ result[@file_b] = keys_in(@file_b).that_are_not_in(@file_a)
53
+ result.reject{|file,keys| keys.empty?}
54
+ end
55
+ def keys_in(file)
56
+ Diff.new(file)
57
+ end
58
+
59
+ private
60
+ class Diff
61
+ def initialize(file)
62
+ @file = file
63
+ end
64
+ def that_are_not_in(other_file)
65
+ diff(other_file, @file)
66
+ end
67
+ def diff(file_a, file_b)
68
+ file_a.flat_translation_keys - file_b.flat_translation_keys
69
+ end
70
+ end
71
+ end
72
+
73
+ def missing_keys(diff)
74
+ output = []
75
+ diff.each_pair do |file, keys|
76
+ output << "\nIn file #{file.path}:\n"
77
+ output << keys.map{|key| " "+file.full_key(key) }.join("\n")
78
+ end
79
+ output.join
80
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mo_translation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sebastian Geiger
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A very basic gem that helps with rails I18n files
31
+ email: sebastian.geiger@mo-stud.io
32
+ executables:
33
+ - mo_translation
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/mo_translation.rb
38
+ - bin/mo_translation
39
+ homepage:
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.25
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: A very basic gem that helps with rails I18n files
63
+ test_files: []