wca_i18n 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 52de91ef7d972252dd256379fe3d091743f72d4e
4
+ data.tar.gz: b814ae9af19eba3e04e1a48feb02954637a2fc61
5
+ SHA512:
6
+ metadata.gz: 8d15b262f498624b3a92a272dee1723e84b69081ef087d5c1d0550f11e58a00dee380103e1d8d22b3882928d28b685c933bcf59e50b375873e56e4bfb7488900
7
+ data.tar.gz: 72ef348d65fd55382f5f7ad7cd42162fc7f03260a90b52e257d48e7aa9964f91d79c9c9ec505e5c402f5a30a9874b8ee99a9f2355676d8f33c0dae0a3a90bb32
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ end_of_line = lf
5
+ charset = utf-8
6
+ trim_trailing_whitespace = true
7
+ insert_final_newline = true
8
+ indent_style = space
9
+ indent_size = 2
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.3
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in wca_i18n.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ wca_i18n (0.2.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ byebug (9.1.0)
10
+ diff-lcs (1.3)
11
+ rake (10.5.0)
12
+ rspec (3.7.0)
13
+ rspec-core (~> 3.7.0)
14
+ rspec-expectations (~> 3.7.0)
15
+ rspec-mocks (~> 3.7.0)
16
+ rspec-core (3.7.1)
17
+ rspec-support (~> 3.7.0)
18
+ rspec-expectations (3.7.0)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.7.0)
21
+ rspec-mocks (3.7.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.7.0)
24
+ rspec-support (3.7.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.16)
31
+ byebug (~> 9.0)
32
+ rake (~> 10.0)
33
+ rspec (~> 3.0)
34
+ wca_i18n!
35
+
36
+ BUNDLED WITH
37
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Jeremy Fleischman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # WcaI18n [![Build Status](https://travis-ci.org/thewca/wca-i18n.svg?branch=master)](https://travis-ci.org/thewca/wca-i18n)
2
+
3
+ Use this Gem to diff Rails translations.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'wca_i18n'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install wca_i18n
20
+
21
+ ## Usage
22
+
23
+ ### Using the wca_i18n executable
24
+
25
+ Use the included `wca_i18n` binary to compare translations to a base translation to see
26
+ how out of date they are.
27
+
28
+ ```bash
29
+ $ wca_i18n en.yml *.yml
30
+ ```
31
+
32
+ ### Using the wca_i18n library
33
+
34
+ There are two parts to this library: `WcaI18n::YAMLWithComments` (a YAML parser
35
+ that preserves comments) and `WcaI18n::Translation` (used to load and diff
36
+ translation YAML files). Until we have better documentation, it's best to look
37
+ at our [specs](https://github.com/thewca/wca-i18n/tree/master/spec) for how to
38
+ them.
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thewca/wca-i18n.
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wca_i18n"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/rspec ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
12
+ load(bundle_binstub) if File.file?(bundle_binstub)
13
+
14
+ require "pathname"
15
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
16
+ Pathname.new(__FILE__).realpath)
17
+
18
+ require "rubygems"
19
+ require "bundler/setup"
20
+
21
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/wca_i18n ADDED
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'wca_i18n'
5
+
6
+ def run!
7
+ verbose = false
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: #{__FILE__} [options] <base_translation_file> <translation_files>"
10
+ opts.on("-v", "--verbose", "Run verbosely") do |v|
11
+ verbose = v
12
+ end
13
+ end.parse!
14
+
15
+ base_translation_file = ARGV.shift
16
+ throw "Please pass a base_translation_file" unless base_translation_file
17
+ throw "base_translation_file not found: #{base_translation_file}" unless File.exist?(base_translation_file)
18
+
19
+ translation_files = ARGV
20
+ throw "Please pass a list of translation_files" unless translation_files
21
+
22
+ base_translation = load_translation(base_translation_file)
23
+ translations = translation_files.map { |f| load_translation(f) }.reject { |t| t.locale == base_translation.locale }
24
+
25
+ compare_base_with_translations(base_translation, translations.sort_by!(&:locale), verbose)
26
+ end
27
+
28
+ def load_translation(filename)
29
+ locale = File.basename(filename, ".*")
30
+ WcaI18n::Translation.new(locale, File.read(filename))
31
+ end
32
+
33
+ def compare_base_with_translations(base_translation, translations, verbose)
34
+ translations.each do |translation|
35
+ diff = translation.compare_to(base_translation)
36
+ puts "#{translation.locale}:#{indent(format_diff(diff, verbose), 1)}"
37
+ end
38
+ end
39
+
40
+ def format_diff(diff, verbose=false)
41
+ types = [:missing, :outdated, :unused]
42
+
43
+ pretty_counts = types.map do |type|
44
+ contexts = diff[type]
45
+ pretty = "#{contexts.length} #{type.to_s}"
46
+ pretty += ":\n" + indent(format_contexts(contexts), 1) if verbose
47
+ pretty
48
+ end
49
+
50
+ if verbose
51
+ "\n" + pretty_counts.join("\n")
52
+ else
53
+ total = diff.values.map(&:length).sum
54
+ "#{total} total = #{pretty_counts.join(" + ")}"
55
+ end
56
+ end
57
+
58
+ def format_contexts(contexts)
59
+ contexts.map { |context| context.join(" > ") }.join("\n")
60
+ end
61
+
62
+ def indent(str, level)
63
+ str.split("\n").map { |line| "\t"*level + line }.join("\n")
64
+ end
65
+
66
+ run! if __FILE__ == $0
@@ -0,0 +1,103 @@
1
+ require "json"
2
+ require "digest"
3
+ require "wca_i18n/yaml_with_comments"
4
+
5
+ module WcaI18n
6
+ TranslatedLeaf = Struct.new(:translated, :original_hash)
7
+
8
+ class Translation
9
+ PLURALIZATION_KEYS = %w(zero one two few many other).freeze
10
+
11
+ attr_accessor :locale, :data
12
+
13
+ def initialize(locale, file_content)
14
+ self.locale = locale.to_s
15
+ self.data = commented_yaml_to_translated_yaml(YAMLWithComments.parse(file_content))
16
+ end
17
+
18
+ def compare_to(base)
19
+ return diff_recursive(base.data[base.locale], self.data[self.locale], [])
20
+ end
21
+
22
+ def self.hash_translation(value)
23
+ # If the key is a pluralization, we use all the subkeys to compute the hash
24
+ # Please see this wiki page explaining why we do this: https://github.com/thewca/worldcubeassociation.org/wikigTranslating-the-website#translations-status-internals
25
+ to_digest = pluralization?(value) ? JSON.generate(value) : value
26
+ original_str = Digest::SHA1.hexdigest(to_digest)[0..6]
27
+ end
28
+
29
+ private def commented_yaml_to_translated_yaml(commented_value)
30
+ if leaf?(commented_value.value)
31
+ original_hash = extract_original_hash_from_comment(commented_value.comment)
32
+ return TranslatedLeaf.new(commented_value.strip_comments, original_hash)
33
+ end
34
+
35
+ commented_value.value.map do |key, value|
36
+ [key, commented_yaml_to_translated_yaml(value)]
37
+ end.to_h
38
+ end
39
+
40
+ private def diff_recursive(base, translation, context)
41
+ diff = { missing: [], unused: [], outdated: [] }
42
+ base_leaf = base.is_a?(TranslatedLeaf)
43
+ translation_leaf = translation.is_a?(TranslatedLeaf)
44
+
45
+ if !base
46
+ diff[:unused] += get_all_recursive(translation, context)
47
+ elsif !translation
48
+ diff[:missing] += get_all_recursive(base, context)
49
+ elsif base_leaf && translation_leaf
50
+ if translation.translated.nil?
51
+ diff[:missing] << context
52
+ elsif self.class.hash_translation(base.translated) != translation.original_hash
53
+ diff[:outdated] << context
54
+ end
55
+ elsif base_leaf && !translation_leaf
56
+ diff[:missing] << context
57
+ diff[:unused] += get_all_recursive(translation, context)
58
+ elsif !base_leaf && translation_leaf
59
+ diff[:missing] += get_all_recursive(base, context)
60
+ diff[:unused] << context
61
+ else
62
+ (base.keys | translation.keys).each do |key|
63
+ merge_diffs!(diff, diff_recursive(base[key], translation[key], [*context, key]))
64
+ end
65
+ end
66
+
67
+ diff
68
+ end
69
+
70
+ private def merge_diffs!(diff, other_diff)
71
+ other_diff.each do |key, value|
72
+ diff[key] += value
73
+ end
74
+ diff
75
+ end
76
+
77
+ private def get_all_recursive(node, context)
78
+ return [ context ] if node.is_a?(TranslatedLeaf)
79
+
80
+ node.map { |key, value| get_all_recursive(value, [*context, key]) }.flatten(1)
81
+ end
82
+
83
+ private def extract_original_hash_from_comment(comment)
84
+ hashes = comment.scan(/original_hash:\s*(.+)/).flatten(1)
85
+ if hashes.size == 0
86
+ nil
87
+ elsif hashes.size == 1
88
+ hashes.first
89
+ elsif hashes.size > 1
90
+ throw "Too many #{HASHTAG} occurrences in: #{comment}"
91
+ end
92
+ end
93
+
94
+ private def leaf?(node)
95
+ # If the node is a pluralization it's also a leaf!
96
+ node.nil? || node.is_a?(String) || self.class.pluralization?(node)
97
+ end
98
+
99
+ def self.pluralization?(node)
100
+ node.is_a?(Hash) && (node.keys & PLURALIZATION_KEYS).any?
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module WcaI18n
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,68 @@
1
+ require "yaml"
2
+
3
+ module WcaI18n
4
+ class YAMLWithComments < Struct.new(:comment, :value)
5
+ def self.parse(text)
6
+ _decorate_with_comments(YAML.safe_load(text), [], text)[1]
7
+ end
8
+
9
+ def strip_comments
10
+ stripped_value = self.value
11
+
12
+ if stripped_value.kind_of?(Hash)
13
+ stripped_value = stripped_value.map do |key, value|
14
+ [key, value.strip_comments]
15
+ end.to_h
16
+ end
17
+
18
+ stripped_value
19
+ end
20
+
21
+ def self._decorate_with_comments(node, context, text)
22
+ if node.kind_of?(Hash)
23
+ node = node.map do |key, value|
24
+ text, decorated = _decorate_with_comments(value, [*context, key], text)
25
+ [key, decorated]
26
+ end.to_h
27
+ end
28
+
29
+ trimmed_text, comment = _extract_comment(text, context)
30
+ [trimmed_text, YAMLWithComments.new(comment, node)]
31
+ end
32
+
33
+ def self._extract_comment(text, context)
34
+ return ["", ""] if context.empty?
35
+
36
+ # Match any character, including newline.
37
+ some_chars = /[\s\S]*?/
38
+
39
+ comment_lines_group = /((?:^\s*#.*\n)*)/
40
+
41
+ match_parent = ""
42
+ context[0...-1].each do |key|
43
+ match_parent = "#{match_parent}#{some_chars}#{build_key_matcher(key)}"
44
+ end
45
+ regexp = /(#{match_parent}#{some_chars})#{comment_lines_group}\s*#{build_key_matcher(context[-1])}/
46
+
47
+ comment = nil
48
+ text = text.sub(regexp) do
49
+ # Group 1 is everything before the comment and the key.
50
+ before = $1
51
+ # Group 2 contains the comments matched before the key.
52
+ comments = $2
53
+ comment = comments.split('#').map(&:strip).reject(&:empty?).join("\n")
54
+
55
+ # We return the beginning without the key, so that the current hash + key
56
+ # are removed from the text, but the parents and the value stay in.
57
+ before
58
+ end
59
+ throw "Could not find key: #{context} in given yaml text" unless comment
60
+
61
+ [text, comment]
62
+ end
63
+
64
+ def self.build_key_matcher(key)
65
+ /['\"]?#{key}['\"]?:/
66
+ end
67
+ end
68
+ end
data/lib/wca_i18n.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "wca_i18n/version"
2
+ require "wca_i18n/translation"
data/wca_i18n.gemspec ADDED
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "wca_i18n/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wca_i18n"
8
+ spec.version = WcaI18n::VERSION
9
+ spec.authors = ["Jeremy Fleischman"]
10
+ spec.email = ["software@worldcubeassociation.org"]
11
+
12
+ spec.summary = %q{Track how up to date Rails translations are.}
13
+ spec.homepage = "https://github.com/thewca/wca_i18n"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "byebug", "~> 9.0"
27
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wca_i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeremy Fleischman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '9.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '9.0'
69
+ description:
70
+ email:
71
+ - software@worldcubeassociation.org
72
+ executables:
73
+ - wca_i18n
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".editorconfig"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/rspec
88
+ - bin/setup
89
+ - exe/wca_i18n
90
+ - lib/wca_i18n.rb
91
+ - lib/wca_i18n/translation.rb
92
+ - lib/wca_i18n/version.rb
93
+ - lib/wca_i18n/yaml_with_comments.rb
94
+ - wca_i18n.gemspec
95
+ homepage: https://github.com/thewca/wca_i18n
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.6.14
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Track how up to date Rails translations are.
119
+ test_files: []