i18n-env-var-lookup 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e8700fc592302d1cf86b7cd42e06267563860ec2368e9067289f139441e68f41
4
+ data.tar.gz: fed7a80eb47cbd3fdb159bceb46aa71258ccb427e5479ef3e58a9a9f9f493596
5
+ SHA512:
6
+ metadata.gz: b12931bdc6001c623985c95687947ba9956bb28fa453bfbb177416f2c9d0dd03a6e845a44af0cd33ec27f088a665f37f39dc679afe0ec864a74c8ea1912f2590
7
+ data.tar.gz: 193bcbe973a354fcb93a5d062299f54ae0e6c84e64c6006688f8d2990c0d959b5f8e71add5e27b240f7e945818a8c965999159138d8fca6353c8641468a41be6
@@ -0,0 +1,102 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+
4
+ module I18n
5
+ module Backend
6
+ module EnvVarLookup
7
+
8
+ # ~{} for embedding, ~{} for escaping
9
+ TOKENIZER = /(\~\~\{[^\}]+\}|\~\{[^\}]+\})/
10
+ INTERPOLATION_SYNTAX_PATTERN = /(\~)?(\~\{([^\}]+)\})/
11
+
12
+ protected
13
+
14
+ def lookup(locale, key, scope = [], options = {})
15
+ result = super
16
+
17
+ unless result
18
+ normalized_key = I18n.normalize_keys(nil, key, scope)
19
+ return if normalized_key.size < 2
20
+
21
+ # simply strip the last key part, e.g.
22
+ # `foo.bar.baz` becomes `foo.bar`
23
+ normalized_key.slice!(-1, 1)
24
+
25
+ # look the stripped key up - if this is a reference, it will
26
+ # get compiled and cached
27
+ lookup(locale, normalized_key, [], options)
28
+
29
+ # then try again to get our result - it will be found now if the
30
+ # previous `lookup` call compiled a new reference with our key,
31
+ # otherwise still stay nil
32
+ result = super
33
+ end
34
+
35
+ return result unless (result.is_a?(String) or result.is_a?(Hash))
36
+
37
+ compiled_result, had_to_compile_result = deep_compile(locale, result, options)
38
+
39
+ if had_to_compile_result
40
+ cache_compiled_result(locale, key, compiled_result, scope, options)
41
+ end
42
+
43
+ compiled_result
44
+ end
45
+
46
+ #subject is hash or string
47
+ def deep_compile(locale, subject, options)
48
+ if subject.is_a?(Hash)
49
+ subject.each do |key, object|
50
+ subject[key], _had_to_compile_result = deep_compile(locale, object, options)
51
+ end
52
+ else
53
+ compile(locale, subject, options)
54
+ end
55
+ end
56
+
57
+ def compile(locale, string, options)
58
+ had_to_compile_result = false
59
+
60
+ if string.is_a?(String)
61
+ result = string.split(TOKENIZER).map do |token|
62
+ embedded_token = token.match(INTERPOLATION_SYNTAX_PATTERN)
63
+
64
+ if embedded_token
65
+ had_to_compile_result = true
66
+ handle_interpolation_match(locale, embedded_token, options)
67
+ else
68
+ token
69
+ end
70
+ end
71
+
72
+ result = (
73
+ result.second && (
74
+ result.second.is_a?(Array) || result.second.is_a?(Hash)
75
+ )
76
+ ) ? result.second : result.join
77
+ else
78
+ result = string
79
+ end
80
+
81
+ [result, had_to_compile_result]
82
+ end
83
+
84
+ def cache_compiled_result(locale, dot_form_key, compiled_result, scope, options)
85
+ keys = I18n.normalize_keys(locale, dot_form_key, scope, options[:separator])
86
+
87
+ translation_hash = {}
88
+ # ignore prepended locale key
89
+ keys[1..-1].inject(translation_hash) do |hash, key|
90
+ key == keys[-1] ? hash[key] = compiled_result : hash[key] = {}
91
+ end
92
+
93
+ store_translations(locale, translation_hash, options)
94
+ end
95
+
96
+ def handle_interpolation_match(locale, embedded_token, options)
97
+ escaped, pattern, key = embedded_token.values_at(1, 2, 3)
98
+ escaped ? pattern : ENV.fetch(key.upcase, pattern)
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,5 @@
1
+ module I18n
2
+ module EnvVarLookup
3
+ VERSION = "0.0.5"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-env-var-lookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Stayman Hou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: test_declarative
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: 'Provides a backend to the i18n gem to allow a definition to contain
98
+ embedded references to environment variables by introducing the special embedded
99
+ notation ~{}. E.g. {title: ~{APP_NAME}} will evaluate t(:title) to the value of
100
+ the $APP_NAME environment variable.'
101
+ email:
102
+ - stayman.hou@gmail.com
103
+ executables: []
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - lib/i18n-env-var-lookup/version.rb
108
+ - lib/i18n/backend/env_var_lookup.rb
109
+ homepage: https://github.com/StaymanHou/i18n-env-var-lookup
110
+ licenses: []
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.1.4
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Provides a backend to the i18n gem to allow translation definitions to reference
131
+ environment variables
132
+ test_files: []