i18n-js 3.9.2 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) 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 +52 -0
  10. data/.gitignore +11 -7
  11. data/.rubocop.yml +16 -0
  12. data/CHANGELOG.md +12 -571
  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/README.md +180 -1003
  18. data/Rakefile +10 -20
  19. data/exe/i18n +5 -0
  20. data/i18n-js.gemspec +48 -29
  21. data/images/i18njs-check.gif +0 -0
  22. data/lib/guard/i18n-js/templates/Guardfile +10 -0
  23. data/lib/guard/i18n-js/version.rb +13 -0
  24. data/lib/guard/i18n-js.rb +78 -0
  25. data/lib/i18n-js/cli/check_command.rb +157 -0
  26. data/lib/i18n-js/cli/command.rb +69 -0
  27. data/lib/i18n-js/cli/export_command.rb +81 -0
  28. data/lib/i18n-js/cli/init_command.rb +52 -0
  29. data/lib/i18n-js/cli/ui.rb +64 -0
  30. data/lib/i18n-js/cli/version_command.rb +18 -0
  31. data/lib/i18n-js/cli.rb +55 -0
  32. data/lib/i18n-js/listen.rb +81 -0
  33. data/lib/i18n-js/schema.rb +104 -0
  34. data/lib/i18n-js/version.rb +5 -0
  35. data/lib/i18n-js.rb +73 -1
  36. metadata +127 -198
  37. data/.editorconfig +0 -24
  38. data/.github/workflows/tests.yaml +0 -106
  39. data/.npmignore +0 -27
  40. data/Appraisals +0 -52
  41. data/app/assets/javascripts/i18n/filtered.js.erb +0 -23
  42. data/app/assets/javascripts/i18n/shims.js +0 -240
  43. data/app/assets/javascripts/i18n/translations.js +0 -3
  44. data/app/assets/javascripts/i18n.js +0 -1095
  45. data/gemfiles/i18n_0_6.gemfile +0 -7
  46. data/gemfiles/i18n_0_7.gemfile +0 -7
  47. data/gemfiles/i18n_0_8.gemfile +0 -7
  48. data/gemfiles/i18n_0_9.gemfile +0 -7
  49. data/gemfiles/i18n_1_0.gemfile +0 -7
  50. data/gemfiles/i18n_1_1.gemfile +0 -7
  51. data/gemfiles/i18n_1_10.gemfile +0 -7
  52. data/gemfiles/i18n_1_2.gemfile +0 -7
  53. data/gemfiles/i18n_1_3.gemfile +0 -7
  54. data/gemfiles/i18n_1_4.gemfile +0 -7
  55. data/gemfiles/i18n_1_5.gemfile +0 -7
  56. data/gemfiles/i18n_1_6.gemfile +0 -7
  57. data/gemfiles/i18n_1_7.gemfile +0 -7
  58. data/gemfiles/i18n_1_8.gemfile +0 -7
  59. data/gemfiles/i18n_1_9.gemfile +0 -7
  60. data/lib/i18n/js/dependencies.rb +0 -67
  61. data/lib/i18n/js/engine.rb +0 -87
  62. data/lib/i18n/js/fallback_locales.rb +0 -70
  63. data/lib/i18n/js/formatters/base.rb +0 -25
  64. data/lib/i18n/js/formatters/js.rb +0 -39
  65. data/lib/i18n/js/formatters/json.rb +0 -13
  66. data/lib/i18n/js/middleware.rb +0 -82
  67. data/lib/i18n/js/private/config_store.rb +0 -31
  68. data/lib/i18n/js/private/hash_with_symbol_keys.rb +0 -36
  69. data/lib/i18n/js/segment.rb +0 -81
  70. data/lib/i18n/js/utils.rb +0 -91
  71. data/lib/i18n/js/version.rb +0 -7
  72. data/lib/i18n/js.rb +0 -274
  73. data/lib/rails/generators/i18n/js/config/config_generator.rb +0 -19
  74. data/lib/rails/generators/i18n/js/config/templates/i18n-js.yml +0 -27
  75. data/lib/tasks/export.rake +0 -8
  76. data/package.json +0 -25
  77. data/spec/fixtures/custom_path.yml +0 -5
  78. data/spec/fixtures/default.yml +0 -5
  79. data/spec/fixtures/erb.yml +0 -5
  80. data/spec/fixtures/except_condition.yml +0 -7
  81. data/spec/fixtures/js_available_locales_custom.yml +0 -1
  82. data/spec/fixtures/js_export_dir_custom.yml +0 -7
  83. data/spec/fixtures/js_export_dir_none.yml +0 -6
  84. data/spec/fixtures/js_extend_parent.yml +0 -6
  85. data/spec/fixtures/js_extend_segment.yml +0 -6
  86. data/spec/fixtures/js_file_per_locale.yml +0 -7
  87. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml +0 -4
  88. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_hash.yml +0 -6
  89. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale.yml +0 -4
  90. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml +0 -4
  91. data/spec/fixtures/js_file_per_locale_with_fallbacks_enabled.yml +0 -4
  92. data/spec/fixtures/js_file_per_locale_without_fallbacks.yml +0 -4
  93. data/spec/fixtures/js_file_with_namespace_prefix_and_pretty_print.yml +0 -9
  94. data/spec/fixtures/js_sort_translation_keys_false.yml +0 -6
  95. data/spec/fixtures/js_sort_translation_keys_true.yml +0 -6
  96. data/spec/fixtures/json_only.yml +0 -18
  97. data/spec/fixtures/locales.yml +0 -133
  98. data/spec/fixtures/merge_plurals.yml +0 -6
  99. data/spec/fixtures/merge_plurals_with_no_overrides.yml +0 -4
  100. data/spec/fixtures/merge_plurals_with_partial_overrides.yml +0 -4
  101. data/spec/fixtures/millions.yml +0 -4
  102. data/spec/fixtures/multiple_conditions.yml +0 -7
  103. data/spec/fixtures/multiple_conditions_per_locale.yml +0 -7
  104. data/spec/fixtures/multiple_files.yml +0 -7
  105. data/spec/fixtures/no_config.yml +0 -2
  106. data/spec/fixtures/no_scope.yml +0 -4
  107. data/spec/fixtures/simple_scope.yml +0 -5
  108. data/spec/js/currency.spec.js +0 -62
  109. data/spec/js/current_locale.spec.js +0 -19
  110. data/spec/js/dates.spec.js +0 -276
  111. data/spec/js/defaults.spec.js +0 -31
  112. data/spec/js/extend.spec.js +0 -110
  113. data/spec/js/interpolation.spec.js +0 -124
  114. data/spec/js/jasmine/MIT.LICENSE +0 -20
  115. data/spec/js/jasmine/jasmine-html.js +0 -190
  116. data/spec/js/jasmine/jasmine.css +0 -166
  117. data/spec/js/jasmine/jasmine.js +0 -2476
  118. data/spec/js/jasmine/jasmine_favicon.png +0 -0
  119. data/spec/js/json_parsable.spec.js +0 -14
  120. data/spec/js/locales.spec.js +0 -31
  121. data/spec/js/localization.spec.js +0 -78
  122. data/spec/js/numbers.spec.js +0 -174
  123. data/spec/js/placeholder.spec.js +0 -24
  124. data/spec/js/pluralization.spec.js +0 -228
  125. data/spec/js/prepare_options.spec.js +0 -41
  126. data/spec/js/require.js +0 -2083
  127. data/spec/js/specs.html +0 -49
  128. data/spec/js/specs_requirejs.html +0 -72
  129. data/spec/js/translate.spec.js +0 -304
  130. data/spec/js/translations.js +0 -188
  131. data/spec/js/utility_functions.spec.js +0 -20
  132. data/spec/ruby/i18n/js/fallback_locales_spec.rb +0 -84
  133. data/spec/ruby/i18n/js/segment_spec.rb +0 -286
  134. data/spec/ruby/i18n/js/utils_spec.rb +0 -138
  135. data/spec/ruby/i18n/js_spec.rb +0 -797
  136. data/spec/spec_helper.rb +0 -80
  137. data/yarn.lock +0 -138
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/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,49 @@
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 = "https://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)/}) }
32
+ end
33
+
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_dependency "glob"
39
+ spec.add_dependency "i18n"
40
+
41
+ spec.add_development_dependency "activesupport"
42
+ spec.add_development_dependency "minitest"
43
+ spec.add_development_dependency "minitest-utils"
44
+ spec.add_development_dependency "pry-meta"
45
+ spec.add_development_dependency "rake"
46
+ spec.add_development_dependency "rubocop"
47
+ spec.add_development_dependency "rubocop-fnando"
48
+ spec.add_development_dependency "simplecov"
30
49
  end
