Class: Middleman::CoreExtensions::Internationalization

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

Defined Under Namespace

Modules: LocaleHelpers Classes: LocalizedPageDescriptor

Instance Attribute Summary collapse

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.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/middleman-more/core_extensions/i18n.rb', line 12

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

  @lookup = {}

  # 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'

  ::Middleman::Sitemap::Resource.send :attr_accessor, :locale_root_path

  app.send :include, LocaleHelpers
end

Instance Attribute Details

#lookupObject (readonly)

Returns the value of attribute lookup.



10
11
12
# File 'lib/middleman-more/core_extensions/i18n.rb', line 10

def lookup
  @lookup
end

Instance Method Details

#after_configurationObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/middleman-more/core_extensions/i18n.rb', line 35

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

  @locales_glob = File.join(app.config[:locales_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



93
94
95
# File 'lib/middleman-more/core_extensions/i18n.rb', line 93

def langs
  @_langs ||= known_languages
end

#localized_path(path, lang) ⇒ Object



138
139
140
141
142
143
# File 'lib/middleman-more/core_extensions/i18n.rb', line 138

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

  @lookup[lookup_path] && @lookup[lookup_path][lang]
end

#manipulate_resource_list(resources)

This method returns an undefined value.

Update the main sitemap resource list



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/middleman-more/core_extensions/i18n.rb', line 99

def manipulate_resource_list(resources)
  @_localization_data = {}

  new_resources = []

  file_extension_resources = resources.select do |resource|
    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.map do |resource|
    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

  # If it uses file extension localization
  file_extension_resources.map do |resource|
    result = parse_locale_extension(resource.path)
    ext_lang, path, page_id = result
    new_resources << build_resource(path, resource.path, page_id, ext_lang)
  end

  @lookup = new_resources.each_with_object({}) do |desc, sum|
    abs_path = desc.source_path.sub(options[:templates_dir], '')
    sum[abs_path] ||= {}
    sum[abs_path][desc.lang] = '/' + desc.path
  end

  resources + new_resources.map { |r| r.to_resource(app) }
end