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.
- checksums.yaml +4 -4
- data/.github/CODEOWNERS +4 -0
- data/.github/FUNDING.yml +1 -1
- data/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
- data/.github/ISSUE_TEMPLATE/config.yml +5 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +23 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +38 -0
- data/.github/dependabot.yml +15 -0
- data/.github/workflows/ruby-tests.yml +52 -0
- data/.gitignore +11 -7
- data/.rubocop.yml +16 -0
- data/CHANGELOG.md +12 -571
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +79 -0
- data/Gemfile +3 -0
- data/LICENSE.md +20 -0
- data/README.md +180 -1003
- data/Rakefile +10 -20
- data/exe/i18n +5 -0
- data/i18n-js.gemspec +48 -29
- data/images/i18njs-check.gif +0 -0
- data/lib/guard/i18n-js/templates/Guardfile +10 -0
- data/lib/guard/i18n-js/version.rb +13 -0
- data/lib/guard/i18n-js.rb +78 -0
- data/lib/i18n-js/cli/check_command.rb +157 -0
- data/lib/i18n-js/cli/command.rb +69 -0
- data/lib/i18n-js/cli/export_command.rb +81 -0
- data/lib/i18n-js/cli/init_command.rb +52 -0
- data/lib/i18n-js/cli/ui.rb +64 -0
- data/lib/i18n-js/cli/version_command.rb +18 -0
- data/lib/i18n-js/cli.rb +55 -0
- data/lib/i18n-js/listen.rb +81 -0
- data/lib/i18n-js/schema.rb +104 -0
- data/lib/i18n-js/version.rb +5 -0
- data/lib/i18n-js.rb +73 -1
- metadata +127 -198
- data/.editorconfig +0 -24
- data/.github/workflows/tests.yaml +0 -106
- data/.npmignore +0 -27
- data/Appraisals +0 -52
- data/app/assets/javascripts/i18n/filtered.js.erb +0 -23
- data/app/assets/javascripts/i18n/shims.js +0 -240
- data/app/assets/javascripts/i18n/translations.js +0 -3
- data/app/assets/javascripts/i18n.js +0 -1095
- data/gemfiles/i18n_0_6.gemfile +0 -7
- data/gemfiles/i18n_0_7.gemfile +0 -7
- data/gemfiles/i18n_0_8.gemfile +0 -7
- data/gemfiles/i18n_0_9.gemfile +0 -7
- data/gemfiles/i18n_1_0.gemfile +0 -7
- data/gemfiles/i18n_1_1.gemfile +0 -7
- data/gemfiles/i18n_1_10.gemfile +0 -7
- data/gemfiles/i18n_1_2.gemfile +0 -7
- data/gemfiles/i18n_1_3.gemfile +0 -7
- data/gemfiles/i18n_1_4.gemfile +0 -7
- data/gemfiles/i18n_1_5.gemfile +0 -7
- data/gemfiles/i18n_1_6.gemfile +0 -7
- data/gemfiles/i18n_1_7.gemfile +0 -7
- data/gemfiles/i18n_1_8.gemfile +0 -7
- data/gemfiles/i18n_1_9.gemfile +0 -7
- data/lib/i18n/js/dependencies.rb +0 -67
- data/lib/i18n/js/engine.rb +0 -87
- data/lib/i18n/js/fallback_locales.rb +0 -70
- data/lib/i18n/js/formatters/base.rb +0 -25
- data/lib/i18n/js/formatters/js.rb +0 -39
- data/lib/i18n/js/formatters/json.rb +0 -13
- data/lib/i18n/js/middleware.rb +0 -82
- data/lib/i18n/js/private/config_store.rb +0 -31
- data/lib/i18n/js/private/hash_with_symbol_keys.rb +0 -36
- data/lib/i18n/js/segment.rb +0 -81
- data/lib/i18n/js/utils.rb +0 -91
- data/lib/i18n/js/version.rb +0 -7
- data/lib/i18n/js.rb +0 -274
- data/lib/rails/generators/i18n/js/config/config_generator.rb +0 -19
- data/lib/rails/generators/i18n/js/config/templates/i18n-js.yml +0 -27
- data/lib/tasks/export.rake +0 -8
- data/package.json +0 -25
- data/spec/fixtures/custom_path.yml +0 -5
- data/spec/fixtures/default.yml +0 -5
- data/spec/fixtures/erb.yml +0 -5
- data/spec/fixtures/except_condition.yml +0 -7
- data/spec/fixtures/js_available_locales_custom.yml +0 -1
- data/spec/fixtures/js_export_dir_custom.yml +0 -7
- data/spec/fixtures/js_export_dir_none.yml +0 -6
- data/spec/fixtures/js_extend_parent.yml +0 -6
- data/spec/fixtures/js_extend_segment.yml +0 -6
- data/spec/fixtures/js_file_per_locale.yml +0 -7
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml +0 -4
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_hash.yml +0 -6
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale.yml +0 -4
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml +0 -4
- data/spec/fixtures/js_file_per_locale_with_fallbacks_enabled.yml +0 -4
- data/spec/fixtures/js_file_per_locale_without_fallbacks.yml +0 -4
- data/spec/fixtures/js_file_with_namespace_prefix_and_pretty_print.yml +0 -9
- data/spec/fixtures/js_sort_translation_keys_false.yml +0 -6
- data/spec/fixtures/js_sort_translation_keys_true.yml +0 -6
- data/spec/fixtures/json_only.yml +0 -18
- data/spec/fixtures/locales.yml +0 -133
- data/spec/fixtures/merge_plurals.yml +0 -6
- data/spec/fixtures/merge_plurals_with_no_overrides.yml +0 -4
- data/spec/fixtures/merge_plurals_with_partial_overrides.yml +0 -4
- data/spec/fixtures/millions.yml +0 -4
- data/spec/fixtures/multiple_conditions.yml +0 -7
- data/spec/fixtures/multiple_conditions_per_locale.yml +0 -7
- data/spec/fixtures/multiple_files.yml +0 -7
- data/spec/fixtures/no_config.yml +0 -2
- data/spec/fixtures/no_scope.yml +0 -4
- data/spec/fixtures/simple_scope.yml +0 -5
- data/spec/js/currency.spec.js +0 -62
- data/spec/js/current_locale.spec.js +0 -19
- data/spec/js/dates.spec.js +0 -276
- data/spec/js/defaults.spec.js +0 -31
- data/spec/js/extend.spec.js +0 -110
- data/spec/js/interpolation.spec.js +0 -124
- data/spec/js/jasmine/MIT.LICENSE +0 -20
- data/spec/js/jasmine/jasmine-html.js +0 -190
- data/spec/js/jasmine/jasmine.css +0 -166
- data/spec/js/jasmine/jasmine.js +0 -2476
- data/spec/js/jasmine/jasmine_favicon.png +0 -0
- data/spec/js/json_parsable.spec.js +0 -14
- data/spec/js/locales.spec.js +0 -31
- data/spec/js/localization.spec.js +0 -78
- data/spec/js/numbers.spec.js +0 -174
- data/spec/js/placeholder.spec.js +0 -24
- data/spec/js/pluralization.spec.js +0 -228
- data/spec/js/prepare_options.spec.js +0 -41
- data/spec/js/require.js +0 -2083
- data/spec/js/specs.html +0 -49
- data/spec/js/specs_requirejs.html +0 -72
- data/spec/js/translate.spec.js +0 -304
- data/spec/js/translations.js +0 -188
- data/spec/js/utility_functions.spec.js +0 -20
- data/spec/ruby/i18n/js/fallback_locales_spec.rb +0 -84
- data/spec/ruby/i18n/js/segment_spec.rb +0 -286
- data/spec/ruby/i18n/js/utils_spec.rb +0 -138
- data/spec/ruby/i18n/js_spec.rb +0 -797
- data/spec/spec_helper.rb +0 -80
- data/yarn.lock +0 -138
data/lib/i18n-js/cli.rb
ADDED
@@ -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,81 @@
|
|
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
|
+
)
|
12
|
+
return unless Rails.env.development?
|
13
|
+
return if started
|
14
|
+
|
15
|
+
gem "listen"
|
16
|
+
require "listen"
|
17
|
+
require "i18n-js"
|
18
|
+
|
19
|
+
self.started = true
|
20
|
+
|
21
|
+
relative_paths =
|
22
|
+
[config_file, locales_dir].map {|path| relative_path(path) }
|
23
|
+
|
24
|
+
debug("Watching #{relative_paths.inspect}")
|
25
|
+
|
26
|
+
listener(config_file, locales_dir.to_s).start
|
27
|
+
I18nJS.call(config_file: config_file)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.relative_path(path)
|
31
|
+
Pathname.new(path).relative_path_from(Rails.root).to_s
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.relative_path_list(paths)
|
35
|
+
paths.map {|path| relative_path(path) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.debug(message)
|
39
|
+
logger.tagged("i18n-js") { logger.debug(message) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.logger
|
43
|
+
@logger ||= ActiveSupport::TaggedLogging.new(Rails.logger)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.listener(config_file, locales_dir)
|
47
|
+
paths = [File.dirname(config_file), locales_dir]
|
48
|
+
|
49
|
+
Listen.to(*paths) do |changed, added, removed|
|
50
|
+
changes = compute_changes(
|
51
|
+
[config_file, locales_dir],
|
52
|
+
changed,
|
53
|
+
added,
|
54
|
+
removed
|
55
|
+
)
|
56
|
+
|
57
|
+
next unless changes.any?
|
58
|
+
|
59
|
+
debug(changes.map {|key, value| "#{key}=#{value.inspect}" }.join(", "))
|
60
|
+
|
61
|
+
::I18n.backend.reload!
|
62
|
+
I18nJS.call(config_file: config_file)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.compute_changes(paths, changed, added, removed)
|
67
|
+
paths = paths.map {|path| relative_path(path) }
|
68
|
+
|
69
|
+
{
|
70
|
+
changed: included_on_watched_paths(paths, changed),
|
71
|
+
added: included_on_watched_paths(paths, added),
|
72
|
+
removed: included_on_watched_paths(paths, removed)
|
73
|
+
}.select {|_k, v| v.any? }
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.included_on_watched_paths(paths, changes)
|
77
|
+
changes.map {|change| relative_path(change) }.select do |change|
|
78
|
+
paths.any? {|path| change.start_with?(path) }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
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
|
data/lib/i18n-js.rb
CHANGED
@@ -1 +1,73 @@
|
|
1
|
-
|
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
|