Skip to content

Commit

Permalink
Merge eb7a61c into f734851
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields committed Jan 18, 2019
2 parents f734851 + eb7a61c commit ad0908b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- [Ruby] Rollback `i18n` gem version requirement `>= 0.8.0`
- [Ruby] Fix merging of plural keys across locales.


## [3.2.0] - 2018-11-16
Expand Down
19 changes: 16 additions & 3 deletions lib/i18n/js/utils.rb
@@ -1,10 +1,23 @@
module I18n
module JS
module Utils
# deep_merge by Stefan Rusterholz, see <http://www.ruby-forum.com/topic/142809>.
# The last result is modified to treat `nil` as missing key
PLURAL_KEYS = %i[zero one two few many other].freeze

# Based on deep_merge by Stefan Rusterholz, see <http://www.ruby-forum.com/topic/142809>.
# This method is used to handle I18n fallbacks. Given two equivalent path nodes in two locale trees:
# 1. If the node in the current locale appears to be an I18n pluralization (:one, :other, etc.),
# use the node as-is without merging. This prevents mixing locales with different pluralization schemes.
# 2. Else if both nodes are Hashes, combine (merge) the key-value pairs of the two nodes into one,
# prioritizing the current locale.
# 3. Else if either node is nil, use the other node.
MERGER = proc do |_key, v1, v2|
Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : (v2.nil? ? v1 : v2)
if Hash === v2 && (v2.keys - PLURAL_KEYS).empty?
v2
elsif Hash === v1 && Hash === v2
v1.merge(v2, &MERGER)
else
v2.nil? ? v1 : v2
end
end

HASH_NIL_VALUE_CLEANER_PROC = proc do |k, v|
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/locales.yml
Expand Up @@ -37,6 +37,9 @@ en:
foo: "Foo"
fallback_test: "Success"
null_test: "fallback for null"
merge_plurals:
one: Apple
other: Apples

de:
fallback_test: "Erfolg"
Expand Down Expand Up @@ -81,6 +84,10 @@ fr:
note: "plus de détails"
edit:
title: "Editer"
merge_plurals:
zero: Pomme
one: Pomme
other: Pommes

ja:
admin:
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/merge_plurals.yml
@@ -0,0 +1,6 @@
fallbacks: false

translations:
- file: "tmp/i18n-js/merge_plurals.js"
only:
- "*.merge_plurals"
26 changes: 23 additions & 3 deletions spec/ruby/i18n/js_spec.rb
Expand Up @@ -595,11 +595,10 @@
end

it "exports with the keys sorted" do
expect(subject).to eq(<<EOS
expect(subject).to eq <<EOS
I18n.translations || (I18n.translations = {});
I18n.translations["en"] = I18n.extend((I18n.translations["en"] || {}), {"admin":{"edit":{"title":"Edit"},"show":{"note":"more details","title":"Show"}},"date":{"abbr_day_names":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"abbr_month_names":[null,"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"day_names":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"formats":{"default":"%Y-%m-%d","long":"%B %d, %Y","short":"%b %d"},"month_names":[null,"January","February","March","April","May","June","July","August","September","October","November","December"]},"fallback_test":"Success","foo":"Foo","null_test":"fallback for null","number":{"currency":{"format":{"delimiter":",","format":"%u%n","precision":2,"separator":".","unit":"$"}},"format":{"delimiter":",","precision":3,"separator":"."}},"time":{"am":"am","formats":{"default":"%a, %d %b %Y %H:%M:%S %z","long":"%B %d, %Y %H:%M","short":"%d %b %H:%M"},"pm":"pm"}});
I18n.translations["en"] = I18n.extend((I18n.translations["en"] || {}), {"admin":{"edit":{"title":"Edit"},"show":{"note":"more details","title":"Show"}},"date":{"abbr_day_names":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"abbr_month_names":[null,"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"day_names":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"formats":{"default":"%Y-%m-%d","long":"%B %d, %Y","short":"%b %d"},"month_names":[null,"January","February","March","April","May","June","July","August","September","October","November","December"]},"fallback_test":"Success","foo":"Foo","merge_plurals":{"one":"Apple","other":"Apples"},"null_test":"fallback for null","number":{"currency":{"format":{"delimiter":",","format":"%u%n","precision":2,"separator":".","unit":"$"}},"format":{"delimiter":",","precision":3,"separator":"."}},"time":{"am":"am","formats":{"default":"%a, %d %b %Y %H:%M:%S %z","long":"%B %d, %Y %H:%M","short":"%d %b %H:%M"},"pm":"pm"}});
EOS
)
end
end
end
Expand Down Expand Up @@ -640,4 +639,25 @@
)
end
end

describe "merging plural keys" do
before do
stub_const('I18n::JS::DEFAULT_EXPORT_DIR_PATH', temp_path)
end

subject do
File.read(File.join(I18n::JS.export_i18n_js_dir_path, "merge_plurals.js"))
end

it "should correctly merge the plural keys" do
set_config "merge_plurals.yml"
I18n::JS.export
file_should_exist "merge_plurals.js"

expect(subject).to eq <<EOS
I18n.translations || (I18n.translations = {});
I18n.translations[\"en\"] = I18n.extend((I18n.translations[\"en\"] || {}), {\"merge_plurals\":{\"one\":\"Apple\",\"other\":\"Apples\"}});\nI18n.translations[\"fr\"] = I18n.extend((I18n.translations[\"fr\"] || {}), {\"merge_plurals\":{\"one\":\"Pomme\",\"other\":\"Pommes\",\"zero\":\"Pomme\"}});
EOS
end
end
end

0 comments on commit ad0908b

Please sign in to comment.