Class: Translatomatic::Locale

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

Overview

Represents a locale

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag) ⇒ Locale



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

.defaultLocale



18
19
20
# File 'lib/translatomatic/locale.rb', line 18

def default
  @default
end

Instance Attribute Details

#languageString (readonly)



8
9
10
# File 'lib/translatomatic/locale.rb', line 8

def language
  @language
end

#regionString (readonly)



14
15
16
# File 'lib/translatomatic/locale.rb', line 14

def region
  @region
end

#scriptString (readonly)



11
12
13
# File 'lib/translatomatic/locale.rb', line 11

def script
  @script
end

Class Method Details

.language_codesArray<String>



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



69
70
71
# File 'lib/translatomatic/locale.rb', line 69

def ==(other)
  eql?(other)
end

#eql?(other) ⇒ boolean



64
65
66
# File 'lib/translatomatic/locale.rb', line 64

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

#to_sString



57
58
59
# File 'lib/translatomatic/locale.rb', line 57

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

#valid?Boolean



52
53
54
# File 'lib/translatomatic/locale.rb', line 52

def valid?
  VALID_LANGUAGES.include?(language)
end