Class: Translatomatic::Locale

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

Overview

Represents a locale

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#languageString (readonly)

Returns ISO 639-1 language.

Returns:

  • (String)

    ISO 639-1 language



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

def language
  @language
end

#regionString (readonly)

Returns ISO 3166-1 alpha-2 country code.

Returns:

  • (String)

    ISO 3166-1 alpha-2 country code



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

def region
  @region
end

#scriptString (readonly)

Returns ISO 15924 script.

Returns:

  • (String)

    ISO 15924 script



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

def script
  @script
end

Class Method Details

.defaultLocale

Returns the default locale.

Returns:

  • (Locale)

    the default locale



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

def self.default
  parse(Translatomatic.config.default_locale)
end

.parse(tag, validate = true) ⇒ Locale

Parse the given tag

Parameters:

  • tag (String)

    A string representing a locale

  • validate (boolean) (defaults to: true)

    If true, return nil if the locale is invalid

Returns:

  • (Locale)

    A locale object



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.

Parameters:

  • other (Object)

    Another object

Returns:

  • (boolean)

    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.

Parameters:

  • other (Object)

    Another object

Returns:

  • (boolean)

    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_sString

Returns Locale as a string.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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