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

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

  # 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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/middleman-more/core_extensions/i18n.rb', line 24

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

  if !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



55
56
57
# File 'lib/middleman-more/core_extensions/i18n.rb', line 55

def langs
  @_langs ||= get_known_languages
end

#manipulate_resource_list(resources) ⇒ void

This method returns an undefined value.

Update the main sitemap resource list



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/middleman-more/core_extensions/i18n.rb', line 61

def manipulate_resource_list(resources)
  @_localization_data = {}

  new_resources = []

  resources.each do |resource|
    # If it's a "localizable template"
    if 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
    elsif result = parse_locale_extension(resource.path)
      lang, path, page_id = result
      new_resources << build_resource(path, resource.path, page_id, lang)
    end
  end

  resources + new_resources
end