i18n-recursive-lookup 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ module I18n
2
+ module RecursiveLookup
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,72 @@
1
+ module I18n
2
+ module Backend
3
+ module RecursiveLookup
4
+
5
+ # ${} for embedding, $${} for escaping
6
+ TOKENIZER = /(\$\$\{[^\}]+\}|\$\{[^\}]+\})/
7
+ INTERPOLATION_SYNTAX_PATTERN = /(\$)?(\$\{([^\}]+)\})/
8
+
9
+ protected
10
+
11
+ def lookup(locale, key, scope = [], options = {})
12
+
13
+ result = super
14
+
15
+ return result unless (result.is_a?(String) or result.is_a?(Hash))
16
+
17
+ compiled_result, had_to_compile_result = deep_compile(locale, result, options)
18
+
19
+ if had_to_compile_result
20
+ cache_compiled_result(locale, key, compiled_result, scope, options)
21
+ end
22
+
23
+ compiled_result
24
+ end
25
+
26
+ #subject is hash or string
27
+ def deep_compile(locale, subject, options)
28
+ if subject.is_a?(String)
29
+ compile(locale, subject, options)
30
+ else
31
+ subject.each do |key, object|
32
+ subject[key], had_to_compile_result = deep_compile(locale, object, options)
33
+ end
34
+ end
35
+ end
36
+
37
+ def compile(locale, string, options)
38
+ had_to_compile_result = false
39
+
40
+ result = string.split(TOKENIZER).map do |token|
41
+ embedded_token = token.match(INTERPOLATION_SYNTAX_PATTERN)
42
+
43
+ if embedded_token
44
+ had_to_compile_result = true
45
+ handle_interpolation_match(locale, embedded_token, options)
46
+ else
47
+ token
48
+ end
49
+ end.join
50
+
51
+ [result, had_to_compile_result]
52
+ end
53
+
54
+ def cache_compiled_result(locale, dot_form_key, compiled_result, scope, options)
55
+ keys = I18n.normalize_keys(locale, dot_form_key, scope, options[:separator])
56
+
57
+ translation_hash = {}
58
+ # ignore prepended locale key
59
+ keys[1..-1].inject(translation_hash) do |hash, key|
60
+ key == keys[-1] ? hash[key] = compiled_result : hash[key] = {}
61
+ end
62
+
63
+ store_translations(locale, translation_hash, options)
64
+ end
65
+
66
+ def handle_interpolation_match(locale, embedded_token, options)
67
+ escaped, pattern, key = embedded_token.values_at(1, 2, 3)
68
+ escaped ? pattern : I18n.translate(key, locale: locale)
69
+ end
70
+ end
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-recursive-lookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Octavian Neamtu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: test_declarative
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! 'Provides a backend to the i18n gem to allow a definition to contain
63
+ embedded references to other definitions by introducing the special embedded notation
64
+ ${}. E.g. {foo: ''bar'', baz: ${foo}} will evaluate t(:baz) to ''bar''.'
65
+ email:
66
+ - oneamtu89@gmail.com
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - lib/i18n-recursive-lookup/version.rb
72
+ - lib/i18n/backend/recursive_lookup.rb
73
+ homepage: https://github.com//i18n-recursive-lookup
74
+ licenses: []
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project: ! '[none]'
93
+ rubygems_version: 1.8.24
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Provides a backend to the i18n gem to allow translation definitions to reference
97
+ other definitions
98
+ test_files: []