Class: Middleman::CoreExtensions::Internationalization
- Inherits:
-
Extension
- Object
- Extension
- Middleman::CoreExtensions::Internationalization
show all
- Defined in:
- middleman-core/lib/middleman-core/core_extensions/i18n.rb
Defined Under Namespace
Classes: LocalizedPageDescriptor
Constant Summary
Constants included
from Contracts
Contracts::PATH_MATCHER
Instance Attribute Summary
Attributes inherited from Extension
#app, #options
Instance Method Summary
collapse
Methods inherited from Extension
activated_extension, #add_exposed_to_context, #after_build, after_extension_activated, #after_extension_activated, #before_build, #before_configuration, clear_after_extension_callbacks, config, define_setting, expose_to_application, expose_to_config, expose_to_template, global_config, helpers, #manipulate_resource_list, option, #ready, resources
Methods included from Contracts
#Contract
Constructor Details
Returns a new instance of Internationalization.
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'middleman-core/lib/middleman-core/core_extensions/i18n.rb', line 17
def initialize(*)
super
require 'i18n'
options[:locales] = options[:langs] unless options[:langs].nil?
options[:locale_map] = options[:lang_map] unless options[:lang_map].nil?
options[:templates_dir] = nil if options[:templates_dir] == false
::I18n.enforce_available_locales = false
end
|
Instance Method Details
#after_configuration ⇒ Object
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
|
# File 'middleman-core/lib/middleman-core/core_extensions/i18n.rb', line 33
def after_configuration
unless options[:no_fallbacks]
require 'i18n/backend/fallbacks'
::I18n::Backend::Simple.include ::I18n::Backend::Fallbacks
end
locales_file_path = options[:data]
app.files.watch :locales,
path: File.join(app.root, locales_file_path),
only: /.*(rb|yml|yaml)$/
app.files.on_change(:locales, &method(:on_file_changed))
@maps = {}
@mount_at_root = options[:mount_at_root].nil? ? locales.first : options[:mount_at_root]
configure_i18n
logger.info "== Locales: #{locales.join(', ')} (Default #{@mount_at_root})"
end
|
#locale ⇒ Object
Also known as:
lang
137
138
139
|
# File 'middleman-core/lib/middleman-core/core_extensions/i18n.rb', line 137
def locale
::I18n.locale
end
|
#locales ⇒ Object
Also known as:
langs
129
130
131
|
# File 'middleman-core/lib/middleman-core/core_extensions/i18n.rb', line 129
def locales
@locales ||= known_locales
end
|
#localized_path(path, locale) ⇒ Object
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# File 'middleman-core/lib/middleman-core/core_extensions/i18n.rb', line 272
def localized_path(path, locale)
lookup = ::Middleman::Util.parse_uri(path)
return path if lookup.host
lookup.path << app.config[:index_file] if lookup.path&.end_with?('/')
if @lookup[lookup.path] && @lookup[lookup.path][locale]
lookup.path = @lookup[lookup.path][locale]
lookup.to_s
end
rescue ::Addressable::URI::InvalidURIError
nil
end
|
#manipulate_resource_list_container!(resource_list) ⇒ Object
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'middleman-core/lib/middleman-core/core_extensions/i18n.rb', line 146
def manipulate_resource_list_container!(resource_list)
new_resources = []
if options[:templates_dir]
templates_glob = File.join(options[:templates_dir], '**')
file_extension_resources = resource_list.select do |resource|
File.fnmatch?(templates_glob, resource.path) &&
parse_locale_extension(resource.path)
end
localizable_folder_resources = resource_list.select do |resource|
!file_extension_resources.include?(resource) &&
File.fnmatch?(templates_glob, resource.path)
end
else
file_extension_resources = resource_list.select do |resource|
parse_locale_extension(resource.path)
end
localizable_folder_resources = []
end
localizable_folder_resources.each do |resource|
next if resource.ignored?
page_id = File.basename(resource.path, File.extname(resource.path))
locales.each do |locale|
path = resource.path.sub(options[:templates_dir], '')
new_resources << build_resource(path, resource.path, page_id, locale)
end
resource_list.update!(resource, :ignored) do
resource.ignore!
end
end
file_extension_resources.each do |resource|
next if resource.ignored?
result = parse_locale_extension(resource.path)
ext_locale, path, page_id = result
new_resource = build_resource(path, resource.path, page_id, ext_locale)
exists = new_resources.find { |r| r.path == new_resource.path }
new_resources.delete(exists) if exists
new_resources << new_resource
resource_list.update!(resource, :ignored) do
resource.ignore!
end
end
@source_path_group = new_resources.group_by do |resource|
_locale, path, _page_id = parse_locale_extension(resource.source_path)
path ||= resource.source_path
path
end
@lookup = {}
@source_path_group.each do |_path, resources|
exposed_paths = resources.map(&:path)
locale_map = resources.each_with_object({}) do |resource, map|
map[resource.locale] = "/#{resource.path}"
end
exposed_paths.each do |path|
@lookup["/#{path}"] = locale_map
end
end
new_resources.each do |r|
r.execute_descriptor(app, resource_list)
end
end
|
#path_root(locale) ⇒ Object
287
288
289
290
291
292
293
294
|
# File 'middleman-core/lib/middleman-core/core_extensions/i18n.rb', line 287
def path_root(locale)
if (options[:mount_at_root] == locale) || (options[:mount_at_root].nil? && locales[0] == locale)
'/'
else
replacement = options[:locale_map][locale] || locale
options[:path].sub(':locale', replacement.to_s).sub(':lang', replacement.to_s) end
end
|