i18n-js 3.8.2 → 4.2.3

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.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +4 -0
  3. data/.github/FUNDING.yml +1 -1
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
  5. data/.github/ISSUE_TEMPLATE/config.yml +5 -0
  6. data/.github/ISSUE_TEMPLATE/feature_request.md +23 -0
  7. data/.github/PULL_REQUEST_TEMPLATE.md +38 -0
  8. data/.github/dependabot.yml +15 -0
  9. data/.github/workflows/ruby-tests.yml +73 -0
  10. data/.gitignore +13 -7
  11. data/.rubocop.yml +19 -0
  12. data/CHANGELOG.md +47 -512
  13. data/CODE_OF_CONDUCT.md +74 -0
  14. data/CONTRIBUTING.md +79 -0
  15. data/Gemfile +3 -0
  16. data/LICENSE.md +20 -0
  17. data/MIGRATING_FROM_V3_TO_V4.md +192 -0
  18. data/README.md +425 -921
  19. data/Rakefile +10 -20
  20. data/bin/release +81 -0
  21. data/exe/i18n +5 -0
  22. data/i18n-js.gemspec +51 -29
  23. data/lib/guard/i18n-js/templates/Guardfile +10 -0
  24. data/lib/guard/i18n-js/version.rb +13 -0
  25. data/lib/guard/i18n-js.rb +95 -0
  26. data/lib/i18n-js/clean_hash.rb +13 -0
  27. data/lib/i18n-js/cli/check_command.rb +17 -0
  28. data/lib/i18n-js/cli/command.rb +79 -0
  29. data/lib/i18n-js/cli/export_command.rb +95 -0
  30. data/lib/i18n-js/cli/init_command.rb +52 -0
  31. data/lib/i18n-js/cli/lint_scripts_command.rb +159 -0
  32. data/lib/i18n-js/cli/lint_translations_command.rb +157 -0
  33. data/lib/i18n-js/cli/plugins_command.rb +67 -0
  34. data/lib/i18n-js/cli/ui.rb +64 -0
  35. data/lib/i18n-js/cli/version_command.rb +18 -0
  36. data/lib/i18n-js/cli.rb +66 -0
  37. data/lib/i18n-js/embed_fallback_translations_plugin.rb +70 -0
  38. data/lib/i18n-js/export_files_plugin.rb +103 -0
  39. data/lib/i18n-js/lint.js +150645 -0
  40. data/lib/i18n-js/lint.ts +196 -0
  41. data/lib/i18n-js/listen.rb +96 -0
  42. data/lib/i18n-js/plugin.rb +103 -0
  43. data/lib/i18n-js/schema.rb +216 -0
  44. data/lib/i18n-js/sort_hash.rb +12 -0
  45. data/lib/i18n-js/version.rb +5 -0
  46. data/lib/i18n-js.rb +107 -1
  47. data/package.json +5 -20
  48. metadata +149 -189
  49. data/.editorconfig +0 -24
  50. data/.github/workflows/tests.yaml +0 -100
  51. data/.npmignore +0 -27
  52. data/Appraisals +0 -44
  53. data/app/assets/javascripts/i18n/filtered.js.erb +0 -23
  54. data/app/assets/javascripts/i18n/shims.js +0 -240
  55. data/app/assets/javascripts/i18n/translations.js +0 -3
  56. data/app/assets/javascripts/i18n.js +0 -1095
  57. data/gemfiles/i18n_0_6.gemfile +0 -7
  58. data/gemfiles/i18n_0_7.gemfile +0 -7
  59. data/gemfiles/i18n_0_8.gemfile +0 -7
  60. data/gemfiles/i18n_0_9.gemfile +0 -7
  61. data/gemfiles/i18n_1_0.gemfile +0 -7
  62. data/gemfiles/i18n_1_1.gemfile +0 -7
  63. data/gemfiles/i18n_1_2.gemfile +0 -7
  64. data/gemfiles/i18n_1_3.gemfile +0 -7
  65. data/gemfiles/i18n_1_4.gemfile +0 -7
  66. data/gemfiles/i18n_1_5.gemfile +0 -7
  67. data/gemfiles/i18n_1_6.gemfile +0 -7
  68. data/gemfiles/i18n_1_7.gemfile +0 -7
  69. data/gemfiles/i18n_1_8.gemfile +0 -7
  70. data/i18njs.png +0 -0
  71. data/lib/i18n/js/dependencies.rb +0 -63
  72. data/lib/i18n/js/engine.rb +0 -87
  73. data/lib/i18n/js/fallback_locales.rb +0 -70
  74. data/lib/i18n/js/formatters/base.rb +0 -25
  75. data/lib/i18n/js/formatters/js.rb +0 -32
  76. data/lib/i18n/js/formatters/json.rb +0 -13
  77. data/lib/i18n/js/middleware.rb +0 -82
  78. data/lib/i18n/js/private/config_store.rb +0 -31
  79. data/lib/i18n/js/private/hash_with_symbol_keys.rb +0 -36
  80. data/lib/i18n/js/segment.rb +0 -80
  81. data/lib/i18n/js/utils.rb +0 -78
  82. data/lib/i18n/js/version.rb +0 -7
  83. data/lib/i18n/js.rb +0 -264
  84. data/lib/rails/generators/i18n/js/config/config_generator.rb +0 -19
  85. data/lib/rails/generators/i18n/js/config/templates/i18n-js.yml +0 -27
  86. data/lib/tasks/export.rake +0 -8
  87. data/spec/fixtures/custom_path.yml +0 -5
  88. data/spec/fixtures/default.yml +0 -5
  89. data/spec/fixtures/erb.yml +0 -5
  90. data/spec/fixtures/except_condition.yml +0 -7
  91. data/spec/fixtures/js_export_dir_custom.yml +0 -7
  92. data/spec/fixtures/js_export_dir_none.yml +0 -6
  93. data/spec/fixtures/js_extend_parent.yml +0 -6
  94. data/spec/fixtures/js_extend_segment.yml +0 -6
  95. data/spec/fixtures/js_file_per_locale.yml +0 -7
  96. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml +0 -4
  97. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_hash.yml +0 -6
  98. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale.yml +0 -4
  99. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml +0 -4
  100. data/spec/fixtures/js_file_per_locale_with_fallbacks_enabled.yml +0 -4
  101. data/spec/fixtures/js_file_per_locale_without_fallbacks.yml +0 -4
  102. data/spec/fixtures/js_file_with_namespace_prefix_and_pretty_print.yml +0 -9
  103. data/spec/fixtures/js_sort_translation_keys_false.yml +0 -6
  104. data/spec/fixtures/js_sort_translation_keys_true.yml +0 -6
  105. data/spec/fixtures/json_only.yml +0 -18
  106. data/spec/fixtures/locales.yml +0 -133
  107. data/spec/fixtures/merge_plurals.yml +0 -6
  108. data/spec/fixtures/merge_plurals_with_no_overrides.yml +0 -4
  109. data/spec/fixtures/merge_plurals_with_partial_overrides.yml +0 -4
  110. data/spec/fixtures/millions.yml +0 -4
  111. data/spec/fixtures/multiple_conditions.yml +0 -7
  112. data/spec/fixtures/multiple_conditions_per_locale.yml +0 -7
  113. data/spec/fixtures/multiple_files.yml +0 -7
  114. data/spec/fixtures/no_config.yml +0 -2
  115. data/spec/fixtures/no_scope.yml +0 -4
  116. data/spec/fixtures/simple_scope.yml +0 -5
  117. data/spec/js/currency.spec.js +0 -62
  118. data/spec/js/current_locale.spec.js +0 -19
  119. data/spec/js/dates.spec.js +0 -276
  120. data/spec/js/defaults.spec.js +0 -31
  121. data/spec/js/extend.spec.js +0 -110
  122. data/spec/js/interpolation.spec.js +0 -124
  123. data/spec/js/jasmine/MIT.LICENSE +0 -20
  124. data/spec/js/jasmine/jasmine-html.js +0 -190
  125. data/spec/js/jasmine/jasmine.css +0 -166
  126. data/spec/js/jasmine/jasmine.js +0 -2476
  127. data/spec/js/jasmine/jasmine_favicon.png +0 -0
  128. data/spec/js/locales.spec.js +0 -31
  129. data/spec/js/localization.spec.js +0 -78
  130. data/spec/js/numbers.spec.js +0 -174
  131. data/spec/js/placeholder.spec.js +0 -24
  132. data/spec/js/pluralization.spec.js +0 -219
  133. data/spec/js/prepare_options.spec.js +0 -41
  134. data/spec/js/require.js +0 -2083
  135. data/spec/js/specs.html +0 -49
  136. data/spec/js/specs_requirejs.html +0 -72
  137. data/spec/js/translate.spec.js +0 -304
  138. data/spec/js/translations.js +0 -188
  139. data/spec/js/utility_functions.spec.js +0 -20
  140. data/spec/ruby/i18n/js/fallback_locales_spec.rb +0 -84
  141. data/spec/ruby/i18n/js/segment_spec.rb +0 -219
  142. data/spec/ruby/i18n/js/utils_spec.rb +0 -106
  143. data/spec/ruby/i18n/js_spec.rb +0 -748
  144. data/spec/spec_helper.rb +0 -80
  145. data/yarn.lock +0 -131
