isomorfeus-i18n 1.0.0.zeta23 → 2.0.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,71 +1,72 @@
1
- require 'isomorfeus-transport'
2
-
3
- if RUBY_ENGINE == 'opal'
4
- require 'isomorfeus/i18n/config'
5
- require 'isomorfeus/i18n/reducer'
6
- Isomorfeus::I18n::Reducer.add_reducer_to_store
7
- require 'lucid_translation/mixin'
8
- require 'isomorfeus/i18n/init'
9
- if Isomorfeus.on_browser?
10
- Isomorfeus.add_client_init_after_store_class_name('Isomorfeus::I18n::Init')
11
- else
12
- Isomorfeus.add_transport_init_class_name('Isomorfeus::I18n::Init')
13
- end
14
- else
15
- require 'active_support'
16
- require 'oj'
17
- require 'fast_gettext'
18
- require 'http_accept_language/parser'
19
- require 'http_accept_language/middleware'
20
- require 'isomorfeus-data'
21
- require 'isomorfeus/i18n/config'
22
- require 'isomorfeus/i18n/init'
23
- require 'lucid_translation/mixin'
24
- require 'isomorfeus/i18n/handler/locale_handler'
25
-
26
- Isomorfeus.add_middleware(HttpAcceptLanguage::Middleware)
27
-
28
- Opal.append_path(__dir__.untaint) unless Opal.paths.include?(__dir__.untaint)
29
-
30
- Isomorfeus.locale_path = File.expand_path(File.join('app', 'locales'))
31
-
32
- # identify available locales
33
- locales = []
34
-
35
- Dir.glob("#{Isomorfeus.locale_path}/**/*.mo").each do |file|
36
- locales << File.basename(file, '.mo')
37
- end
38
- Isomorfeus.i18n_type = :mo unless locales.empty?
39
-
40
- unless Isomorfeus.i18n_type
41
- locales = []
42
- Dir.glob("#{Isomorfeus.locale_path}/**/*.po").each do |file|
43
- locales << File.basename(file, '.po')
44
- end
45
- Isomorfeus.i18n_type = :po unless locales.empty?
46
- end
47
-
48
- unless Isomorfeus.i18n_type
49
- locales = []
50
- Dir.glob("#{Isomorfeus.locale_path}/**/*.yaml").each do |file|
51
- locales << File.basename(file, '.yaml')
52
- end
53
- Dir.glob("#{Isomorfeus.locale_path}/**/*.yml").each do |file|
54
- locales << File.basename(file, '.yml')
55
- end
56
- Isomorfeus.i18n_type = :yaml unless locales.empty?
57
- end
58
-
59
- Isomorfeus.available_locales = locales
60
- Isomorfeus.available_locales = ['en'] if Isomorfeus.available_locales.empty?
61
-
62
- if Isomorfeus.available_locales.include?('en')
63
- Isomorfeus.locale = 'en'
64
- else
65
- Isomorfeus.locale = Isomorfeus.available_locales.first
66
- end
67
-
68
- Isomorfeus.i18n_domain = 'app'
69
-
70
- Isomorfeus::I18n::Init.init
71
- end
1
+ require 'isomorfeus-transport'
2
+
3
+ if RUBY_ENGINE == 'opal'
4
+ require 'isomorfeus/i18n/config'
5
+ require 'isomorfeus/i18n/reducer'
6
+ Isomorfeus::I18n::Reducer.add_reducer_to_store
7
+ require 'lucid_translation/mixin'
8
+ require 'isomorfeus/i18n/init'
9
+ if Isomorfeus.on_browser?
10
+ Isomorfeus.add_client_init_after_store_class_name('Isomorfeus::I18n::Init')
11
+ else
12
+ Isomorfeus.add_transport_init_class_name('Isomorfeus::I18n::Init')
13
+ end
14
+ else
15
+ require 'active_support'
16
+ require 'oj'
17
+ require 'fast_gettext'
18
+ require 'http_accept_language/parser'
19
+ require 'http_accept_language/middleware'
20
+ require 'isomorfeus-data'
21
+ require 'isomorfeus/i18n/config'
22
+ require 'isomorfeus/i18n/init'
23
+ require 'lucid_translation/mixin'
24
+ require 'isomorfeus/i18n/handler/locale_handler'
25
+
26
+ Isomorfeus.add_middleware(HttpAcceptLanguage::Middleware)
27
+
28
+ require 'iso_opal'
29
+ Opal.append_path(__dir__.untaint) unless IsoOpal.paths.include?(__dir__.untaint)
30
+
31
+ Isomorfeus.locale_path = File.expand_path(File.join('app', 'locales'))
32
+
33
+ # identify available locales
34
+ locales = []
35
+
36
+ Dir.glob("#{Isomorfeus.locale_path}/**/*.mo").each do |file|
37
+ locales << File.basename(file, '.mo')
38
+ end
39
+ Isomorfeus.i18n_type = :mo unless locales.empty?
40
+
41
+ unless Isomorfeus.i18n_type
42
+ locales = []
43
+ Dir.glob("#{Isomorfeus.locale_path}/**/*.po").each do |file|
44
+ locales << File.basename(file, '.po')
45
+ end
46
+ Isomorfeus.i18n_type = :po unless locales.empty?
47
+ end
48
+
49
+ unless Isomorfeus.i18n_type
50
+ locales = []
51
+ Dir.glob("#{Isomorfeus.locale_path}/**/*.yaml").each do |file|
52
+ locales << File.basename(file, '.yaml')
53
+ end
54
+ Dir.glob("#{Isomorfeus.locale_path}/**/*.yml").each do |file|
55
+ locales << File.basename(file, '.yml')
56
+ end
57
+ Isomorfeus.i18n_type = :yaml unless locales.empty?
58
+ end
59
+
60
+ Isomorfeus.available_locales = locales
61
+ Isomorfeus.available_locales = ['en'] if Isomorfeus.available_locales.empty?
62
+
63
+ if Isomorfeus.available_locales.include?('en')
64
+ Isomorfeus.locale = 'en'
65
+ else
66
+ Isomorfeus.locale = Isomorfeus.available_locales.first
67
+ end
68
+
69
+ Isomorfeus.i18n_domain = 'app'
70
+
71
+ Isomorfeus::I18n::Init.init
72
+ end
@@ -1,200 +1,194 @@
1
- module LucidTranslation
2
- module Mixin
3
- CONTEXT_SEPARATOR = "\004"
4
- NAMESPACE_SEPARATOR = '|'
5
- NIL_BLOCK = -> { nil }
6
- TRANSLATION_METHODS = [:_, :n_, :np_, :ns_, :p_, :s_]
7
-
8
- if RUBY_ENGINE != 'opal'
9
- class InternalTranslationProxy
10
- extend FastGettext::Translation
11
- extend FastGettext::TranslationMultidomain
12
- end
13
- end
14
-
15
- if RUBY_ENGINE == 'opal'
16
- def _(*keys, &block)
17
- domain = Isomorfeus.i18n_domain
18
- locale = Isomorfeus.locale
19
- Isomorfeus.raise_error(message: "I18n _(): no key given!") if keys.empty?
20
- result = Redux.fetch_by_path(:i18n_state, domain, locale, '_', keys)
21
- return result if result
22
- if Isomorfeus::I18n::Init.initialized?
23
- Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, '_', keys).then do |agent|
24
- if agent.processed
25
- agent.result
26
- else
27
- agent.processed = true
28
- if agent.response.key?(:error)
29
- `console.error(#{agent.response[:error].to_n})`
30
- Isomorfeus.raise_error(message: agent.response[:error])
31
- end
32
- Isomorfeus.store.merge_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
33
- end
34
- end
35
- end
36
- block_given? ? block.call : keys.first
37
- end
38
-
39
- def n_(*keys, count, &block)
40
- domain = Isomorfeus.i18n_domain
41
- locale = Isomorfeus.locale
42
- Isomorfeus.raise_error(message: "I18n n_(): no key given!") if keys.empty?
43
- result = Redux.fetch_by_path(:i18n_state, domain, locale, 'n_', keys + [count])
44
- return result if result
45
- if Isomorfeus::I18n::Init.initialized?
46
- Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, 'n_', keys + [count]).then do |agent|
47
- if agent.processed
48
- agent.result
49
- else
50
- agent.processed = true
51
- if agent.response.key?(:error)
52
- `console.error(#{agent.response[:error].to_n})`
53
- Isomorfeus.raise_error(message: agent.response[:error])
54
- end
55
- Isomorfeus.store.merge_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
56
- end
57
- end
58
- end
59
- block_given? ? block.call : keys.last
60
- end
61
-
62
- def np_(context, plural_one, *args, separator: nil, &block)
63
- nargs = ["#{context}#{separator || CONTEXT_SEPARATOR}#{plural_one}"] + args
64
- translation = n_(*nargs, &NIL_BLOCK)
65
- return translation if translation
66
-
67
- block_given? ? block.call : n_(plural_one, *args)
68
- end
69
-
70
- def ns_(*args, &block)
71
- domain = Isomorfeus.i18n_domain
72
- locale = Isomorfeus.locale
73
- Isomorfeus.raise_error(message: "I18n ns_(): no args given!") if args.empty?
74
- result = Redux.fetch_by_path(:i18n_state, domain, locale, 'ns_', args)
75
- return result if result
76
- if Isomorfeus::I18n::Init.initialized?
77
- Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, 'ns_', args).then do |agent|
78
- if agent.processed
79
- agent.result
80
- else
81
- agent.processed = true
82
- if agent.response.key?(:error)
83
- `console.error(#{agent.response[:error].to_n})`
84
- Isomorfeus.raise_error(message: agent.response[:error])
85
- end
86
- Isomorfeus.store.merge_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
87
- end
88
- end
89
- end
90
- block_given? ? block.call : n_(*args).split(NAMESPACE_SEPARATOR).last
91
- end
92
-
93
- def p_(namespace, key, separator = nil, &block)
94
- domain = Isomorfeus.i18n_domain
95
- locale = Isomorfeus.locale
96
- args = separator ? [namespace, key, separator] : [namespace, key]
97
- result = Redux.fetch_by_path(:i18n_state, domain, locale, 'p_', args)
98
- return result if result
99
- if Isomorfeus::I18n::Init.initialized?
100
- Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, 'p_', args).then do |agent|
101
- if agent.processed
102
- agent.result
103
- else
104
- agent.processed = true
105
- if agent.response.key?(:error)
106
- `console.error(#{agent.response[:error].to_n})`
107
- Isomorfeus.raise_error(message: agent.response[:error])
108
- end
109
- Isomorfeus.store.merge_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
110
- end
111
- end
112
- end
113
- block_given? ? block.call : key
114
- end
115
-
116
- def s_(key, separator = nil, &block)
117
- domain = Isomorfeus.i18n_domain
118
- locale = Isomorfeus.locale
119
- args = separator ? [key, separator] : [key]
120
- result = Redux.fetch_by_path(:i18n_state, domain, locale, 's_', args)
121
- return result if result
122
- if Isomorfeus::I18n::Init.initialized?
123
- Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, 's_', args).then do |agent|
124
- if agent.processed
125
- agent.result
126
- else
127
- agent.processed = true
128
- if agent.response.key?(:error)
129
- `console.error(#{agent.response[:error].to_n})`
130
- Isomorfeus.raise_error(message: agent.response[:error])
131
- end
132
- Isomorfeus.store.merge_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
133
- end
134
- end
135
- end
136
- block_given? ? block.call : key.split(separator || NAMESPACE_SEPARATOR).last
137
- end
138
-
139
- def N_(translate)
140
- translate
141
- end
142
-
143
- def Nn_(*keys)
144
- keys
145
- end
146
-
147
- TRANSLATION_METHODS.each do |method|
148
- define_method("d#{method}") do |domain, *args, &block|
149
- old_domain = Isomorfeus.i18n_domain
150
- begin
151
- Isomorfeus.i18n_domain = domain
152
- send(method, *args, &block)
153
- ensure
154
- Isomorfeus.i18n_domain = old_domain
155
- end
156
- end
157
-
158
- define_method("D#{method}") do |*args, &block|
159
- domain = Isomorfeus.i18n_domain
160
- locale = Isomorfeus.locale
161
- Isomorfeus.raise_error(message: "I18n D#{method}(): no args given!") if args.empty?
162
- result = Redux.fetch_by_path(:i18n_state, domain, locale, "D#{method}", args)
163
- return result if result
164
- if Isomorfeus::I18n::Init.initialized?
165
- Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, "D#{method}", args).then do |agent|
166
- if agent.processed
167
- agent.result
168
- else
169
- agent.processed = true
170
- if agent.response.key?(:error)
171
- `console.error(#{agent.response[:error].to_n})`
172
- Isomorfeus.raise_error(message: agent.response[:error])
173
- end
174
- Isomorfeus.store.merge_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
175
- end
176
- end
177
- end
178
- block_given? ? block.call : send(method, *args, &block)
179
- end
180
- end
181
- else
182
- TRANSLATION_METHODS.each do |method|
183
- define_method(method) do |domain, *args, &block|
184
- Isomorfeus::I18n::Init.init unless Thread.current[:isomorfeus_i18n_initialized] == true
185
- InternalTranslationProxy.send(method, domain, *args, &block)
186
- end
187
-
188
- define_method("d#{method}") do |domain, *args, &block|
189
- Isomorfeus::I18n::Init.init unless Thread.current[:isomorfeus_i18n_initialized] == true
190
- InternalTranslationProxy.send("d#{method}", domain, *args, &block)
191
- end
192
-
193
- define_method("D#{method}") do |*args, &block|
194
- Isomorfeus::I18n::Init.init unless Thread.current[:isomorfeus_i18n_initialized] == true
195
- InternalTranslationProxy.send("D#{method}", *args, &block)
196
- end
197
- end
198
- end
199
- end
200
- end
1
+ module LucidTranslation
2
+ module Mixin
3
+ CONTEXT_SEPARATOR = "\004"
4
+ NAMESPACE_SEPARATOR = '|'
5
+ NIL_BLOCK = -> { nil }
6
+ TRANSLATION_METHODS = [:_, :n_, :np_, :ns_, :p_, :s_]
7
+
8
+ if RUBY_ENGINE != 'opal'
9
+ class InternalTranslationProxy
10
+ extend FastGettext::Translation
11
+ extend FastGettext::TranslationMultidomain
12
+ end
13
+ end
14
+
15
+ if RUBY_ENGINE == 'opal'
16
+ def _(*keys, &block)
17
+ domain = Isomorfeus.i18n_domain
18
+ locale = Isomorfeus.locale
19
+ Isomorfeus.raise_error(message: "I18n _(): no key given!") if keys.empty?
20
+ result = Redux.fetch_by_path(:i18n_state, domain, locale, '_', keys)
21
+ return result if result
22
+ if Isomorfeus::I18n::Init.initialized?
23
+ Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, '_', keys).then do |agent|
24
+ if agent.processed
25
+ agent.result
26
+ else
27
+ agent.processed = true
28
+ if agent.response.key?(:error)
29
+ Isomorfeus.raise_error(message: agent.response[:error])
30
+ end
31
+ Isomorfeus.store.collect_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
32
+ end
33
+ end
34
+ end
35
+ block_given? ? block.call : keys.first
36
+ end
37
+
38
+ def n_(*keys, count, &block)
39
+ domain = Isomorfeus.i18n_domain
40
+ locale = Isomorfeus.locale
41
+ Isomorfeus.raise_error(message: "I18n n_(): no key given!") if keys.empty?
42
+ result = Redux.fetch_by_path(:i18n_state, domain, locale, 'n_', keys + [count])
43
+ return result if result
44
+ if Isomorfeus::I18n::Init.initialized?
45
+ Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, 'n_', keys + [count]).then do |agent|
46
+ if agent.processed
47
+ agent.result
48
+ else
49
+ agent.processed = true
50
+ if agent.response.key?(:error)
51
+ Isomorfeus.raise_error(message: agent.response[:error])
52
+ end
53
+ Isomorfeus.store.collect_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
54
+ end
55
+ end
56
+ end
57
+ block_given? ? block.call : keys.last
58
+ end
59
+
60
+ def np_(context, plural_one, *args, separator: nil, &block)
61
+ nargs = ["#{context}#{separator || CONTEXT_SEPARATOR}#{plural_one}"] + args
62
+ translation = n_(*nargs, &NIL_BLOCK)
63
+ return translation if translation
64
+
65
+ block_given? ? block.call : n_(plural_one, *args)
66
+ end
67
+
68
+ def ns_(*args, &block)
69
+ domain = Isomorfeus.i18n_domain
70
+ locale = Isomorfeus.locale
71
+ Isomorfeus.raise_error(message: "I18n ns_(): no args given!") if args.empty?
72
+ result = Redux.fetch_by_path(:i18n_state, domain, locale, 'ns_', args)
73
+ return result if result
74
+ if Isomorfeus::I18n::Init.initialized?
75
+ Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, 'ns_', args).then do |agent|
76
+ if agent.processed
77
+ agent.result
78
+ else
79
+ agent.processed = true
80
+ if agent.response.key?(:error)
81
+ Isomorfeus.raise_error(message: agent.response[:error])
82
+ end
83
+ Isomorfeus.store.collect_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
84
+ end
85
+ end
86
+ end
87
+ block_given? ? block.call : n_(*args).split(NAMESPACE_SEPARATOR).last
88
+ end
89
+
90
+ def p_(namespace, key, separator = nil, &block)
91
+ domain = Isomorfeus.i18n_domain
92
+ locale = Isomorfeus.locale
93
+ args = separator ? [namespace, key, separator] : [namespace, key]
94
+ result = Redux.fetch_by_path(:i18n_state, domain, locale, 'p_', args)
95
+ return result if result
96
+ if Isomorfeus::I18n::Init.initialized?
97
+ Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, 'p_', args).then do |agent|
98
+ if agent.processed
99
+ agent.result
100
+ else
101
+ agent.processed = true
102
+ if agent.response.key?(:error)
103
+ Isomorfeus.raise_error(message: agent.response[:error])
104
+ end
105
+ Isomorfeus.store.collect_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
106
+ end
107
+ end
108
+ end
109
+ block_given? ? block.call : key
110
+ end
111
+
112
+ def s_(key, separator = nil, &block)
113
+ domain = Isomorfeus.i18n_domain
114
+ locale = Isomorfeus.locale
115
+ args = separator ? [key, separator] : [key]
116
+ result = Redux.fetch_by_path(:i18n_state, domain, locale, 's_', args)
117
+ return result if result
118
+ if Isomorfeus::I18n::Init.initialized?
119
+ Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, 's_', args).then do |agent|
120
+ if agent.processed
121
+ agent.result
122
+ else
123
+ agent.processed = true
124
+ if agent.response.key?(:error)
125
+ Isomorfeus.raise_error(message: agent.response[:error])
126
+ end
127
+ Isomorfeus.store.collect_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
128
+ end
129
+ end
130
+ end
131
+ block_given? ? block.call : key.split(separator || NAMESPACE_SEPARATOR).last
132
+ end
133
+
134
+ def N_(translate)
135
+ translate
136
+ end
137
+
138
+ def Nn_(*keys)
139
+ keys
140
+ end
141
+
142
+ TRANSLATION_METHODS.each do |method|
143
+ define_method("d#{method}") do |domain, *args, &block|
144
+ old_domain = Isomorfeus.i18n_domain
145
+ begin
146
+ Isomorfeus.i18n_domain = domain
147
+ send(method, *args, &block)
148
+ ensure
149
+ Isomorfeus.i18n_domain = old_domain
150
+ end
151
+ end
152
+
153
+ define_method("D#{method}") do |*args, &block|
154
+ domain = Isomorfeus.i18n_domain
155
+ locale = Isomorfeus.locale
156
+ Isomorfeus.raise_error(message: "I18n D#{method}(): no args given!") if args.empty?
157
+ result = Redux.fetch_by_path(:i18n_state, domain, locale, "D#{method}", args)
158
+ return result if result
159
+ if Isomorfeus::I18n::Init.initialized?
160
+ Isomorfeus::Transport.promise_send_path('Isomorfeus::I18n::Handler::LocaleHandler', domain, locale, "D#{method}", args).then do |agent|
161
+ if agent.processed
162
+ agent.result
163
+ else
164
+ agent.processed = true
165
+ if agent.response.key?(:error)
166
+ Isomorfeus.raise_error(message: agent.response[:error])
167
+ end
168
+ Isomorfeus.store.collect_and_defer_dispatch(type: 'I18N_LOAD', data: { domain => agent.response[domain] })
169
+ end
170
+ end
171
+ end
172
+ block_given? ? block.call : send(method, *args, &block)
173
+ end
174
+ end
175
+ else
176
+ TRANSLATION_METHODS.each do |method|
177
+ define_method(method) do |domain, *args, &block|
178
+ Isomorfeus::I18n::Init.init unless Thread.current[:isomorfeus_i18n_initialized] == true
179
+ InternalTranslationProxy.send(method, domain, *args, &block)
180
+ end
181
+
182
+ define_method("d#{method}") do |domain, *args, &block|
183
+ Isomorfeus::I18n::Init.init unless Thread.current[:isomorfeus_i18n_initialized] == true
184
+ InternalTranslationProxy.send("d#{method}", domain, *args, &block)
185
+ end
186
+
187
+ define_method("D#{method}") do |*args, &block|
188
+ Isomorfeus::I18n::Init.init unless Thread.current[:isomorfeus_i18n_initialized] == true
189
+ InternalTranslationProxy.send("D#{method}", *args, &block)
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end