Module: Bridgetown::Localizable

Included in:
GeneratedPage, Resource::Base
Defined in:
bridgetown-core/lib/bridgetown-core/concerns/localizable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sort_by_locale(items, available_locales) ⇒ Object

Sort items by their locale’s position in the configured available_locales list. Items with unknown locales sort last.



7
8
9
# File 'bridgetown-core/lib/bridgetown-core/concerns/localizable.rb', line 7

def self.sort_by_locale(items, available_locales)
  items.sort_by { |item| available_locales.index(item.data.locale) || Float::INFINITY }
end

Instance Method Details

#all_localesObject



11
12
13
14
15
# File 'bridgetown-core/lib/bridgetown-core/concerns/localizable.rb', line 11

def all_locales
  return @all_locales if @all_locales

  @all_locales = site.tmp_cache.dig(:locale_index, locale_index_key) || find_matching_locales
end

#locale_index_keyObject

Key used to group locale variants of the same content in the locale index. Uses the same matching criteria as matches_resource? (slug + path identity).



31
32
33
34
35
36
37
38
39
# File 'bridgetown-core/lib/bridgetown-core/concerns/localizable.rb', line 31

def locale_index_key
  slug = data.slug
  return unless slug

  path_key = relative_path.is_a?(String) ? localeless_path : relative_path.parent.to_s
  prefix   = respond_to?(:collection) ? collection.label : "generated"

  "#{prefix}:#{slug}:#{path_key}"
end

#localeless_pathObject



25
26
27
# File 'bridgetown-core/lib/bridgetown-core/concerns/localizable.rb', line 25

def localeless_path
  relative_path.gsub(%r{\A#{data.locale}/}, "")
end

#matches_resource?(item) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
# File 'bridgetown-core/lib/bridgetown-core/concerns/localizable.rb', line 17

def matches_resource?(item)
  if item.relative_path.is_a?(String)
    item.localeless_path == localeless_path
  else
    item.relative_path.parent == relative_path.parent
  end && item.data.slug == data.slug
end