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.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +22 -22
- data/lib/isomorfeus/i18n/config.rb +78 -78
- data/lib/isomorfeus/i18n/handler/locale_handler.rb +76 -76
- data/lib/isomorfeus/i18n/init.rb +47 -48
- data/lib/isomorfeus/i18n/reducer.rb +38 -31
- data/lib/isomorfeus/i18n/version.rb +4 -4
- data/lib/isomorfeus-i18n.rb +72 -71
- data/lib/lucid_translation/mixin.rb +194 -200
- metadata +43 -43
data/lib/isomorfeus-i18n.rb
CHANGED
@@ -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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
Isomorfeus.available_locales =
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
result
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
agent.
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
agent.
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
agent.
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
agent.
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
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
|