Class: Middleman::CoreExtensions::Internationalization

Inherits:
Extension
  • Object
show all
Defined in:
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, option, #ready, resources

Methods included from Contracts

#Contract

Constructor Details

#initializeInternationalization

Returns a new instance of Internationalization.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/middleman-core/core_extensions/i18n.rb', line 15

def initialize(*)
  super

  require 'i18n'

  options[:locales] = options[:langs] unless options[:langs].nil?

  options[:locale_map] = options[:lang_map] unless options[:lang_map].nil?

  # Don't fail on invalid locale, that's not what our current
  # users expect.
  ::I18n.enforce_available_locales = false

  # This is for making the tests work - since the tests
  # don't completely reload middleman, I18n.load_path can get
  # polluted with paths from other test app directories that don't
  # exist anymore.
  if ENV['TEST']
    app.after_configuration_eval do
      ::I18n.load_path.delete_if { |path| path =~ %r{tmp/aruba} }
      ::I18n.reload!
    end
  end
end

Instance Method Details

#after_configurationObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/middleman-core/core_extensions/i18n.rb', line 40

def after_configuration
  # See https://github.com/svenfuchs/i18n/wiki/Fallbacks
  unless options[:no_fallbacks]
    require 'i18n/backend/fallbacks'
    ::I18n::Backend::Simple.send(:include, ::I18n::Backend::Fallbacks)
  end

  locales_file_path = options[:data]

  # Tell the file watcher to observe the :data_dir
  app.files.watch :locales,
                  path: File.join(app.root, locales_file_path),
                  only: /.*(rb|yml|yaml)$/

  # Setup data files before anything else so they are available when
  # parsing config.rb
  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

#localeObject Also known as: lang



140
141
142
# File 'lib/middleman-core/core_extensions/i18n.rb', line 140

def locale
  ::I18n.locale
end

#localesObject Also known as: langs



132
133
134
# File 'lib/middleman-core/core_extensions/i18n.rb', line 132

def locales
  @locales ||= known_locales
end

#localized_path(path, locale) ⇒ Object



258
259
260
261
262
263
# File 'lib/middleman-core/core_extensions/i18n.rb', line 258

def localized_path(path, locale)
  lookup_path = path.dup
  lookup_path << app.config[:index_file] if lookup_path.end_with?('/')

  @lookup[lookup_path] && @lookup[lookup_path][locale]
end

#manipulate_resource_list(resources) ⇒ Object



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
# File 'lib/middleman-core/core_extensions/i18n.rb', line 150

def manipulate_resource_list(resources)
  new_resources = []

  file_extension_resources = resources.select do |resource|
    # Ignore resources which are outside of the localizable directory
    File.fnmatch?(File.join(options[:templates_dir], '**'), resource.path) &&
      parse_locale_extension(resource.path)
  end

  localizable_folder_resources = resources.select do |resource|
    !file_extension_resources.include?(resource) && File.fnmatch?(File.join(options[:templates_dir], '**'), resource.path)
  end

  # If it's a "localizable template"
  localizable_folder_resources.each do |resource|
    next if resource.ignored?

    page_id = File.basename(resource.path, File.extname(resource.path))
    locales.each do |locale|
      # Remove folder name
      path = resource.path.sub(options[:templates_dir], '')
      new_resources << build_resource(path, resource.path, page_id, locale)
    end

    resource.ignore!

    # This is for backwards compatibility with the old provides_metadata-based code
    # that used to be in this extension, but I don't know how much sense it makes.
    # next if resource.options[:locale]

    # $stderr.puts "Defaulting #{resource.path} to #{@mount_at_root}"
    # resource.add_metadata options: { locale: @mount_at_root }, locals: { locale: @mount_at_root }
  end

  # If it uses file extension localization
  file_extension_resources.each do |resource|
    next if resource.ignored?

    result = parse_locale_extension(resource.path)
    ext_locale, path, page_id = result
    new_resources << build_resource(path, resource.path, page_id, ext_locale)

    resource.ignore!
  end

  # This generates a lookup hash that maps the real path (as seen by the web
  # page user) to the paths of the localized versions. The lookup is later
  # used by `url_for '/some/page.html', :locale => :en` and other url
  # helpers.
  #
  # For example (given :mount_at_root => :es) and localized paths:
  #
  # @lookup['/en/magic/stuff.html'] = {:en => '/en/magic/stuff.html', :de => '/de/magisches/zeug.html', :es => '/magico/cosas.html'}
  # @lookup['/de/index.html'] = {:en => '/en/index.html', :de => '/de/index.html', :es => '/index.html'}
  # @lookup['/en/index.html'] = {:en => '/en/index.html', :de => '/de/index.html', :es => '/index.html'}
  # @lookup['/index.html'] = {:en => '/en/index.html', :de => '/de/index.html', :es => '/index.html'}
  #
  # We do this by grouping by the source paths with the locales removed. All
  # the localized pages with the same content in different languages get the
  # same key.
  #
  @source_path_group = new_resources.group_by do |resource|
    # Try to get source path without extension
    _locale, path, _page_id = parse_locale_extension(resource.source_path)

    # If that fails, there is no extension, so we use the original path. We
    # can not use resource.path here, because .path may be translated, so the
    # file names do not match up.
    path ||= resource.source_path

    # This will contain the localizable/ directory, but that does not matter,
    # because it will be contained in both alternatives above, so the
    # grouping key will be correct.
    path
  end

  # Then we walk this grouped hash and generate the lookup table as given
  # above.
  @lookup = {}
  @source_path_group.each do |src_path, resources|
    # For each group we generate a list of the paths the user really sees
    # (e.g. ['/en/index.html', '/de/index.html', '/index.html'])
    exposed_paths = resources.map(&:path)

    # We also generate a map with the same infos, but with the locales as keys.
    # e.g. {:en => '/en/index.html', :de => '/de/index.html', :es => '/index.html'}
    locale_map = resources.each_with_object({}) do |resource, map|
      map[resource.locale] = '/' + resource.path
    end

    # Then we add those to the lookup table, so every path has a
    # cross-reference to any other path in other locales.
    exposed_paths.each do |path|
      @lookup['/' + path] = locale_map
    end

    if @mount_at_root == false
      src_path = src_path.sub(options[:templates_dir] + '/', '')
      @lookup["/#{src_path}"] = locale_map
    end
  end

  new_resources.reduce(resources) do |sum, r|
    r.execute_descriptor(app, sum)
  end
end

#path_root(locale) ⇒ Object



266
267
268
269
270
271
272
273
# File 'lib/middleman-core/core_extensions/i18n.rb', line 266

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) # Backward compat
  end
end