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

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

.defaultLocale

Returns The default locale.

Returns:

  • (Locale)

    The default locale



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

def default
  @default
end

Instance Attribute Details

#languageString (readonly)

Returns ISO 639-1 language.

Returns:

  • (String)

    ISO 639-1 language



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

def language
  @language
end

#regionString (readonly)

Returns ISO 3166-1 alpha-2 country code.

Returns:

  • (String)

    ISO 3166-1 alpha-2 country code



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

def region
  @region
end

#scriptString (readonly)

Returns ISO 15924 script.

Returns:

  • (String)

    ISO 15924 script



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

def script
  @script
end

Class Method Details

.language_codesArray<String>

Returns A list of ISO 639-1 language codes.

Returns:

  • (Array<String>)

    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

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



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.

Parameters:

  • other (Object)

    Another object

Returns:

  • (boolean)

    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.

Parameters:

  • other (Object)

    Another object

Returns:

  • (boolean)

    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_sString

Returns Locale as a string.

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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