i18n-js 3.9.2 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) 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 +17 -569
  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 +191 -0
  18. data/README.md +206 -997
  19. data/Rakefile +10 -20
  20. data/exe/i18n +5 -0
  21. data/i18n-js.gemspec +48 -29
  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 +95 -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 +95 -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 +95 -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 -199
  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/i18njs.png +0 -0
  61. data/lib/i18n/js/dependencies.rb +0 -67
  62. data/lib/i18n/js/engine.rb +0 -87
  63. data/lib/i18n/js/fallback_locales.rb +0 -70
  64. data/lib/i18n/js/formatters/base.rb +0 -25
  65. data/lib/i18n/js/formatters/js.rb +0 -39
  66. data/lib/i18n/js/formatters/json.rb +0 -13
  67. data/lib/i18n/js/middleware.rb +0 -82
  68. data/lib/i18n/js/private/config_store.rb +0 -31
  69. data/lib/i18n/js/private/hash_with_symbol_keys.rb +0 -36
  70. data/lib/i18n/js/segment.rb +0 -81
  71. data/lib/i18n/js/utils.rb +0 -91
  72. data/lib/i18n/js/version.rb +0 -7
  73. data/lib/i18n/js.rb +0 -274
  74. data/lib/rails/generators/i18n/js/config/config_generator.rb +0 -19
  75. data/lib/rails/generators/i18n/js/config/templates/i18n-js.yml +0 -27
  76. data/lib/tasks/export.rake +0 -8
  77. data/package.json +0 -25
  78. data/spec/fixtures/custom_path.yml +0 -5
  79. data/spec/fixtures/default.yml +0 -5
  80. data/spec/fixtures/erb.yml +0 -5
  81. data/spec/fixtures/except_condition.yml +0 -7
  82. data/spec/fixtures/js_available_locales_custom.yml +0 -1
  83. data/spec/fixtures/js_export_dir_custom.yml +0 -7
  84. data/spec/fixtures/js_export_dir_none.yml +0 -6
  85. data/spec/fixtures/js_extend_parent.yml +0 -6
  86. data/spec/fixtures/js_extend_segment.yml +0 -6
  87. data/spec/fixtures/js_file_per_locale.yml +0 -7
  88. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml +0 -4
  89. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_hash.yml +0 -6
  90. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale.yml +0 -4
  91. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml +0 -4
  92. data/spec/fixtures/js_file_per_locale_with_fallbacks_enabled.yml +0 -4
  93. data/spec/fixtures/js_file_per_locale_without_fallbacks.yml +0 -4
  94. data/spec/fixtures/js_file_with_namespace_prefix_and_pretty_print.yml +0 -9
  95. data/spec/fixtures/js_sort_translation_keys_false.yml +0 -6
  96. data/spec/fixtures/js_sort_translation_keys_true.yml +0 -6
  97. data/spec/fixtures/json_only.yml +0 -18
  98. data/spec/fixtures/locales.yml +0 -133
  99. data/spec/fixtures/merge_plurals.yml +0 -6
  100. data/spec/fixtures/merge_plurals_with_no_overrides.yml +0 -4
  101. data/spec/fixtures/merge_plurals_with_partial_overrides.yml +0 -4
  102. data/spec/fixtures/millions.yml +0 -4
  103. data/spec/fixtures/multiple_conditions.yml +0 -7
  104. data/spec/fixtures/multiple_conditions_per_locale.yml +0 -7
  105. data/spec/fixtures/multiple_files.yml +0 -7
  106. data/spec/fixtures/no_config.yml +0 -2
  107. data/spec/fixtures/no_scope.yml +0 -4
  108. data/spec/fixtures/simple_scope.yml +0 -5
  109. data/spec/js/currency.spec.js +0 -62
  110. data/spec/js/current_locale.spec.js +0 -19
  111. data/spec/js/dates.spec.js +0 -276
  112. data/spec/js/defaults.spec.js +0 -31
  113. data/spec/js/extend.spec.js +0 -110
  114. data/spec/js/interpolation.spec.js +0 -124
  115. data/spec/js/jasmine/MIT.LICENSE +0 -20
  116. data/spec/js/jasmine/jasmine-html.js +0 -190
  117. data/spec/js/jasmine/jasmine.css +0 -166
  118. data/spec/js/jasmine/jasmine.js +0 -2476
  119. data/spec/js/jasmine/jasmine_favicon.png +0 -0
  120. data/spec/js/json_parsable.spec.js +0 -14
  121. data/spec/js/locales.spec.js +0 -31
  122. data/spec/js/localization.spec.js +0 -78
  123. data/spec/js/numbers.spec.js +0 -174
  124. data/spec/js/placeholder.spec.js +0 -24
  125. data/spec/js/pluralization.spec.js +0 -228
  126. data/spec/js/prepare_options.spec.js +0 -41
  127. data/spec/js/require.js +0 -2083
  128. data/spec/js/specs.html +0 -49
  129. data/spec/js/specs_requirejs.html +0 -72
  130. data/spec/js/translate.spec.js +0 -304
  131. data/spec/js/translations.js +0 -188
  132. data/spec/js/utility_functions.spec.js +0 -20
  133. data/spec/ruby/i18n/js/fallback_locales_spec.rb +0 -84
  134. data/spec/ruby/i18n/js/segment_spec.rb +0 -286
  135. data/spec/ruby/i18n/js/utils_spec.rb +0 -138
  136. data/spec/ruby/i18n/js_spec.rb +0 -797
  137. data/spec/spec_helper.rb +0 -80
  138. data/yarn.lock +0 -138
