Skip to content

Commit 5b75bfb

Browse files
author
Sven Fuchs
committed
allow to configure the default scope separator, allow to pass a custom scope separator (e.g. I18n.t(:'foo|bar', :separator => '|')
1 parent e277711 commit 5b75bfb

File tree

6 files changed

+58
-22
lines changed

6 files changed

+58
-22
lines changed

Diff for: lib/i18n.rb

+14-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module I18n
1212
@@backend = nil
1313
@@load_path = nil
1414
@@default_locale = :'en'
15+
@@default_separator = '.'
1516
@@exception_handler = :default_exception_handler
1617

1718
class << self
@@ -50,6 +51,16 @@ def available_locales
5051
backend.available_locales
5152
end
5253

54+
# Returns the current default scope separator. Defaults to '.'
55+
def default_separator
56+
@@default_separator
57+
end
58+
59+
# Sets the current default scope separator.
60+
def default_separator=(separator)
61+
@@default_separator = separator
62+
end
63+
5364
# Sets the exception handler.
5465
def exception_handler=(exception_handler)
5566
@@exception_handler = exception_handler
@@ -190,9 +201,9 @@ def default_exception_handler(exception, locale, key, options)
190201
# Merges the given locale, key and scope into a single array of keys.
191202
# Splits keys that contain dots into multiple keys. Makes sure all
192203
# keys are Symbols.
193-
def normalize_translation_keys(locale, key, scope)
194-
keys = [locale] + Array(scope) + [key]
195-
keys = keys.map { |k| k.to_s.split(/\./) }
204+
def normalize_translation_keys(locale, key, scope, separator = nil)
205+
keys = [locale] + Array(scope) + Array(key)
206+
keys = keys.map { |k| k.to_s.split(separator || I18n.default_separator) }
196207
keys.flatten.map { |k| k.to_sym }
197208
end
198209
end

Diff for: lib/i18n/backend/simple.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module I18n
44
module Backend
55
class Simple
6-
RESERVED_KEYS = [:scope, :default]
6+
RESERVED_KEYS = [:scope, :default, :separator]
77
MATCH = /(\\\\)?\{\{([^\}]+)\}\}/
88

99
# Accepts a list of paths to translation files. Loads translations from
@@ -25,10 +25,10 @@ def translate(locale, key, options = {})
2525
raise InvalidLocale.new(locale) if locale.nil?
2626
return key.map { |k| translate(locale, k, options) } if key.is_a?(Array)
2727

28-
count, scope, default = options.values_at(:count, *RESERVED_KEYS)
28+
count, scope, default, separator = options.values_at(:count, *RESERVED_KEYS)
2929
values = options.reject { |name, value| RESERVED_KEYS.include?(name) }
3030

31-
entry = lookup(locale, key, scope)
31+
entry = lookup(locale, key, scope, separator)
3232
entry = entry.nil? ? default(locale, key, default, options) : resolve(locale, key, entry, options)
3333

3434
raise(I18n::MissingTranslationData.new(locale, key, options)) if entry.nil?
@@ -92,10 +92,10 @@ def translations
9292
# nested translations hash. Splits keys or scopes containing dots
9393
# into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
9494
# <tt>%w(currency format)</tt>.
95-
def lookup(locale, key, scope = [])
95+
def lookup(locale, key, scope = [], separator = nil)
9696
return unless key
9797
init_translations unless initialized?
98-
keys = I18n.send(:normalize_translation_keys, locale, key, scope)
98+
keys = I18n.send(:normalize_translation_keys, locale, key, scope, separator)
9999
keys.inject(translations) do |result, k|
100100
if (x = result[k.to_sym]).nil?
101101
return nil

Diff for: test/backend/simple/lookup_test.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ class I18nSimpleBackendLookupTest < Test::Unit::TestCase
66

77
# useful because this way we can use the backend with no key for interpolation/pluralization
88
def test_lookup_given_nil_as_a_key_returns_nil
9-
assert_nil @backend.send(:lookup, 'en', nil)
9+
assert_nil @backend.send(:lookup, :en, nil)
1010
end
1111

