Class: Middleman::CoreExtensions::Internationalization

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-more/core_extensions/i18n.rb

Defined Under Namespace

Modules: LocaleHelpers

Instance Attribute Summary

Attributes inherited from Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Extension

activate, activated_extension, after_extension_activated, clear_after_extension_callbacks, config, extension_name, helpers, option, register

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ Internationalization

Returns a new instance of Internationalization.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/middleman-more/core_extensions/i18n.rb', line 10

def initialize(app, options_hash={}, &block)
  super

  # TODO
  # If :directory_indexes is already active,
  # throw a warning explaining the bug and telling the use
  # to reverse the order.

  # 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

  app.config.define_setting :locales_dir, 'locales', 'The directory holding your locale configurations'

  app.send :include, LocaleHelpers
end

Instance Method Details

#after_configurationObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/middleman-more/core_extensions/i18n.rb', line 29

def after_configuration
  app.files.reload_path(app.config[:locals_dir] || options[:data])

  @locales_glob = File.join(app.config[:locals_dir] || options[:data], '**', '*.{rb,yml,yaml}')
  @locales_regex = convert_glob_to_regex(@locales_glob)

  @maps = {}
  @mount_at_root = options[:mount_at_root].nil? ? langs.first : options[:mount_at_root]

  configure_i18n

  unless app.build?
    logger.info "== Locales: #{langs.join(', ')} (Default #{@mount_at_root})"
  end

  # Don't output localizable files
  app.ignore File.join(options[:templates_dir], '**')

  app.sitemap.(&method(:metadata_for_path))
  app.files.changed(&method(:on_file_changed))
  app.files.deleted(&method(:on_file_changed))
end

#langsObject



60
61
62
# File 'lib/middleman-more/core_extensions/i18n.rb', line 60

def langs
  @_langs ||= known_languages
end

#manipulate_resource_list(resources)

This method returns an undefined value.

Update the main sitemap resource list



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/middleman-more/core_extensions/i18n.rb', line 66

def manipulate_resource_list(resources)
  @_localization_data = {}

  new_resources = []

  resources.each do |resource|
    # If it uses file extension localization
    if parse_locale_extension(resource.path)
      result = parse_locale_extension(resource.path)
      ext_lang, path, page_id = result
      new_resources << build_resource(path, resource.path, page_id, ext_lang)
    # If it's a "localizable template"
    elsif File.fnmatch?(File.join(options[:templates_dir], '**'), resource.path)
      page_id = File.basename(resource.path, File.extname(resource.path))
      langs.each do |lang|
        # Remove folder name
        path = resource.path.sub(options[:templates_dir], '')
        new_resources << build_resource(path, resource.path, page_id, lang)
      end
    end
  end

  resources + new_resources
end