Class: Translatomatic::Locale
- Inherits:
-
Object
- Object
- Translatomatic::Locale
- Defined in:
- lib/translatomatic/locale.rb
Overview
Represents a 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
-
.default ⇒ Locale
The default locale.
-
.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.
35 36 37 38 39 40 41 42 |
# File 'lib/translatomatic/locale.rb', line 35 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 ⇒ String (readonly)
Returns ISO 639-1 language.
6 7 8 |
# File 'lib/translatomatic/locale.rb', line 6 def language @language end |
#region ⇒ String (readonly)
Returns ISO 3166-1 alpha-2 country code.
12 13 14 |
# File 'lib/translatomatic/locale.rb', line 12 def region @region end |
#script ⇒ String (readonly)
Returns ISO 15924 script.
9 10 11 |
# File 'lib/translatomatic/locale.rb', line 9 def script @script end |
Class Method Details
.default ⇒ Locale
Returns the default locale.
30 31 32 |
# File 'lib/translatomatic/locale.rb', line 30 def self.default parse(Translatomatic::Config.instance.default_locale) end |
.parse(tag, validate = true) ⇒ Locale
Parse the given tag
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/translatomatic/locale.rb', line 18 def self.parse(tag, validate = true) return nil if tag.nil? locale = tag unless tag.kind_of?(Translatomatic::Locale) tag = tag.to_s.gsub(/_/, '-') 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.
62 63 64 |
# File 'lib/translatomatic/locale.rb', line 62 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.
57 58 59 |
# File 'lib/translatomatic/locale.rb', line 57 def eql?(other) other.kind_of?(Translatomatic::Locale) && other.hash == hash end |
#to_s ⇒ String
Returns Locale as a string.
50 51 52 |
# File 'lib/translatomatic/locale.rb', line 50 def to_s [language, script, region].compact.join("-") end |
#valid? ⇒ Boolean
Returns true if language is a valid ISO 639-1 language.
45 46 47 |
# File 'lib/translatomatic/locale.rb', line 45 def valid? VALID_LANGUAGES.include?(language) end |