Class: Translatomatic::Locale
- Inherits:
-
Object
- Object
- Translatomatic::Locale
- Defined in:
- lib/translatomatic/locale.rb
Overview
Represents a locale
Class Attribute Summary collapse
-
.default ⇒ Locale
The default locale.
Instance Attribute Summary collapse
-
#language ⇒ String
readonly
ISO 639-1 language.
-
#region ⇒ String
readonly
ISO 3166-1 alpha-2 country code.
-
#script ⇒ String
readonly
ISO 15924 script.
Class Method Summary collapse
-
.language_codes ⇒ Array<String>
A list of ISO 639-1 language codes.
-
.parse(tag, validate = true) ⇒ Locale
Parse the given tag.
Instance Method Summary collapse
-
#==(other) ⇒ boolean
True if the other object is a Locale object and has equal language, script and region.
-
#eql?(other) ⇒ boolean
True if the other object is a Locale object and has equal language, script and region.
-
#initialize(tag) ⇒ Locale
constructor
Create a new locale object.
-
#to_s ⇒ String
Locale as a string.
-
#valid? ⇒ Boolean
True if language is a valid ISO 639-1 language.
Constructor Details
#initialize(tag) ⇒ Locale
Returns create a new locale object.
42 43 44 45 46 47 48 49 |
# File 'lib/translatomatic/locale.rb', line 42 def initialize(tag) data = ::I18n::Locale::Tag::Rfc4646.tag(tag) if data @language = data.language @script = data.script @region = data.region end end |
Class Attribute Details
.default ⇒ Locale
Returns The default locale.
18 19 20 |
# File 'lib/translatomatic/locale.rb', line 18 def default @default end |
Instance Attribute Details
#language ⇒ String (readonly)
Returns ISO 639-1 language.
8 9 10 |
# File 'lib/translatomatic/locale.rb', line 8 def language @language end |
#region ⇒ String (readonly)
Returns ISO 3166-1 alpha-2 country code.
14 15 16 |
# File 'lib/translatomatic/locale.rb', line 14 def region @region end |
#script ⇒ String (readonly)
Returns ISO 15924 script.
11 12 13 |
# File 'lib/translatomatic/locale.rb', line 11 def script @script end |
Class Method Details
.language_codes ⇒ Array<String>
Returns A list of ISO 639-1 language codes.
36 37 38 |
# File 'lib/translatomatic/locale.rb', line 36 def language_codes VALID_LANGUAGES end |
.parse(tag, validate = true) ⇒ Locale
Parse the given tag
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/translatomatic/locale.rb', line 24 def parse(tag, validate = true) return nil if tag.nil? locale = tag unless tag.is_a?(Translatomatic::Locale) tag = tag.to_s.tr('_', '-') locale = new(tag) end validate && !locale.valid? ? nil : locale end |
Instance Method Details
#==(other) ⇒ boolean
Returns true if the other object is a Translatomatic::Locale object and has equal language, script and region.
69 70 71 |
# File 'lib/translatomatic/locale.rb', line 69 def ==(other) eql?(other) end |
#eql?(other) ⇒ boolean
Returns true if the other object is a Translatomatic::Locale object and has equal language, script and region.
64 65 66 |
# File 'lib/translatomatic/locale.rb', line 64 def eql?(other) other.is_a?(Translatomatic::Locale) && other.hash == hash end |
#to_s ⇒ String
Returns Locale as a string.
57 58 59 |
# File 'lib/translatomatic/locale.rb', line 57 def to_s [language, script, region].compact.join('-') end |
#valid? ⇒ Boolean
Returns true if language is a valid ISO 639-1 language.
52 53 54 |
# File 'lib/translatomatic/locale.rb', line 52 def valid? VALID_LANGUAGES.include?(language) end |