Class: HeadMusic::Named::Locale
- Inherits:
-
Object
- Object
- HeadMusic::Named::Locale
- Defined in:
- lib/head_music/named/locale.rb
Overview
Locale encapsulates a language and optional region or country.
Constant Summary collapse
- DEFAULT_CODE =
:en_US
Instance Attribute Summary collapse
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
Class Method Summary collapse
Instance Method Summary collapse
-
#code ⇒ Object
-
#initialize(language:, region: nil) ⇒ Locale
constructor
A new instance of Locale.
Constructor Details
#initialize(language:, region: nil) ⇒ Locale
Returns a new instance of Locale.
21 22 23 24 |
# File 'lib/head_music/named/locale.rb', line 21 def initialize(language:, region: nil) @language = language @region = region end |
Instance Attribute Details
#language ⇒ Object (readonly)
Returns the value of attribute language.
6 7 8 |
# File 'lib/head_music/named/locale.rb', line 6 def language @language end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
6 7 8 |
# File 'lib/head_music/named/locale.rb', line 6 def region @region end |
Class Method Details
.default_locale ⇒ Object
8 9 10 |
# File 'lib/head_music/named/locale.rb', line 8 def self.default_locale get(DEFAULT_CODE) end |
.get(code) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/head_music/named/locale.rb', line 12 def self.get(code) @locales ||= {} parts = code.to_s.split(/[_-]/) language = parts[0].downcase region = parts[1]&.upcase key = [language, region].compact.join("_").to_sym @locales[key] ||= new(language: language, region: region) end |
Instance Method Details
#code ⇒ Object
26 27 28 |
# File 'lib/head_music/named/locale.rb', line 26 def code @code ||= [@language, @region].compact.join("_") end |