Binary file
@@ -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,78 @@
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
+ require @require_file
57
+ ::I18nJS.call(config_file: @config_file)
58
+ end
59
+
60
+ current_thread.join
61
+ end
62
+
63
+ def validate_file(key, file)
64
+ return true if file && File.file?(file)
65
+
66
+ error("#{key.inspect} must be a file")
67
+ false
68
+ end
69
+
70
+ def error(message)
71
+ ::Guard::UI.error "[guard-i18n-js] #{message}"
72
+ end
73
+
74
+ def info(message)
75
+ ::Guard::UI.info "[guard-i18n-js] #{message}"
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,157 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "benchmark"
4
+
5
+ module I18nJS
6
+ class CLI
7
+ class CheckCommand < Command
8
+ command_name "check"
9
+ description "Check for missing translations"
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
+ "--[no-]color",
32
+ "Force colored output"
33
+ ) do |colored|
34
+ options[:colored] = colored
35
+ end
36
+
37
+ opts.on("-h", "--help", "Prints this help") do
38
+ ui.exit_with opts.to_s
39
+ end
40
+ end
41
+
42
+ command do
43
+ set_defaults!
44
+ ui.colored = options[:colored]
45
+
46
+ unless options[:config_file]
47
+ ui.fail_with("=> ERROR: you need to specify the config file")
48
+ end
49
+
50
+ ui.stdout_print("=> Config file:", options[:config_file].inspect)
51
+ config_file = File.expand_path(options[:config_file])
52
+
53
+ if options[:require_file]
54
+ ui.stdout_print("=> 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
+ config = Glob::SymbolizeKeys.call(YAML.load_file(config_file))
73
+ Schema.validate!(config)
74
+
75
+ load_require_file!(require_file) if require_file
76
+
77
+ default_locale = I18n.default_locale
78
+ available_locales = I18n.available_locales
79
+ ignored_keys = config.dig(:check, :ignore) || []
80
+
81
+ mapping = available_locales.each_with_object({}) do |locale, buffer|
82
+ buffer[locale] =
83
+ Glob::Map.call(Glob.filter(I18nJS.translations, ["#{locale}.*"]))
84
+ .map {|key| key.gsub(/^.*?\./, "") }
85
+ end
86
+
87
+ default_locale_keys = mapping.delete(default_locale)
88
+
89
+ if ignored_keys.any?
90
+ ui.stdout_print "=> Check #{options[:config_file].inspect} for " \
91
+ "ignored keys."
92
+ end
93
+
94
+ ui.stdout_print "=> #{default_locale}: #{default_locale_keys.size} " \
95
+ "translations"
96
+
97
+ total_missing_count = 0
98
+
99
+ mapping.each do |locale, partial_keys|
100
+ ignored_count = 0
101
+
102
+ # Compute list of filtered keys (i.e. keys not ignored)
103
+ filtered_keys = partial_keys.reject do |key|
104
+ key = "#{locale}.#{key}"
105
+
106
+ ignored = ignored_keys.include?(key)
107
+ ignored_count += 1 if ignored
108
+ ignored
109
+ end
110
+
111
+ extraneous = (partial_keys - default_locale_keys).reject do |key|
112
+ key = "#{locale}.#{key}"
113
+ ignored = ignored_keys.include?(key)
114
+ ignored_count += 1 if ignored
115
+ ignored
116
+ end
117
+
118
+ missing = (default_locale_keys - (filtered_keys - extraneous))
119
+ .reject {|key| ignored_keys.include?("#{locale}.#{key}") }
120
+
121
+ ignored_count += extraneous.size
122
+ total_missing_count += missing.size
123
+
124
+ ui.stdout_print "=> #{locale}: #{missing.size} missing, " \
125
+ "#{extraneous.size} extraneous, " \
126
+ "#{ignored_count} ignored"
127
+
128
+ all_keys = (default_locale_keys + extraneous + missing).uniq.sort
129
+
130
+ all_keys.each do |key|
131
+ next if ignored_keys.include?("#{locale}.#{key}")
132
+
133
+ label = if extraneous.include?(key)
134
+ ui.yellow("extraneous")
135
+ elsif missing.include?(key)
136
+ ui.red("missing")
137
+ else
138
+ next
139
+ end
140
+
141
+ ui.stdout_print(" - #{locale}.#{key} (#{label})")
142
+ end
143
+ end
144
+
145
+ exit(1) if total_missing_count.nonzero?
146
+ end
147
+
148
+ private def set_defaults!
149
+ config_file = "./config/i18n.yml"
150
+ require_file = "./config/environment.rb"
151
+
152
+ options[:config_file] ||= config_file if File.file?(config_file)
153
+ options[:require_file] ||= require_file if File.file?(require_file)
154
+ end
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,69 @@
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_require_file!(require_file)
43
+ require_without_warnings(require_file)
44
+ rescue Exception => error # rubocop:disable Lint/RescueException
45
+ ui.stderr_print("=> ERROR: couldn't load",
46
+ options[:require_file].inspect)
47
+ ui.fail_with(
48
+ "\n#{error_description(error)}\n#{error.backtrace.join("\n")}"
49
+ )
50
+ end
51
+
52
+ private def error_description(error)
53
+ [
54
+ error.class.name,
55
+ error.message
56
+ ].reject(&:empty?).join(" => ")
57
+ end
58
+
59
+ private def require_without_warnings(path)
60
+ old_verbose = $VERBOSE
61
+ $VERBOSE = nil
62
+
63
+ load path
64
+ ensure
65
+ $VERBOSE = old_verbose
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,81 @@
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("-h", "--help", "Prints this help") do
31
+ ui.exit_with opts.to_s
32
+ end
33
+ end
34
+
35
+ command do
36
+ set_defaults!
37
+
38
+ unless options[:config_file]
39
+ ui.fail_with("=> ERROR: you need to specify the config file")
40
+ end
41
+
42
+ ui.stdout_print("=> Config file:", options[:config_file].inspect)
43
+ config_file = File.expand_path(options[:config_file])
44
+
45
+ if options[:require_file]
46
+ ui.stdout_print("=> Require file:", options[:require_file].inspect)
47
+ require_file = File.expand_path(options[:require_file])
48
+ end
49
+
50
+ unless File.file?(config_file)
51
+ ui.fail_with(
52
+ "=> ERROR: config file doesn't exist at",
53
+ config_file.inspect
54
+ )
55
+ end
56
+
57
+ if require_file && !File.file?(require_file)
58
+ ui.fail_with(
59
+ "=> ERROR: require file doesn't exist at",
60
+ require_file.inspect
61
+ )
62
+ end
63
+
64
+ time = Benchmark.realtime do
65
+ load_require_file!(require_file) if require_file
66
+ I18nJS.call(config_file: config_file)
67
+ end
68
+
69
+ ui.stdout_print("=> Done in #{time.round(2)}s")
70
+ end
71
+
72
+ private def set_defaults!
73
+ config_file = "./config/i18n.yml"
74
+ require_file = "./config/environment.rb"
75
+
76
+ options[:config_file] ||= config_file if File.file?(config_file)
77
+ options[:require_file] ||= require_file if File.file?(require_file)
78
+ end
79
+ end
80
+ end
81
+ 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
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJS
4
+ class CLI
5
+ class UI
6
+ attr_reader :stdout, :stderr
7
+ attr_accessor :colored
8
+
9
+ def initialize(stdout:, stderr:, colored: nil)
10
+ @stdout = stdout
11
+ @stderr = stderr
12
+ @colored = colored
13
+ end
14
+
15
+ def stdout_print(*message)
16
+ stdout << "#{message.join(' ')}\n"
17
+ end
18
+
19
+ def stderr_print(*message)
20
+ stderr << "#{message.join(' ')}\n"
21
+ end
22
+
23
+ def fail_with(*message)
24
+ stderr_print(message)
25
+ exit(1)
26
+ end
27
+
28
+ def exit_with(*message)
29
+ stdout_print(message)
30
+ exit(0)
31
+ end
32
+
33
+ def yellow(text)
34
+ ansi(text, 33)
35
+ end
36
+
37
+ def red(text)
38
+ ansi(text, 31)
39
+ end
40
+
41
+ def colored?
42
+ colored_output = if colored.nil?
43
+ stdout.tty?
44
+ else
45
+ colored
46
+ end
47
+
48
+ colored_output && !no_color?
49
+ end
50
+
51
+ def ansi(text, code)
52
+ if colored?
53
+ "\e[#{code}m#{text}\e[0m"
54
+ else
55
+ text
56
+ end
57
+ end
58
+
59
+ def no_color?
60
+ !ENV["NO_COLOR"].nil? && ENV["NO_COLOR"] == "1"
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJS
4
+ class CLI
5
+ class VersionCommand < Command
6
+ command_name "version"
7
+ description "Show package version"
8
+
9
+ parse do |opts|
10
+ opts.banner = "Usage: i18n #{name}"
11
+
12
+ opts.on_tail do
13
+ ui.exit_with("v#{I18nJS::VERSION}")
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end