Class: Translatomatic::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/translatomatic/locale.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#languageObject (readonly)

Returns the value of attribute language.



3
4
5
# File 'lib/translatomatic/locale.rb', line 3

def language
  @language
end

#regionObject (readonly)

Returns the value of attribute region.



5
6
7
# File 'lib/translatomatic/locale.rb', line 5

def region
  @region
end

#scriptObject (readonly)

Returns the value of attribute script.



4
5
6
# File 'lib/translatomatic/locale.rb', line 4

def script
  @script
end

Class Method Details

.defaultObject



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

Returns:

  • (Boolean)


34
35
36
# File 'lib/translatomatic/locale.rb', line 34

def eql?(other)
  other.kind_of?(Translatomatic::Locale) && other.hash == hash
end

#hashObject



42
43
44
# File 'lib/translatomatic/locale.rb', line 42

def hash
  [language, script, region].hash
end

#to_sObject



30
31
32
# File 'lib/translatomatic/locale.rb', line 30

def to_s
  [language, script, region].compact.join("-")
end

#valid?Boolean

Returns:

  • (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