-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubstitutes_i18_backend.rb
50 lines (38 loc) · 1.64 KB
/
substitutes_i18_backend.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'minitest/autorun'
require 'i18n'
class SubstitutesI18Backend < I18n::Backend::Simple
def translate(locale, key, options = {})
translation = super(locale, key, options)
substitute_variables(translation)
end
private
def substitute_variables(translation)
translation.scan(/\@\[\w*\]/).each do |variable|
translation.gsub!(variable, I18n.translate(variable[2..-2]))
end
translation
end
end
class Tests < Minitest::Test
I18n.backend = SubstitutesI18Backend.new
I18n.backend.store_translations('en', simple_key: 'There are no substitutions')
I18n.backend.store_translations('en', key_with_variable: 'I have @[number] substitution')
I18n.backend.store_translations('en', number: 'one')
I18n.backend.store_translations('en', key_with_nested_varibles: 'He said: @[key_with_variable]')
I18n.backend.store_translations('en', key_with_more_variables: 'You can @[action] as many @[subject] as you @[verb]')
I18n.backend.store_translations('en', action: 'substitute')
I18n.backend.store_translations('en', subject: 'keys')
I18n.backend.store_translations('en', verb: 'wish')
def test_translation_no_variables
assert_equal "There are no substitutions", I18n.translate(:simple_key)
end
def test_translation_simple_variables
assert_equal "I have one substitution", I18n.translate(:key_with_variable)
end
def test_translation_nested_variables
assert_equal "He said: I have one substitution", I18n.translate(:key_with_nested_varibles)
end
def test_translation_more_variables
assert_equal "You can substitute as many keys as you wish", I18n.translate(:key_with_more_variables)
end
end