Class: Utopia::Localization::Locales
- Inherits:
-
Object
- Object
- Utopia::Localization::Locales
- Defined in:
- lib/utopia/localization/locales.rb
Overview
Matches configured locales against language ranges.
Instance Attribute Summary collapse
-
#names ⇒ Object
readonly
Returns the value of attribute names.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Class Method Summary collapse
-
.expand(locale, patterns) ⇒ Object
Expand a locale into progressively less specific language ranges.
Instance Method Summary collapse
-
#freeze ⇒ Object
Freeze this object and its internal state.
-
#initialize(names) ⇒ Locales
constructor
Initialize the configured locales.
-
#match(languages) ⇒ Object
Select configured locales matching the given language ranges.
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.(name, @patterns) end freeze end |
Instance Attribute Details
#names ⇒ Object (readonly)
Returns the value of attribute names.
47 48 49 |
# File 'lib/utopia/localization/locales.rb', line 47 def names @names end |
#patterns ⇒ Object (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.(locale, patterns) parts = locale.split("-") while parts.any? pattern = parts.join("-") patterns[pattern] ||= locale parts.pop end end |
Instance Method Details
#freeze ⇒ Object
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 |