Module: I18n::JS
- Defined in:
- lib/i18n/js.rb,
lib/i18n/js/utils.rb,
lib/i18n/js/engine.rb,
lib/i18n/js/segment.rb,
lib/i18n/js/version.rb,
lib/i18n/js/middleware.rb,
lib/i18n/js/dependencies.rb,
lib/i18n/js/formatters/js.rb,
lib/i18n/js/formatters/base.rb,
lib/i18n/js/formatters/json.rb,
lib/i18n/js/fallback_locales.rb,
lib/i18n/js/private/hash_with_symbol_keys.rb
Defined Under Namespace
Modules: Dependencies, Formatters, Private, Utils
Classes: Engine, FallbackLocales, Middleware, Segment, SprocketsExtension
Constant Summary
collapse
- DEFAULT_CONFIG_PATH =
"config/i18n-js.yml"
- DEFAULT_EXPORT_DIR_PATH =
"public/javascripts"
- VERSION =
"3.5.0"
Class Method Summary
collapse
Class Method Details
.backend ⇒ Object
Allow using a different backend than the one globally configured
32
33
34
|
# File 'lib/i18n/js.rb', line 32
def self.backend
@backend ||= I18n.backend
end
|
.backend=(alternative_backend) ⇒ Object
36
37
38
|
# File 'lib/i18n/js.rb', line 36
def self.backend=(alternative_backend)
@backend = alternative_backend
end
|
.config ⇒ Object
Load configuration file for partial exporting and custom output directory
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/i18n/js.rb', line 110
def self.config
if config_file_exists?
erb_result_from_yaml_file = ERB.new(File.read(config_file_path)).result
Private::HashWithSymbolKeys.new(
(::YAML.load(erb_result_from_yaml_file) || {})
)
else
Private::HashWithSymbolKeys.new({})
end.freeze
end
|
.config_file_exists? ⇒ Boolean
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if configuration file exist
123
124
125
|
# File 'lib/i18n/js.rb', line 123
def self.config_file_exists?
File.file? config_file_path
end
|
.config_file_path ⇒ Object
The configuration file. This defaults to the ‘config/i18n-js.yml` file.
23
24
25
|
# File 'lib/i18n/js.rb', line 23
def self.config_file_path
@config_file_path ||= DEFAULT_CONFIG_PATH
end
|
.config_file_path=(new_path) ⇒ Object
27
28
29
|
# File 'lib/i18n/js.rb', line 27
def self.config_file_path=(new_path)
@config_file_path = new_path
end
|
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/i18n/js.rb', line 56
def self.configured_segments
config[:translations].inject([]) do |segments, options_hash|
options_hash_with_symbol_keys = Private::HashWithSymbolKeys.new(options_hash)
file = options_hash_with_symbol_keys[:file]
only = options_hash_with_symbol_keys[:only] || '*'
exceptions = [options_hash_with_symbol_keys[:except] || []].flatten
result = segment_for_scope(only, exceptions)
merge_with_fallbacks!(result) if fallbacks
unless result.empty?
segments << Segment.new(
file,
result,
(options_hash_with_symbol_keys),
)
end
segments
end
end
|
.exclude(translations, exceptions) ⇒ Object
Exclude keys from translations listed in the ‘except:` section in the config file
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/i18n/js.rb', line 141
def self.exclude(translations, exceptions)
return translations if exceptions.empty?
exceptions.inject(translations) do |memo, exception|
exception_scopes = exception.to_s.split(".")
Utils.deep_reject(memo) do |key, value, scopes|
Utils.scopes_match?(scopes, exception_scopes)
end
end
end
|
.export ⇒ Object
Export translations to JavaScript, considering settings from configuration file
42
43
44
45
46
|
# File 'lib/i18n/js.rb', line 42
def self.export
export_i18n_js
translation_segments.each(&:save!)
end
|
221
222
223
224
225
226
227
228
|
# File 'lib/i18n/js.rb', line 221
def self.(options)
segment_options = Private::HashWithSymbolKeys.new({
js_extend: js_extend,
sort_translation_keys: sort_translation_keys?,
json_only: json_only
}).freeze
segment_options.merge(options.slice(*Segment::OPTIONS))
end
|
.fallbacks ⇒ Object
197
198
199
200
201
202
|
# File 'lib/i18n/js.rb', line 197
def self.fallbacks
config.fetch(:fallbacks) do
true
end
end
|
.filter(translations, scopes) ⇒ Object
Filter translations according to the specified scope.
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/i18n/js.rb', line 153
def self.filter(translations, scopes)
scopes = scopes.split(".") if scopes.is_a?(String)
scopes = scopes.clone
scope = scopes.shift
if scope == "*"
results = {}
translations.each do |scope, translations|
tmp = scopes.empty? ? translations : filter(translations, scopes)
results[scope.to_sym] = tmp unless tmp.nil?
end
return results
elsif translations.respond_to?(:key?) && translations.key?(scope.to_sym)
return {scope.to_sym => scopes.empty? ? translations[scope.to_sym] : filter(translations[scope.to_sym], scopes)}
end
nil
end
|
.filtered_translations ⇒ Object
90
91
92
93
94
95
96
97
98
|
# File 'lib/i18n/js.rb', line 90
def self.filtered_translations
translations = {}.tap do |result|
translation_segments.each do |segment|
Utils.deep_merge!(result, segment.translations)
end
end
return Utils.deep_key_sort(translations) if I18n::JS.sort_translation_keys?
translations
end
|
.js_extend ⇒ Object
204
205
206
207
208
209
|
# File 'lib/i18n/js.rb', line 204
def self.js_extend
config.fetch(:js_extend) do
true
end
end
|
.json_only ⇒ Object
190
191
192
193
194
195
|
# File 'lib/i18n/js.rb', line 190
def self.json_only
config.fetch(:json_only) do
false
end
end
|
.merge_with_fallbacks!(result) ⇒ Object
deep_merge! given result with result for fallback locale
80
81
82
83
84
85
86
87
88
|
# File 'lib/i18n/js.rb', line 80
def self.merge_with_fallbacks!(result)
I18n.available_locales.each do |locale|
fallback_locales = FallbackLocales.new(fallbacks, locale)
fallback_locales.each do |fallback_locale|
result[locale] = Utils.deep_merge(result[fallback_locale] || {}, result[locale] || {})
end
end
end
|
.scoped_translations(scopes, exceptions = []) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/i18n/js.rb', line 127
def self.scoped_translations(scopes, exceptions = []) result = {}
[scopes].flatten.each do |scope|
translations_without_exceptions = exclude(translations, exceptions)
filtered_translations = filter(translations_without_exceptions, scope) || {}
Utils.deep_merge!(result, filtered_translations)
end
result
end
|
.segment_for_scope(scope, exceptions) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/i18n/js.rb', line 48
def self.segment_for_scope(scope, exceptions)
if scope == "*"
exclude(translations, exceptions)
else
scoped_translations(scope, exceptions)
end
end
|
.sort_translation_keys=(value) ⇒ Object
217
218
219
|
# File 'lib/i18n/js.rb', line 217
def self.sort_translation_keys=(value)
@sort_translation_keys = !!value
end
|
.sort_translation_keys? ⇒ Boolean
211
212
213
214
215
|
# File 'lib/i18n/js.rb', line 211
def self.sort_translation_keys?
@sort_translation_keys ||= (config[:sort_translation_keys]) if config.key?(:sort_translation_keys)
@sort_translation_keys = true if @sort_translation_keys.nil?
@sort_translation_keys
end
|
.translation_segments ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/i18n/js.rb', line 100
def self.translation_segments
if config_file_exists? && config[:translations]
configured_segments
else
[Segment.new("#{DEFAULT_EXPORT_DIR_PATH}/translations.js", translations)]
end
end
|
.translations ⇒ Object
Initialize and return translations
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/i18n/js.rb', line 172
def self.translations
self.backend.instance_eval do
init_translations unless initialized?
Private::HashWithSymbolKeys.new(translations).
slice(*::I18n.available_locales).
to_h
end
end
|
.use_fallbacks? ⇒ Boolean
186
187
188
|
# File 'lib/i18n/js.rb', line 186
def self.use_fallbacks?
fallbacks != false
end
|