i18n-json 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d34f8bbee444530bf71f4d3b7760e1d08de77a44b41896cf58385c8aa03a688d
4
+ data.tar.gz: 3017e38551fa0c1978b8374589c365561dcea14107f7f6adb6c82179cba3bbae
5
+ SHA512:
6
+ metadata.gz: ea8b93e7caf242b46a4486ae53d81b5cd9534329f6a998e17bce88570a5935dbcfb343f47ff669891b6655d9bb010939bd3bad9157a1a75290c63a2628b3dab0
7
+ data.tar.gz: e719b49e2ac49c152fc07e8aa4f73c0194d665688b6be12b832399b331ef1d1822f3debf3bec4b005e0e4f975cb8cc53c31eb8570f9781175e8328b4947f63dd
@@ -0,0 +1,2 @@
1
+ ---
2
+ custom: ["https://www.paypal.me/nandovieira/🍕"]
@@ -0,0 +1,47 @@
1
+ ---
2
+ name: tests
3
+
4
+ on: [push]
5
+
6
+ jobs:
7
+ build:
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - name: Clone repo
12
+ uses: actions/checkout@v1
13
+
14
+ - name: Ruby cache
15
+ uses: actions/cache@v1
16
+ id: cache
17
+ with:
18
+ path: ~/local/rubies
19
+ key: ruby-2.6.5
20
+
21
+ - name: Rubygems cache
22
+ uses: actions/cache@v1
23
+ with:
24
+ path: vendor/bundle
25
+ key: ${{ runner.os }}-gem-${{ hashFiles('**/Gemfile.lock') }}
26
+ restore-keys: ${{ runner.os }}-gem-
27
+
28
+ - name: Install ruby
29
+ uses: clupprich/ruby-build-action@master
30
+ id: ruby
31
+ with:
32
+ ruby-version: 2.6.5
33
+ cache-available: ${{ steps.cache.outputs.cache-hit == 'true' }}
34
+
35
+ - name: Bundle install
36
+ run: |
37
+ gem install bundler
38
+ bundle config path vendor/bundle
39
+ bundle install --jobs 4 --retry 3
40
+
41
+ - name: Run ruby tests
42
+ run: |
43
+ bundle exec rake
44
+
45
+ - name: Run lint
46
+ run: |
47
+ bundle exec rubocop
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,10 @@
1
+ ---
2
+ inherit_gem:
3
+ rubocop-fnando: .rubocop.yml
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.6
7
+
8
+ Naming/FileName:
9
+ Exclude:
10
+ - lib/i18n-json.rb
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ Nothing
6
+
7
+ ## [0.0.1] 2019-12-16
8
+
9
+ - Initial release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at me@fnando.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in i18n-json.gemspec
6
+ gemspec
@@ -0,0 +1,90 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ i18n-json (0.0.1)
5
+ glob
6
+ i18n
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (6.0.2)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 0.7, < 2)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ zeitwerk (~> 2.2)
17
+ ast (2.4.0)
18
+ awesome_print (1.8.0)
19
+ byebug (11.0.1)
20
+ coderay (1.1.2)
21
+ concurrent-ruby (1.1.5)
22
+ docile (1.3.2)
23
+ glob (0.2.0)
24
+ i18n (1.7.0)
25
+ concurrent-ruby (~> 1.0)
26
+ jaro_winkler (1.5.4)
27
+ json (2.3.0)
28
+ method_source (0.9.2)
29
+ minitest (5.13.0)
30
+ minitest-utils (0.4.6)
31
+ minitest
32
+ parallel (1.19.1)
33
+ parser (2.6.5.0)
34
+ ast (~> 2.4.0)
35
+ pry (0.12.2)
36
+ coderay (~> 1.1.0)
37
+ method_source (~> 0.9.0)
38
+ pry-byebug (3.7.0)
39
+ byebug (~> 11.0)
40
+ pry (~> 0.10)
41
+ pry-meta (0.0.10)
42
+ awesome_print
43
+ pry
44
+ pry-byebug
45
+ pry-remote
46
+ pry-remote (0.1.8)
47
+ pry (~> 0.9)
48
+ slop (~> 3.0)
49
+ rainbow (3.0.0)
50
+ rake (13.0.1)
51
+ redis (4.1.3)
52
+ rubocop (0.77.0)
53
+ jaro_winkler (~> 1.5.1)
54
+ parallel (~> 1.10)
55
+ parser (>= 2.6)
56
+ rainbow (>= 2.2.2, < 4.0)
57
+ ruby-progressbar (~> 1.7)
58
+ unicode-display_width (>= 1.4.0, < 1.7)
59
+ rubocop-fnando (0.0.3)
60
+ ruby-progressbar (1.10.1)
61
+ simplecov (0.17.1)
62
+ docile (~> 1.1)
63
+ json (>= 1.8, < 3)
64
+ simplecov-html (~> 0.10.0)
65
+ simplecov-html (0.10.2)
66
+ slop (3.6.0)
67
+ thread_safe (0.3.6)
68
+ tzinfo (1.2.5)
69
+ thread_safe (~> 0.1)
70
+ unicode-display_width (1.6.0)
71
+ zeitwerk (2.2.2)
72
+
73
+ PLATFORMS
74
+ ruby
75
+
76
+ DEPENDENCIES
77
+ activesupport
78
+ bundler
79
+ i18n-json!
80
+ minitest
81
+ minitest-utils
82
+ pry-meta
83
+ rake
84
+ redis
85
+ rubocop
86
+ rubocop-fnando
87
+ simplecov
88
+
89
+ BUNDLED WITH
90
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Nando Vieira
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.
@@ -0,0 +1,80 @@
1
+ # I18n::JSON
2
+
3
+ [![](https://github.com/fnando/i18n-json/workflows/tests/badge.svg)](https://github.com/fnando/i18n-json/actions?query=workflow%3Atests)
4
+
5
+ Export I18n translations to JSON. A perfect fit if you want to export
6
+ translations to JavaScript.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "i18n-json"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install i18n-json
23
+
24
+ ## Usage
25
+
26
+ About patterns:
27
+
28
+ - Patterns can use `*` as a wildcard and can appear more than once.
29
+ - `*` will include everything
30
+ - `*.messages.*`
31
+ - Patterns starting with `!` are excluded.
32
+ - `!*.activerecord.*` will exclude all ActiveRecord translations.
33
+
34
+ The config file:
35
+
36
+ ```yml
37
+ ---
38
+ translations:
39
+ - file: app/frontend/locales/en.json
40
+ patterns:
41
+ - "en.*"
42
+ - "!en.*.activerecord.errors"
43
+
44
+ - file: app/frontend/locales/:locale.json
45
+ patterns:
46
+ - "*"
47
+ ```
48
+
49
+ The code API:
50
+
51
+ ```ruby
52
+ require "i18n-json"
53
+
54
+ I18n::JSON.call(config_file: "config/i18n.yml")
55
+ ```
56
+
57
+ The CLI API:
58
+
59
+ ```console
60
+ $ i18n-json init --config config/i18n.yml
61
+ $ i18n-json export --config config/i18n.yml --require config/boot.rb
62
+ ```
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ 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).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/i18n-json. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
77
+
78
+ ## Code of Conduct
79
+
80
+ Everyone interacting in the I18n::Json project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/fnando/i18n-json/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "i18n/json"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -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
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/i18n-json"
5
+ I18nJSON::CLI.new(argv: ARGV, stdout: $stdout, stderr: $stderr).call
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "./lib/i18n-json/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "i18n-json"
7
+ spec.version = I18nJSON::VERSION
8
+ spec.authors = ["Nando Vieira"]
9
+ spec.email = ["me@fnando.com"]
10
+
11
+ spec.summary = "Export I18n translations to JSON."
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/fnando/i18n-json"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
21
+ # into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`
24
+ .split("\x0")
25
+ .reject {|f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "glob"
32
+ spec.add_dependency "i18n"
33
+
34
+ spec.add_development_dependency "activesupport"
35
+ spec.add_development_dependency "bundler"
36
+ spec.add_development_dependency "minitest"
37
+ spec.add_development_dependency "minitest-utils"
38
+ spec.add_development_dependency "pry-meta"
39
+ spec.add_development_dependency "rake"
40
+ spec.add_development_dependency "redis"
41
+ spec.add_development_dependency "rubocop"
42
+ spec.add_development_dependency "rubocop-fnando"
43
+ spec.add_development_dependency "simplecov"
44
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "i18n"
4
+ require "json"
5
+ require "yaml"
6
+ require "glob"
7
+ require "fileutils"
8
+ require "optparse"
9
+
10
+ require_relative "./i18n-json/cli"
11
+ require_relative "./i18n-json/cli/command"
12
+ require_relative "./i18n-json/cli/ui"
13
+ require_relative "./i18n-json/cli/init_command"
14
+ require_relative "./i18n-json/cli/export_command"
15
+ require_relative "./i18n-json/schema"
16
+ require_relative "./i18n-json/version"
17
+
18
+ module I18nJSON
19
+ def self.call(config_file:)
20
+ config = Glob::SymbolizeKeys.call(YAML.load_file(config_file))
21
+ Schema.validate!(config)
22
+
23
+ config[:translations].each do |group|
24
+ export_group(group)
25
+ end
26
+ end
27
+
28
+ def self.export_group(group)
29
+ filtered_translations = Glob.filter(translations, group[:patterns])
30
+ output_file_path = File.expand_path(group[:file])
31
+
32
+ if output_file_path.include?(":locale")
33
+ filtered_translations.keys.each do |locale|
34
+ locale_file_path = output_file_path.gsub(/:locale/, locale.to_s)
35
+ write_file(locale_file_path, locale => filtered_translations[locale])
36
+ end
37
+ else
38
+ write_file(output_file_path, filtered_translations)
39
+ end
40
+ end
41
+
42
+ def self.write_file(file_path, translations)
43
+ FileUtils.mkdir_p(File.dirname(file_path))
44
+
45
+ File.open(file_path, "w") do |file|
46
+ file << ::JSON.pretty_generate(translations)
47
+ end
48
+ end
49
+
50
+ def self.translations
51
+ I18n.backend.instance_eval do
52
+ has_been_initialized_before =
53
+ respond_to?(:initialized?, true) && initialized?
54
+ init_translations unless has_been_initialized_before
55
+ translations
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJSON
4
+ class CLI
5
+ attr_reader :ui
6
+
7
+ def initialize(argv:, stdout:, stderr:)
8
+ @argv = argv.dup
9
+ @ui = UI.new(stdout: stdout, stderr: stderr)
10
+ end
11
+
12
+ def call
13
+ command_name = @argv.shift
14
+ command = commands.find {|cmd| cmd.name == command_name }
15
+
16
+ ui.fail_with(root_help) unless command
17
+
18
+ command.call
19
+ end
20
+
21
+ private def command_classes
22
+ [InitCommand, ExportCommand]
23
+ end
24
+
25
+ private def commands
26
+ command_classes.map do |command_class|
27
+ command_class.new(argv: @argv, ui: ui)
28
+ end
29
+ end
30
+
31
+ private def root_help
32
+ commands_list = commands
33
+ .map {|cmd| "- #{cmd.name}: #{cmd.description}" }
34
+ .join("\n")
35
+
36
+ <<~TEXT
37
+ Usage: i18n-json COMMAND FLAGS
38
+
39
+ Commands:
40
+
41
+ #{commands_list}
42
+
43
+ Run `i18n-json COMMAND --help` for more information on specific commands.
44
+ TEXT
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJSON
4
+ class CLI
5
+ class Command
6
+ attr_reader :ui, :argv
7
+
8
+ def self.command_name(name)
9
+ define_method(:name) { name }
10
+ end
11
+
12
+ def self.description(description)
13
+ define_method(:description) { description }
14
+ end
15
+
16
+ def self.parse(&block)
17
+ define_method(:parse) do
18
+ OptionParser
19
+ .new {|opts| instance_exec(opts, &block) }
20
+ .parse!(argv)
21
+ end
22
+ end
23
+
24
+ def self.command(&block)
25
+ define_method(:command, &block)
26
+ end
27
+
28
+ def initialize(argv:, ui:)
29
+ @argv = argv.dup
30
+ @ui = ui
31
+ end
32
+
33
+ def call
34
+ parse
35
+ command
36
+ end
37
+
38
+ def options
39
+ @options ||= {}
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJSON
4
+ class CLI
5
+ class ExportCommand < Command
6
+ command_name "export"
7
+ description "Export translations as JSON files"
8
+
9
+ parse do |opts|
10
+ opts.banner = "Usage: i18n-json #{name} [options]"
11
+
12
+ opts.on(
13
+ "-cCONFIG_FILE",
14
+ "--config=CONFIG_FILE",
15
+ "The configuration file that will be generated"
16
+ ) do |config_file|
17
+ options[:config_file] = config_file
18
+ end
19
+
20
+ opts.on(
21
+ "-rREQUIRE_FILE",
22
+ "--require=REQUIRE_FILE",
23
+ "A Ruby file that must be loaded"
24
+ ) do |require_file|
25
+ options[:require_file] = require_file
26
+ end
27
+
28
+ opts.on("-h", "--help", "Prints this help") do
29
+ ui.exit_with opts.to_s
30
+ end
31
+ end
32
+
33
+ command do
34
+ config_file = File.expand_path(options[:config_file])
35
+
36
+ if options[:require_file]
37
+ require_file = File.expand_path(options[:require_file])
38
+ end
39
+
40
+ unless File.file?(config_file)
41
+ ui.fail_with("ERROR: config file doesn't exist at #{config_file}")
42
+ end
43
+
44
+ if require_file && !File.file?(require_file)
45
+ ui.fail_with("ERROR: require file doesn't exist at #{require_file}")
46
+ end
47
+
48
+ require require_file if require_file
49
+ I18nJSON.call(config_file: config_file)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJSON
4
+ class CLI
5
+ class InitCommand < Command
6
+ command_name "init"
7
+ description "Initialize a project"
8
+
9
+ parse do |opts|
10
+ opts.banner = "Usage: i18n-json #{name} [options]"
11
+
12
+ opts.on(
13
+ "-cCONFIG_FILE",
14
+ "--config=CONFIG_FILE",
15
+ "The configuration file that will be generated"
16
+ ) do |config_file|
17
+ options[:config_file] = config_file
18
+ end
19
+
20
+ opts.on("-h", "--help", "Prints this help") do
21
+ ui.exit_with opts.to_s
22
+ end
23
+ end
24
+
25
+ command do
26
+ file_path = File.expand_path(options[:config_file])
27
+
28
+ if File.file?(file_path)
29
+ ui.fail_with("ERROR: #{file_path} already exists!")
30
+ end
31
+
32
+ FileUtils.mkdir_p(File.dirname(file_path))
33
+
34
+ File.open(file_path, "w") do |file|
35
+ file << <<~YAML
36
+ ---
37
+ translations:
38
+ - file: some/dir/locale.json
39
+ patterns:
40
+ - "*"
41
+ - "!*.activerecord"
42
+
43
+ YAML
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJSON
4
+ class CLI
5
+ class UI
6
+ def initialize(stdout:, stderr:)
7
+ @stdout = stdout
8
+ @stderr = stderr
9
+ end
10
+
11
+ def stdout_print(message)
12
+ @stdout << "#{message}\n"
13
+ end
14
+
15
+ def stderr_print(message)
16
+ @stderr << "#{message}\n"
17
+ end
18
+
19
+ def fail_with(message)
20
+ stderr_print(message)
21
+ exit(1)
22
+ end
23
+
24
+ def exit_with(message)
25
+ stdout_print(message)
26
+ exit(0)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJSON
4
+ class Schema
5
+ InvalidError = Class.new(StandardError)
6
+
7
+ ROOT_KEYS = %i[translations].freeze
8
+ REQUIRED_ROOT_KEYS = %i[translations].freeze
9
+ REQUIRED_TRANSLATION_KEYS = %i[file patterns].freeze
10
+ TRANSLATION_KEYS = %i[file patterns].freeze
11
+
12
+ def self.validate!(target)
13
+ new(target).validate!
14
+ end
15
+
16
+ attr_reader :target
17
+
18
+ def initialize(target)
19
+ @target = target
20
+ end
21
+
22
+ def validate!
23
+ expect_type(:root, target, Hash, target)
24
+
25
+ expect_required_keys(REQUIRED_ROOT_KEYS, target)
26
+ reject_extraneous_keys(ROOT_KEYS, target)
27
+ validate_translations
28
+ end
29
+
30
+ def validate_translations
31
+ translations = target[:translations]
32
+
33
+ expect_type(:translations, translations, Array, target)
34
+ expect_array_with_items(:translations, translations)
35
+
36
+ translations.each do |translation|
37
+ validate_translation(translation)
38
+ end
39
+ end
40
+
41
+ def validate_translation(translation)
42
+ expect_required_keys(REQUIRED_TRANSLATION_KEYS, translation)
43
+ reject_extraneous_keys(TRANSLATION_KEYS, translation)
44
+ expect_type(:file, translation[:file], String, translation)
45
+ expect_type(:patterns, translation[:patterns], Array, translation)
46
+ expect_array_with_items(:patterns, translation[:patterns], translation)
47
+ end
48
+
49
+ def reject(error_message, node = nil)
50
+ node_json = "\n#{JSON.pretty_generate(node)}" if node
51
+ raise InvalidError, "#{error_message}#{node_json}"
52
+ end
53
+
54
+ def expect_type(attribute, value, expected_type, payload)
55
+ return if value.is_a?(expected_type)
56
+
57
+ actual_type = value.class
58
+
59
+ message = [
60
+ "Expected #{attribute.inspect} to be #{expected_type};",
61
+ "got #{actual_type} instead"
62
+ ].join(" ")
63
+
64
+ reject message, payload
65
+ end
66
+
67
+ def expect_array_with_items(attribute, value, payload = value)
68
+ return unless value.empty?
69
+
70
+ reject "Expected #{attribute.inspect} to have at least one item", payload
71
+ end
72
+
73
+ def expect_required_keys(required_keys, value)
74
+ keys = value.keys.map(&:to_sym)
75
+
76
+ required_keys.each do |key|
77
+ next if keys.include?(key)
78
+
79
+ reject "Expected #{key.inspect} to be defined", value
80
+ end
81
+ end
82
+
83
+ def reject_extraneous_keys(allowed_keys, value)
84
+ keys = value.keys.map(&:to_sym)
85
+ extraneous = keys - allowed_keys
86
+
87
+ return if extraneous.empty?
88
+
89
+ reject "Unexpected keys: #{extraneous.join(', ')}", value
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJSON
4
+ VERSION = "0.0.1"
5
+ end
metadata ADDED
@@ -0,0 +1,237 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nando Vieira
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: glob
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-utils
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-meta
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: redis
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-fnando
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: simplecov
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description: Export I18n translations to JSON.
182
+ email:
183
+ - me@fnando.com
184
+ executables:
185
+ - i18n-json
186
+ extensions: []
187
+ extra_rdoc_files: []
188
+ files:
189
+ - ".github/FUNDING.yml"
190
+ - ".github/workflows/tests.yml"
191
+ - ".gitignore"
192
+ - ".rubocop.yml"
193
+ - CHANGELOG.md
194
+ - CODE_OF_CONDUCT.md
195
+ - Gemfile
196
+ - Gemfile.lock
197
+ - LICENSE.txt
198
+ - README.md
199
+ - Rakefile
200
+ - bin/console
201
+ - bin/setup
202
+ - exe/i18n-json
203
+ - i18n-json.gemspec
204
+ - lib/i18n-json.rb
205
+ - lib/i18n-json/cli.rb
206
+ - lib/i18n-json/cli/command.rb
207
+ - lib/i18n-json/cli/export_command.rb
208
+ - lib/i18n-json/cli/init_command.rb
209
+ - lib/i18n-json/cli/ui.rb
210
+ - lib/i18n-json/schema.rb
211
+ - lib/i18n-json/version.rb
212
+ homepage: https://github.com/fnando/i18n-json
213
+ licenses:
214
+ - MIT
215
+ metadata:
216
+ homepage_uri: https://github.com/fnando/i18n-json
217
+ source_code_uri: https://github.com/fnando/i18n-json
218
+ post_install_message:
219
+ rdoc_options: []
220
+ require_paths:
221
+ - lib
222
+ required_ruby_version: !ruby/object:Gem::Requirement
223
+ requirements:
224
+ - - ">="
225
+ - !ruby/object:Gem::Version
226
+ version: '0'
227
+ required_rubygems_version: !ruby/object:Gem::Requirement
228
+ requirements:
229
+ - - ">="
230
+ - !ruby/object:Gem::Version
231
+ version: '0'
232
+ requirements: []
233
+ rubygems_version: 3.0.3
234
+ signing_key:
235
+ specification_version: 4
236
+ summary: Export I18n translations to JSON.
237
+ test_files: []