Skip to content

Commit

Permalink
Fix a bug where empty locale files would prevent access to the base d…
Browse files Browse the repository at this point in the history
…ata.
  • Loading branch information
jim committed Mar 5, 2014
1 parent cfa1852 commit 7136b73
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -5,7 +5,8 @@
* Fix the name of Lima (goddamnhippie)
* Add south Sudan Swedish translation (barsoom)
* Add Russian translations of Russian Federation (Envek)
* Fix a regression in the localization of Taiwan from the 1.0 rewrite
* Fix a regression in the localization of Taiwan from the 1.0 rewrite.
* Fix a bug where empty locale files would prevent access to the base data.

### 1.0.0 (April 20, 2013)
* Updated version numbering and pushed 1.0.0pre to v1.0.0.
Expand Down
11 changes: 7 additions & 4 deletions lib/carmen/utils.rb
Expand Up @@ -2,8 +2,11 @@ module Carmen
module Utils
# Merge an array of hashes deeply.
#
# When a conflict occurs and either the old value or the new value don't
# respond_to? :merge, the new value is used.
# When a conflict occurs:
# - if both the old value and the new value respond_to? :merge, they
# are recursively passed to deep_merge_hash and the result used.
# - if either doesn't respond_to? :merge, then the new value is used if
# it is not nil. If the new value is nil, the old value is used.
#
# Returns a meged hash.
def self.deep_hash_merge(hashes)
Expand All @@ -14,7 +17,7 @@ def self.deep_hash_merge(hashes)
if old_value.respond_to?(:merge) && new_value.respond_to?(:merge)
deep_hash_merge([old_value, new_value])
else
new_value
new_value || old_value
end
}
}
Expand All @@ -23,7 +26,7 @@ def self.deep_hash_merge(hashes)
# Merge arrays of hashes using the specified keys.
#
# If two hashes have the same value for a key, they are merged together.
# Otherwise, a new hash is appened to the array.
# Otherwise, a new hash is appended to the array.
#
# Matching arrays uses the keys in the order they are provided.
#
Expand Down
17 changes: 17 additions & 0 deletions spec/carmen/i18n_spec.rb
Expand Up @@ -79,4 +79,21 @@
end
end

describe 'overlaying empty files onto a locale' do
before do
@i18n.append_locale_path(carmen_spec_overlay_locale_path)
@i18n.locale = 'de'
end

after do
@i18n.locale = Carmen::I18n::Simple::DEFAULT_LOCALE
@i18n.reset!
end

it 'still has access to the base locale data' do
@i18n.t('world.eu.official_name').must_equal('Das großartige Staat von Eurasia')
end

end

end
12 changes: 12 additions & 0 deletions spec/carmen/utils_spec.rb
Expand Up @@ -23,5 +23,17 @@

merged.must_equal(expected)
end
end

describe 'Utils.deep_hash_merge' do

it 'merges hashes' do
first = { 'a' => 'old', 'b' => 'old', 'c' => 'old' }
second = { 'a' => nil, 'b' => 'new', 'd' => 'new' }

expected = { 'a' => 'old', 'b' => 'new', 'c' => 'old', 'd' => 'new' }

Carmen::Utils.deep_hash_merge([first, second]).must_equal(expected)
end

end
3 changes: 3 additions & 0 deletions spec_data/overlay/locale/de/world.yml
@@ -0,0 +1,3 @@
---
de:
world:

0 comments on commit 7136b73

Please sign in to comment.