@@ -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
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../i18n-js"
4
+ require_relative "cli/command"
5
+ require_relative "cli/ui"
6
+ require_relative "cli/init_command"
7
+ require_relative "cli/version_command"
8
+ require_relative "cli/export_command"
9
+ require_relative "cli/check_command"
10
+
11
+ module I18nJS
12
+ class CLI
13
+ attr_reader :ui
14
+
15
+ def initialize(argv:, stdout:, stderr:, colored: stdout.tty?)
16
+ @argv = argv.dup
17
+ @ui = UI.new(stdout: stdout, stderr: stderr, colored: colored)
18
+ end
19
+
20
+ def call
21
+ command_name = @argv.shift
22
+ command = commands.find {|cmd| cmd.name == command_name }
23
+
24
+ ui.fail_with(root_help) unless command
25
+
26
+ command.call
27
+ end
28
+
29
+ private def command_classes
30
+ [InitCommand, ExportCommand, VersionCommand, CheckCommand]
31
+ end
32
+
33
+ private def commands
34
+ command_classes.map do |command_class|
35
+ command_class.new(argv: @argv, ui: ui)
36
+ end
37
+ end
38
+
39
+ private def root_help
40
+ commands_list = commands
41
+ .map {|cmd| "- #{cmd.name}: #{cmd.description}" }
42
+ .join("\n")
43
+
44
+ <<~TEXT
45
+ Usage: i18n COMMAND FLAGS
46
+
47
+ Commands:
48
+
49
+ #{commands_list}
50
+
51
+ Run `i18n COMMAND --help` for more information on specific commands.
52
+ TEXT
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJS
4
+ class << self
5
+ attr_accessor :started
6
+ end
7
+
8
+ def self.listen(
9
+ config_file: Rails.root.join("config/i18n.yml"),
10
+ locales_dir: Rails.root.join("config/locales"),
11
+ options: {}
12
+ )
13
+ return unless Rails.env.development?
14
+ return if started
15
+
16
+ gem "listen"
17
+ require "listen"
18
+ require "i18n-js"
19
+
20
+ self.started = true
21
+
22
+ locales_dirs = Array(locales_dir)
23
+
24
+ relative_paths =
25
+ [config_file, *locales_dirs].map {|path| relative_path(path) }
26
+
27
+ debug("Watching #{relative_paths.inspect}")
28
+
29
+ listener(config_file, locales_dirs.map(&:to_s), options).start
30
+ I18nJS.call(config_file: config_file)
31
+ end
32
+
33
+ def self.relative_path(path)
34
+ Pathname.new(path).relative_path_from(Rails.root).to_s
35
+ end
36
+
37
+ def self.relative_path_list(paths)
38
+ paths.map {|path| relative_path(path) }
39
+ end
40
+
41
+ def self.debug(message)
42
+ logger.tagged("i18n-js") { logger.debug(message) }
43
+ end
44
+
45
+ def self.logger
46
+ @logger ||= ActiveSupport::TaggedLogging.new(Rails.logger)
47
+ end
48
+
49
+ def self.listener(config_file, locales_dirs, options)
50
+ paths = [File.dirname(config_file), *locales_dirs]
51
+
52
+ Listen.to(*paths, options) do |changed, added, removed|
53
+ changes = compute_changes(
54
+ [config_file, *locales_dirs],
55
+ changed,
56
+ added,
57
+ removed
58
+ )
59
+
60
+ next unless changes.any?
61
+
62
+ debug(changes.map {|key, value| "#{key}=#{value.inspect}" }.join(", "))
63
+
64
+ capture do
65
+ system "i18n", "export", "--config", config_file.to_s
66
+ end
67
+ end
68
+ end
69
+
70
+ def self.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 self.compute_changes(paths, changed, added, removed)
81
+ paths = paths.map {|path| relative_path(path) }
82
+
83
+ {
84
+ changed: included_on_watched_paths(paths, changed),
85
+ added: included_on_watched_paths(paths, added),
86
+ removed: included_on_watched_paths(paths, removed)
87
+ }.select {|_k, v| v.any? }
88
+ end
89
+
90
+ def self.included_on_watched_paths(paths, changes)
91
+ changes.map {|change| relative_path(change) }.select do |change|
92
+ paths.any? {|path| change.start_with?(path) }
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJS
4
+ class Schema
5
+ InvalidError = Class.new(StandardError)
6
+
7
+ ROOT_KEYS = %i[translations check].freeze
8
+ REQUIRED_ROOT_KEYS = %i[translations].freeze
9
+ REQUIRED_CHECK_KEYS = %i[ignore].freeze
10
+ REQUIRED_TRANSLATION_KEYS = %i[file patterns].freeze
11
+ TRANSLATION_KEYS = %i[file patterns].freeze
12
+
13
+ def self.validate!(target)
14
+ new(target).validate!
15
+ end
16
+
17
+ attr_reader :target
18
+
19
+ def initialize(target)
20
+ @target = target
21
+ end
22
+
23
+ def validate!
24
+ expect_type(:root, target, Hash, target)
25
+
26
+ expect_required_keys(REQUIRED_ROOT_KEYS, target)
27
+ reject_extraneous_keys(ROOT_KEYS, target)
28
+ validate_translations
29
+ validate_check
30
+ end
31
+
32
+ def validate_check
33
+ return unless target.key?(:check)
34
+
35
+ check = target[:check]
36
+
37
+ expect_type(:check, check, Hash, target)
38
+ expect_required_keys(REQUIRED_CHECK_KEYS, check)
39
+ expect_type(:ignore, check[:ignore], Array, check)
40
+ end
41
+
42
+ def validate_translations
43
+ translations = target[:translations]
44
+
45
+ expect_type(:translations, translations, Array, target)
46
+ expect_array_with_items(:translations, translations)
47
+
48
+ translations.each do |translation|
49
+ validate_translation(translation)
50
+ end
51
+ end
52
+
53
+ def validate_translation(translation)
54
+ expect_required_keys(REQUIRED_TRANSLATION_KEYS, translation)
55
+ reject_extraneous_keys(TRANSLATION_KEYS, translation)
56
+ expect_type(:file, translation[:file], String, translation)
57
+ expect_type(:patterns, translation[:patterns], Array, translation)
58
+ expect_array_with_items(:patterns, translation[:patterns], translation)
59
+ end
60
+
61
+ def reject(error_message, node = nil)
62
+ node_json = "\n#{JSON.pretty_generate(node)}" if node
63
+ raise InvalidError, "#{error_message}#{node_json}"
64
+ end
65
+
66
+ def expect_type(attribute, value, expected_type, payload)
67
+ return if value.is_a?(expected_type)
68
+
69
+ actual_type = value.class
70
+
71
+ message = [
72
+ "Expected #{attribute.inspect} to be #{expected_type};",
73
+ "got #{actual_type} instead"
74
+ ].join(" ")
75
+
76
+ reject message, payload
77
+ end
78
+
79
+ def expect_array_with_items(attribute, value, payload = value)
80
+ return unless value.empty?
81
+
82
+ reject "Expected #{attribute.inspect} to have at least one item", payload
83
+ end
84
+
85
+ def expect_required_keys(required_keys, value)
86
+ keys = value.keys.map(&:to_sym)
87
+
88
+ required_keys.each do |key|
89
+ next if keys.include?(key)
90
+
91
+ reject "Expected #{key.inspect} to be defined", value
92
+ end
93
+ end
94
+
95
+ def reject_extraneous_keys(allowed_keys, value)
96
+ keys = value.keys.map(&:to_sym)
97
+ extraneous = keys - allowed_keys
98
+
99
+ return if extraneous.empty?
100
+
101
+ reject "Unexpected keys: #{extraneous.join(', ')}", value
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nJS
4
+ VERSION = "4.0.1"
5
+ end
data/lib/i18n-js.rb CHANGED
@@ -1 +1,73 @@
1
- require "i18n/js"
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-js/schema"
11
+ require_relative "i18n-js/version"
12
+
13
+ module I18nJS
14
+ MissingConfigError = Class.new(StandardError)
15
+
16
+ def self.call(config_file: nil, config: nil)
17
+ if !config_file && !config
18
+ raise MissingConfigError,
19
+ "you must set either `config_file` or `config`"
20
+ end
21
+
22
+ config = Glob::SymbolizeKeys.call(config || YAML.load_file(config_file))
23
+ Schema.validate!(config)
24
+ exported_files = []
25
+
26
+ config[:translations].each do |group|
27
+ exported_files += export_group(group)
28
+ end
29
+
30
+ exported_files
31
+ end
32
+
33
+ def self.export_group(group)
34
+ filtered_translations = Glob.filter(translations, group[:patterns])
35
+ output_file_path = File.expand_path(group[:file])
36
+ exported_files = []
37
+
38
+ if output_file_path.include?(":locale")
39
+ filtered_translations.each_key do |locale|
40
+ locale_file_path = output_file_path.gsub(/:locale/, locale.to_s)
41
+ exported_files << write_file(locale_file_path,
42
+ locale => filtered_translations[locale])
43
+ end
44
+ else
45
+ exported_files << write_file(output_file_path, filtered_translations)
46
+ end
47
+
48
+ exported_files
49
+ end
50
+
51
+ def self.write_file(file_path, translations)
52
+ FileUtils.mkdir_p(File.dirname(file_path))
53
+
54
+ contents = ::JSON.pretty_generate(translations)
55
+ digest = Digest::MD5.hexdigest(contents)
56
+ file_path = file_path.gsub(/:digest/, digest)
57
+
58
+ File.open(file_path, "w") do |file|
59
+ file << contents
60
+ end
61
+
62
+ file_path
63
+ end
64
+
65
+ def self.translations
66
+ ::I18n.backend.instance_eval do
67
+ has_been_initialized_before =
68
+ respond_to?(:initialized?, true) && initialized?
69
+ init_translations unless has_been_initialized_before
70
+ translations
71
+ end
72
+ end
73
+ end