Class: Translatomatic::Locale
- Inherits:
-
Object
- Object
- Translatomatic::Locale
- Defined in:
- lib/translatomatic/locale.rb
Instance Attribute Summary collapse
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#script ⇒ Object
readonly
Returns the value of attribute script.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(tag) ⇒ Locale
constructor
A new instance of Locale.
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(tag) ⇒ Locale
Returns a new instance of Locale.
16 17 18 19 20 21 22 23 |
# File 'lib/translatomatic/locale.rb', line 16 def initialize(tag) data = I18n::Locale::Tag::Rfc4646.tag(tag) if data @language = data.language @script = data.script @region = data.region end end |
Instance Attribute Details
#language ⇒ Object (readonly)
Returns the value of attribute language.
3 4 5 |
# File 'lib/translatomatic/locale.rb', line 3 def language @language end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
5 6 7 |
# File 'lib/translatomatic/locale.rb', line 5 def region @region end |
#script ⇒ Object (readonly)
Returns the value of attribute script.
4 5 6 |
# File 'lib/translatomatic/locale.rb', line 4 def script @script end |
Class Method Details
.default ⇒ Object
12 13 14 |
# File 'lib/translatomatic/locale.rb', line 12 def self.default DEFAULT_LOCALE end |
.parse(tag, validate = true) ⇒ Object
7 8 9 10 |
# File 'lib/translatomatic/locale.rb', line 7 def self.parse(tag, validate = true) locale = tag.kind_of?(Translatomatic::Locale) ? tag : new(tag) validate && !locale.valid? ? nil : locale end |
Instance Method Details
#==(other) ⇒ Object
38 39 40 |
# File 'lib/translatomatic/locale.rb', line 38 def ==(other) eql?(other) end |
#eql?(other) ⇒ Boolean
34 35 36 |
# File 'lib/translatomatic/locale.rb', line 34 def eql?(other) other.kind_of?(Translatomatic::Locale) && other.hash == hash end |
#hash ⇒ Object
42 43 44 |
# File 'lib/translatomatic/locale.rb', line 42 def hash [language, script, region].hash end |
#to_s ⇒ Object
30 31 32 |
# File 'lib/translatomatic/locale.rb', line 30 def to_s [language, script, region].compact.join("-") end |
#valid? ⇒ Boolean
25 26 27 28 |
# File 'lib/translatomatic/locale.rb', line 25 def valid? # test if lang is a valid ISO 639-1 language VALID_LANGUAGES.include?(language) end |