data/Rakefile CHANGED
@@ -1,25 +1,15 @@
1
- require "appraisal"
2
- require "rubygems"
3
- require "bundler"
4
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
5
2
 
6
- Bundler::GemHelper.install_tasks
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "rubocop/rake_task"
7
6
 
8
- RSpec::Core::RakeTask.new(:"spec:ruby")
9
-
10
- desc "Run JavaScript specs"
11
- task "spec:js" do
12
- # Need to call `exit!` manually to propogate exit status
13
- system "npm", "test" or exit!(1)
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.test_files = FileList["test/**/*_test.rb"]
14
11
  end
15
12
 
16
- desc "Run all specs"
17
- task :spec => ["spec:ruby", "spec:js"]
13
+ RuboCop::RakeTask.new
18
14
 
19
- if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
20
- task :default do
21
- sh "appraisal install && rake appraisal spec"
22
- end
23
- else
24
- task :default => :spec
25
- end
15
+ task default: %i[test rubocop]
data/bin/release ADDED
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "optparse"
5
+ require_relative "../lib/i18n-js/version"
6
+
7
+ def write_file(path, contents)
8
+ File.open(File.expand_path(path), "w") do |io|
9
+ io << contents
10
+ end
11
+ end
12
+
13
+ changelog_path = "./CHANGELOG.md"
14
+ version_path = "./lib/i18n-js/version.rb"
15
+
16
+ version = nil
17
+ segments = I18nJS::VERSION.split(".")
18
+ major, minor, patch = *segments.take(3).map(&:to_i)
19
+ pre = segments[4].to_s
20
+ pre_version = pre.gsub(/[^\d]/m, "").to_i
21
+ date = Time.now.strftime("%b %d, %Y")
22
+ dry_run = false
23
+ alpha = false
24
+
25
+ OptionParser.new do |opts|
26
+ opts.on("--major") do
27
+ version = "#{major + 1}.0.0"
28
+ end
29
+
30
+ opts.on("--minor") do
31
+ version = "#{major}.#{minor + 1}.0"
32
+ end
33
+
34
+ opts.on("--patch") do
35
+ version = "#{major}.#{minor}.#{patch + 1}"
36
+ end
37
+
38
+ opts.on("--alpha") do
39
+ alpha = true
40
+ end
41
+
42
+ opts.on("--dry-run") do
43
+ dry_run = true
44
+ end
45
+ end.parse!
46
+
47
+ version = "#{version}.alpha#{pre_version + 1}" if alpha
48
+
49
+ unless version
50
+ puts "ERROR: You need to use either one of: --major, --minor, --patch"
51
+ exit 1
52
+ end
53
+
54
+ puts "=> Current version: #{I18nJS::VERSION}"
55
+ puts "=> Next version: #{version}"
56
+
57
+ system "yarn", "install"
58
+ system "yarn", "compile"
59
+
60
+ write_file changelog_path,
61
+ File.read(changelog_path)
62
+ .gsub("Unreleased", "v#{version} - #{date}")
63
+
64
+ puts "=> Updated #{changelog_path}"
65
+
66
+ write_file version_path,
67
+ File.read(version_path)
68
+ .gsub(/VERSION = ".*?"/, %[VERSION = "#{version}"])
69
+
70
+ puts "=> Updated #{version_path}"
71
+
72
+ unless dry_run
73
+ system "git", "add", changelog_path, version_path
74
+ system "git", "commit", "-m", "Bump up version (v#{version})"
75
+ system "rake", "release"
76
+ end
77
+
78
+ if dry_run
79
+ system "rake", "build"
80
+ system "git", "checkout", changelog_path, version_path
81
+ end
data/exe/i18n ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/i18n-js/cli"
5
+ I18nJS::CLI.new(argv: ARGV, stdout: $stdout, stderr: $stderr).call
data/i18n-js.gemspec CHANGED
@@ -1,30 +1,52 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "i18n/js/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "i18n-js"
7
- s.version = I18n::JS::VERSION
8
- s.platform = Gem::Platform::RUBY
9
- s.authors = ["Nando Vieira"]
10
- s.email = ["fnando.vieira@gmail.com"]
11
- s.homepage = "http://rubygems.org/gems/i18n-js"
12
- s.summary = "It's a small library to provide the Rails I18n translations on the Javascript."
13
- s.description = s.summary
14
- s.license = "MIT"
15
-
16
- s.files = `git ls-files`.split("\n")
17
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
- s.require_paths = ["lib"]
20
-
21
- s.add_dependency "i18n", ">= 0.6.6"
22
-
23
- s.add_development_dependency "appraisal", "~> 2.3"
24
- s.add_development_dependency "rspec", "~> 3.0"
25
- s.add_development_dependency "rake", "~> 12.0"
26
- s.add_development_dependency "gem-release", ">= 0.7"
27
- s.add_development_dependency "coveralls", ">= 0.7"
28
-
29
- s.required_ruby_version = ">= 2.1.0"
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./lib/i18n-js/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "i18n-js"
7
+ spec.version = I18nJS::VERSION
8
+ spec.authors = ["Nando Vieira"]
9
+ spec.email = ["me@fnando.com"]
10
+
11
+ spec.summary = "Export i18n translations and use them on JavaScript."
12
+ spec.description = spec.summary
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
15
+ spec.metadata = {"rubygems_mfa_required" => "true"}
16
+
17
+ github_url = "https://github.com/fnando/i18n-js"
18
+ github_tree_url = "#{github_url}/tree/v#{spec.version}"
19
+
20
+ spec.homepage = github_url
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["bug_tracker_uri"] = "#{github_url}/issues"
23
+ spec.metadata["source_code_uri"] = github_tree_url
24
+ spec.metadata["changelog_uri"] = "#{github_tree_url}/CHANGELOG.md"
25
+ spec.metadata["documentation_uri"] = "#{github_tree_url}/README.md"
26
+ spec.metadata["license_uri"] = "#{github_tree_url}/LICENSE.md"
27
+
28
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
29
+ `git ls-files -z`
30
+ .split("\x0")
31
+ .reject {|f| f.match(%r{^(test|spec|features|images)/}) }
32
+ end
33
+
34
+ spec.files << "lib/i18n-js/lint.js"
35
+
36
+ spec.bindir = "exe"
37
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
38
+ spec.require_paths = ["lib"]
39
+
40
+ spec.add_dependency "glob", ">= 0.4.0"
41
+ spec.add_dependency "i18n"
42
+
43
+ spec.add_development_dependency "activesupport"
44
+ spec.add_development_dependency "minitest"
45
+ spec.add_development_dependency "minitest-utils"
46
+ spec.add_development_dependency "mocha"
47
+ spec.add_development_dependency "pry-meta"
48
+ spec.add_development_dependency "rake"
49
+ spec.add_development_dependency "rubocop"
50
+ spec.add_development_dependency "rubocop-fnando"
51
+ spec.add_development_dependency "simplecov"
30
52
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard(:"i18n-js",
4
+ run_on_start: true,
5
+ config_file: "./config/i18n.yml",
6
+ require_file: "./config/environment.rb") do
7
+ watch(%r{^(app|config)/locales/.+\.(yml|po)$})
8
+ watch(%r{^config/i18n.yml$})
9
+ watch("Gemfile")
10
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ gem "guard"
4
+ gem "guard-compat"
5
+ require "guard/compat/plugin"
6
+
7
+ require "i18n-js"
8
+
9
+ module Guard
10
+ class I18njsVersion < Plugin
11
+ VERSION = I18nJS::VERSION
12
+ end
13
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ gem "guard"
4
+ gem "guard-compat"
5
+ require "guard/compat/plugin"
6
+
7
+ require "i18n-js"
8
+
9
+ module Guard
10
+ class I18njs < Plugin
11
+ attr_reader :config_file, :require_file, :current_thread
12
+
13
+ def initialize(options = {})
14
+ @config_file = options.delete(:config_file)
15
+ @require_file = options.delete(:require_file)
16
+ super
17
+ end
18
+
19
+ def start
20
+ export_files
21
+ end
22
+
23
+ def stop
24
+ current_thread&.exit
25
+ end
26
+
27
+ def reload
28
+ export_files
29
+ end
30
+
31
+ def run_all
32
+ export_files
33
+ end
34
+
35
+ def run_on_additions(paths)
36
+ export_files(paths)
37
+ end
38
+
39
+ def run_on_modifications(paths)
40
+ export_files(paths)
41
+ end
42
+
43
+ def run_on_removals(paths)
44
+ export_files(paths)
45
+ end
46
+
47
+ def export_files(changes = nil)
48
+ return unless validate_file(:config_file, config_file)
49
+ return unless validate_file(:require_file, require_file)
50
+
51
+ current_thread&.exit
52
+
53
+ info("Changes detected: #{changes.join(', ')}") if changes
54
+
55
+ @current_thread = Thread.new do
56
+ capture do
57
+ system "i18n",
58
+ "export",
59
+ "--config",
60
+ config_file.to_s,
61
+ "--require",
62
+ require_file.to_s,
63
+ "--quiet"
64
+ end
65
+ end
66
+
67
+ current_thread.join
68
+ end
69
+
70
+ def capture
71
+ original = $stdout
72
+ $stdout = StringIO.new
73
+ yield
74
+ rescue StandardError
75
+ # noop
76
+ ensure
77
+ $stdout = original
78
+ end
79
+
80
+ def validate_file(key, file)
81
+ return true if file && File.file?(file)
82
+
83
+ error("#{key.inspect} must be a file")
84
+ false
85
+ end
86
+
87
+ def error(message)
88
+ ::Guard::UI.error "[i18n-js] #{message}"
89
+ end
90
+
91
+ def info(message)
92
+ ::Guard::UI.info "[i18n-js] #{message}"
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJS
4
+ def self.clean_hash(hash)
5
+ hash.keys.each_with_object({}) do |key, buffer|
6
+ value = hash[key]
7
+
8
+ next if value.is_a?(Proc)
9
+
10
+ buffer[key] = value.is_a?(Hash) ? clean_hash(value) : value
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJS
4
+ class CLI
5
+ class CheckCommand < LintTranslationsCommand
6
+ command_name "check"
7
+ description "Check for missing translations based on the default " \
8
+ "locale (DEPRECATED: Use `i18n lint:translations` instead)"
9
+
10
+ def command
11
+ ui.stderr_print "=> WARNING: `i18n check` has been deprecated in " \
12
+ "favor of `i18n lint:translations`"
13
+ super
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJS
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
+
42
+ private def load_config_file(config_file)
43
+ config = Glob::SymbolizeKeys.call(YAML.load_file(config_file))
44
+
45
+ if config.key?(:check)
46
+ config[:lint_translations] ||= config.delete(:check)
47
+ end
48
+
49
+ config
50
+ end
51
+
52
+ private def load_require_file!(require_file)
53
+ require_without_warnings(require_file)
54
+ rescue Exception => error # rubocop:disable Lint/RescueException
55
+ ui.stderr_print("=> ERROR: couldn't load",
56
+ options[:require_file].inspect)
57
+ ui.fail_with(
58
+ "\n#{error_description(error)}\n#{error.backtrace.join("\n")}"
59
+ )
60
+ end
61
+
62
+ private def error_description(error)
63
+ [
64
+ error.class.name,
65
+ error.message
66
+ ].reject(&:empty?).join(" => ")
67
+ end
68
+
69
+ private def require_without_warnings(path)
70
+ old_verbose = $VERBOSE
71
+ $VERBOSE = nil
72
+
73
+ load path
74
+ ensure
75
+ $VERBOSE = old_verbose
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "benchmark"
4
+
5
+ module I18nJS
6
+ class CLI
7
+ class ExportCommand < Command
8
+ command_name "export"
9
+ description "Export translations as JSON files"
10
+
11
+ parse do |opts|
12
+ opts.banner = "Usage: i18n #{name} [options]"
13
+
14
+ opts.on(
15
+ "-cCONFIG_FILE",
16
+ "--config=CONFIG_FILE",
17
+ "The configuration file that will be used"
18
+ ) do |config_file|
19
+ options[:config_file] = config_file
20
+ end
21
+
22
+ opts.on(
23
+ "-rREQUIRE_FILE",
24
+ "--require=REQUIRE_FILE",
25
+ "A Ruby file that must be loaded"
26
+ ) do |require_file|
27
+ options[:require_file] = require_file
28
+ end
29
+
30
+ opts.on(
31
+ "-q",
32
+ "--quiet",
33
+ "A Ruby file that must be loaded"
34
+ ) do |quiet|
35
+ options[:quiet] = quiet
36
+ end
37
+
38
+ opts.on("-h", "--help", "Prints this help") do
39
+ ui.exit_with opts.to_s
40
+ end
41
+ end
42
+
43
+ command do
44
+ set_defaults!
45
+
46
+ unless options[:config_file]
47
+ ui.fail_with("=> ERROR: you need to specify the config file")
48
+ end
49
+
50
+ log("=> Config file:", options[:config_file].inspect)
51
+ config_file = File.expand_path(options[:config_file])
52
+
53
+ if options[:require_file]
54
+ log("=> Require file:", options[:require_file].inspect)
55
+ require_file = File.expand_path(options[:require_file])
56
+ end
57
+
58
+ unless File.file?(config_file)
59
+ ui.fail_with(
60
+ "=> ERROR: config file doesn't exist at",
61
+ config_file.inspect
62
+ )
63
+ end
64
+
65
+ if require_file && !File.file?(require_file)
66
+ ui.fail_with(
67
+ "=> ERROR: require file doesn't exist at",
68
+ require_file.inspect
69
+ )
70
+ end
71
+
72
+ time = Benchmark.realtime do
73
+ load_require_file!(require_file) if require_file
74
+ I18nJS.call(config_file: config_file)
75
+ end
76
+
77
+ log("=> Done in #{time.round(2)}s")
78
+ end
79
+
80
+ private def log(*args)
81
+ return if options[:quiet]
82
+
83
+ ui.stdout_print(*args)
84
+ end
85
+
86
+ private def set_defaults!
87
+ config_file = "./config/i18n.yml"
88
+ require_file = "./config/environment.rb"
89
+
90
+ options[:config_file] ||= config_file if File.file?(config_file)
91
+ options[:require_file] ||= require_file if File.file?(require_file)
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJS
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 #{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(
27
+ options.fetch(:config_file, "config/i18n.yml")
28
+ )
29
+
30
+ if File.file?(file_path)
31
+ ui.fail_with("ERROR: #{file_path} already exists!")
32
+ end
33
+
34
+ FileUtils.mkdir_p(File.dirname(file_path))
35
+
36
+ File.open(file_path, "w") do |file|
37
+ file << <<~YAML
38
+ ---
39
+ translations:
40
+ - file: app/javascript/locales.json
41
+ patterns:
42
+ - "*"
43
+ - "!*.activerecord"
44
+ - "!*.errors"
45
+ - "!*.number.nth"
46
+
47
+ YAML
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end