Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Prefix your message with one of the following:
- [Security] in case of vulnerabilities.
-->

## Unreleased

- [Added] Add `parallel` option to `I18nJS.call` to parallelize file export when
`:locale` placeholder is used.

## v4.2.3 - Mar 29, 2023

- [Fixed] Load plugins when running `i18n lint:*` commands.
Expand Down
2 changes: 2 additions & 0 deletions i18n-js.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "concurrent-ruby", "~> 1.0", ">= 1.3.1"
spec.add_dependency "glob", ">= 0.4.0"
spec.add_dependency "i18n"

spec.add_development_dependency "activesupport"
spec.add_development_dependency "faker"
spec.add_development_dependency "minitest"
spec.add_development_dependency "minitest-utils"
spec.add_development_dependency "mocha"
Expand Down
31 changes: 24 additions & 7 deletions lib/i18n-js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "optparse"
require "erb"
require "digest/md5"
require "concurrent-ruby"

require_relative "i18n-js/schema"
require_relative "i18n-js/version"
Expand All @@ -18,7 +19,7 @@
module I18nJS
MissingConfigError = Class.new(StandardError)

def self.call(config_file: nil, config: nil)
def self.call(config_file: nil, config: nil, parallel: false)
if !config_file && !config
raise MissingConfigError,
"you must set either `config_file` or `config`"
Expand All @@ -32,7 +33,9 @@ def self.call(config_file: nil, config: nil)

exported_files = []

config[:translations].each {|group| exported_files += export_group(group) }
config[:translations].each do |group|
exported_files += export_group(group, parallel:)
end

plugins.each do |plugin|
plugin.after_export(files: exported_files.dup) if plugin.enabled?
Expand All @@ -41,7 +44,7 @@ def self.call(config_file: nil, config: nil)
exported_files
end

def self.export_group(group)
def self.export_group(group, parallel: false)
filtered_translations = Glob.filter(translations, group[:patterns])
filtered_translations =
plugins.reduce(filtered_translations) do |buffer, plugin|
Expand All @@ -57,10 +60,24 @@ def self.export_group(group)
exported_files = []

if output_file_path.include?(":locale")
filtered_translations.each_key do |locale|
locale_file_path = output_file_path.gsub(":locale", locale.to_s)
exported_files << write_file(locale_file_path,
locale => filtered_translations[locale])
if parallel
promises = []
filtered_translations.each_key do |locale|
promises <<
Concurrent::Promises.future(
output_file_path.gsub(":locale", locale.to_s),
locale => filtered_translations[locale]
) do |locale_file_path, translations|
write_file(locale_file_path, translations)
end
end
exported_files = Concurrent::Promises.zip(*promises).value!
else
filtered_translations.each_key do |locale|
locale_file_path = output_file_path.gsub(":locale", locale.to_s)
exported_files << write_file(locale_file_path,
locale => filtered_translations[locale])
end
end
else
exported_files << write_file(output_file_path, filtered_translations)
Expand Down
30 changes: 30 additions & 0 deletions test/benchmarks/parallel_export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "bundler/setup"

require "faker"
require "i18n"
require "yaml"
require "i18n-js"
require "benchmark"

(1..100).each do |i|
File.open("test/fixtures/bench/bench_#{i}.yml", "w") do |f|
strings = {}
5_000.times do
strings[Faker::Lorem.words(number: 4).join("_")] = Faker::Lorem.sentence
end
f.write({"x#{i}": strings}.to_yaml)
end
end

I18n.load_path = Dir["test/fixtures/bench/*.yml"]
I18n.backend.load_translations

Benchmark.bm(7) do |bench|
bench.report("baseline") do
I18nJS.call(config_file: "./test/config/locale_placeholder.yml")
end
bench.report("parallel") do
I18nJS.call(config_file: "./test/config/locale_placeholder.yml",
parallel: true)
end
end
1 change: 1 addition & 0 deletions test/fixtures/bench/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.yml
20 changes: 20 additions & 0 deletions test/i18n-js/exporter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ class ExporterTest < Minitest::Test
"test/output/pt.json"
end

test "exports multiple files using :locale in parallel" do
I18n.load_path << Dir["./test/fixtures/yml/*.yml"]
actual_files =
I18nJS.call(config_file: "./test/config/locale_placeholder.yml",
parallel: true)

expected_files = [
"test/output/en.json",
"test/output/es.json",
"test/output/pt.json"
]

assert_exported_files expected_files,
actual_files
assert_json_file "test/fixtures/expected/multiple_files/es.json",
"test/output/es.json"
assert_json_file "test/fixtures/expected/multiple_files/pt.json",
"test/output/pt.json"
end

test "exports multiple files using :locale as dirname" do
I18n.load_path << Dir["./test/fixtures/yml/*.yml"]
actual_files =
Expand Down