Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort translations after filtering them out. #695

Merged
merged 4 commits into from
Dec 30, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Prefix your message with one of the following:
## Unreleased

- [Changed] Do not re-export files whose contents haven't changed.
- [Changed] Translations will always be deep sorted.

## v4.2.1 - Dec 25, 2022

Expand Down
5 changes: 4 additions & 1 deletion lib/i18n-js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_relative "i18n-js/schema"
require_relative "i18n-js/version"
require_relative "i18n-js/plugin"
require_relative "i18n-js/sort_hash"

module I18nJS
MissingConfigError = Class.new(StandardError)
Expand Down Expand Up @@ -51,6 +52,7 @@ def self.export_group(group)
end
end

filtered_translations = sort_hash(filtered_translations)
output_file_path = File.expand_path(group[:file])
exported_files = []

Expand All @@ -75,7 +77,8 @@ def self.write_file(file_path, translations)
file_path = file_path.gsub(/:digest/, digest)

# Don't rewrite the file if it already exists and has the same content.
# It helps the asset pipeline or webpack understand that file wasn't changed.
# It helps the asset pipeline or webpack understand that file wasn't
# changed.
if File.exist?(file_path) && File.read(file_path) == contents
return file_path
end
Expand Down
12 changes: 12 additions & 0 deletions lib/i18n-js/sort_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module I18nJS
def self.sort_hash(hash)
return hash unless hash.is_a?(Hash)

hash.keys.sort_by(&:to_s).each_with_object({}) do |key, seed|
value = hash[key]
seed[key] = value.is_a?(Hash) ? sort_hash(value) : value
end
end
end
15 changes: 9 additions & 6 deletions test/i18n-js/exporter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ExporterTest < Minitest::Test
"test/output/group.json"
end

test "export files using erb" do
test "exports files using erb" do
I18n.load_path << Dir["./test/fixtures/yml/*.yml"]
actual_files = I18nJS.call(config_file: "./test/config/config.yml.erb")

Expand Down Expand Up @@ -189,7 +189,7 @@ def transform(translations:)
"test/output/everything.json"
end

test "do not overwrite exported files if identical" do
test "does not overwrite exported files if identical" do
I18n.load_path << Dir["./test/fixtures/yml/*.yml"]
exported_file_path = "test/output/everything.json"

Expand All @@ -198,15 +198,16 @@ def transform(translations:)
assert_exported_files [exported_file_path], actual_files
exported_file_mtime = File.mtime(exported_file_path)

sleep 0.1

# Second run
I18nJS.call(config_file: "./test/config/everything.yml")

# mtime should be the same
assert_equal exported_file_mtime,
File.mtime(exported_file_path)
assert_equal exported_file_mtime, File.mtime(exported_file_path)
end

test "overwrite exported files if not identical" do
test "overwrites exported files if not identical" do
I18n.load_path << Dir["./test/fixtures/yml/*.yml"]
exported_file_path = "test/output/everything.json"

Expand All @@ -215,9 +216,11 @@ def transform(translations:)
assert_exported_files [exported_file_path], actual_files

# Change content of existed exported file (add space to the end of file).
File.open(exported_file_path, 'a') { |f| f << ' ' }
File.open(exported_file_path, "a") {|f| f << " " }
exported_file_mtime = File.mtime(exported_file_path)

sleep 0.1

# Second run
I18nJS.call(config_file: "./test/config/everything.yml")

Expand Down
21 changes: 21 additions & 0 deletions test/i18n-js/sort_hash_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

require "test_helper"

class SortHashTest < Minitest::Test
test "returns non-hash objects" do
assert_equal 1, I18nJS.sort_hash(1)
end

test "sorts shallow hash" do
expected = {a: 1, b: 2, c: 3}

assert_equal expected, I18nJS.sort_hash(c: 3, a: 1, b: 2)
end

test "sorts nested hash" do
expected = {a: {b: 1, c: 2}, d: 3}

assert_equal expected, I18nJS.sort_hash(d: 3, a: {c: 2, b: 1})
end
end