1212
def test_lookup_given_nested_keys_looks_up_a_nested_hash_value
13-
assert_equal 'bar', @backend.send(:lookup, 'en', :bar, [:foo])
13+
assert_equal 'bar', @backend.send(:lookup, :en, :bar, [:foo])
14+
end
15+
16+
def test_lookup_using_a_custom_separator
17+
assert_equal 'bar', @backend.send(:lookup, :en, 'foo|bar', [], '|')
18+
end
19+
20+
def test_default_using_a_custom_separator
21+
assert_equal 'bar', @backend.send(:default, :en, :'does_not_exist', :'foo|bar', :separator => '|')
1422
end
1523
end

Diff for: test/backend/simple/pluralize_test.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@ def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluraliz
3333
assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
3434
end
3535

36-
# def test_interpolate_given_a_string_raises_invalid_pluralization_data
37-
# assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
38-
# end
39-
#
40-
# def test_interpolate_given_an_array_raises_invalid_pluralization_data
41-
# assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
42-
# end
36+
def test_pluralize_given_pluralization_data_as_default
37+
assert_equal 'bars', I18n.t(:bar, :count => 2, :default => { :one => 'bar', :other => 'bars' })
38+
end
4339
end
4440

Diff for: test/backend/simple/translate_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
55
include I18nSimpleBackendTestSetup
66

77
def test_translate_calls_lookup_with_locale_given
8-
@backend.expects(:lookup).with('de', :bar, [:foo]).returns 'bar'
8+
@backend.expects(:lookup).with('de', :bar, [:foo], nil).returns 'bar'
99
@backend.translate 'de', :bar, :scope => [:foo]
1010
end
1111

Diff for: test/i18n_test.rb

+25-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_uses_simple_backend_set_by_default
2323
end
2424

2525
def test_can_set_backend
26-
assert_nothing_raised{ I18n.backend = self }
26+
assert_nothing_raised { I18n.backend = self }
2727
assert_equal self, I18n.backend
2828
I18n.backend = I18n::Backend::Simple.new
2929
end
@@ -33,7 +33,7 @@ def test_uses_en_us_as_default_locale_by_default
3333
end
3434

3535
def test_can_set_default_locale
36-
assert_nothing_raised{ I18n.default_locale = 'de' }
36+
assert_nothing_raised { I18n.default_locale = 'de' }
3737
assert_equal 'de', I18n.default_locale
3838
I18n.default_locale = 'en'
3939
end
@@ -43,14 +43,35 @@ def test_uses_default_locale_as_locale_by_default
4343
end
4444

4545
def test_can_set_locale_to_thread_current
46-
assert_nothing_raised{ I18n.locale = 'de' }
46+
assert_nothing_raised { I18n.locale = 'de' }
4747
assert_equal 'de', I18n.locale
4848
assert_equal 'de', Thread.current[:locale]
4949
I18n.locale = 'en'
5050
end
5151

52+
def test_defaults_to_dot_as_separator
53+
assert_equal '.', I18n.default_separator
54+
end
55+
56+
def test_can_set_default_separator
57+
assert_nothing_raised { I18n.default_separator = "\001" }
58+
I18n.default_separator = '.' # revert it
59+
end
60+
61+
def test_normalize_keys
62+
assert_equal [:en, :foo, :bar], I18n.send(:normalize_translation_keys, :en, :bar, :foo)
63+
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, :'baz.buz', :'foo.bar')
64+
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, 'baz.buz', 'foo.bar')
65+
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, %w(baz buz), %w(foo bar))
66+
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, [:baz, :buz], [:foo, :bar])
67+
end
68+
69+
def test_uses_passed_separator_to_normalize_keys
70+
assert_equal [:en, :foo, :bar, :baz, :buz], I18n.send(:normalize_translation_keys, :en, :'baz|buz', :'foo|bar', '|')
71+
end
72+
5273
def test_can_set_exception_handler
53-
assert_nothing_raised{ I18n.exception_handler = :custom_exception_handler }
74+
assert_nothing_raised { I18n.exception_handler = :custom_exception_handler }
5475
I18n.exception_handler = :default_exception_handler # revert it
5576
end
5677

0 commit comments

Comments
 (0)