Class: Utopia::Localization::Locales

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/localization/locales.rb

Overview

Matches configured locales against language ranges.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names) ⇒ Locales

Initialize the configured locales.



25
26
27
28
29
30
31
32
33
34
# File 'lib/utopia/localization/locales.rb', line 25

def initialize(names)
  @names = names
  @patterns = {}
  
  @names.each do |name|
    self.class.expand(name, @patterns)
  end
  
  freeze
end

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



47
48
49
# File 'lib/utopia/localization/locales.rb', line 47

def names
  @names
end

#patternsObject (readonly)

Returns the value of attribute patterns.



48
49
50
# File 'lib/utopia/localization/locales.rb', line 48

def patterns
  @patterns
end

Class Method Details

.expand(locale, patterns) ⇒ Object

Expand a locale into progressively less specific language ranges.



13
14
15
16
17
18
19
20
21
# File 'lib/utopia/localization/locales.rb', line 13

def self.expand(locale, patterns)
  parts = locale.split("-")
  
  while parts.any?
    pattern = parts.join("-")
    patterns[pattern] ||= locale
    parts.pop
  end
end

Instance Method Details

#freezeObject

Freeze this object and its internal state.



38
39
40
41
42
43
44
45
# File 'lib/utopia/localization/locales.rb', line 38

def freeze
  return self if frozen?
  
  @names.freeze
  @patterns.freeze
  
  return super
end

#match(languages) ⇒ Object

Select configured locales matching the given language ranges.



53
54
55
56
57
# File 'lib/utopia/localization/locales.rb', line 53

def match(languages)
  languages.filter_map do |language|
    @patterns[language.name]
  end
end