class Translatomatic::Locale

Represents a locale @see en.wikipedia.org/wiki/Locale_(computer_software)

Constants

VALID_LANGUAGES

list of 2 letter country codes

Attributes

default[RW]

@return [Locale] The default locale

language[R]

@return [String] ISO 639-1 language

region[R]

@return [String] ISO 3166-1 alpha-2 country code

script[R]

@return [String] ISO 15924 script

Public Class Methods

language_codes() click to toggle source

@return [Array<String>] A list of ISO 639-1 language codes

# File lib/translatomatic/locale.rb, line 36
def language_codes
  VALID_LANGUAGES
end
new(tag) click to toggle source

@return [Locale] create a new locale object

# 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
parse(tag, validate = true) click to toggle source

Parse the given tag @param tag [String] A string representing a locale @param validate [boolean] If true, return nil if the locale is invalid @return [Locale] A locale object

# 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

Public Instance Methods

==(other) click to toggle source

(see eql?)

# File lib/translatomatic/locale.rb, line 69
def ==(other)
  eql?(other)
end
eql?(other) click to toggle source

@param other [Object] Another object @return [boolean] true if the other object is a {Translatomatic::Locale}

object and has equal language, script and region.
# File lib/translatomatic/locale.rb, line 64
def eql?(other)
  other.is_a?(Translatomatic::Locale) && other.hash == hash
end
hash() click to toggle source

@!visibility private

# File lib/translatomatic/locale.rb, line 74
def hash
  [language, script, region].hash
end
to_s() click to toggle source

@return [String] Locale as a string

# File lib/translatomatic/locale.rb, line 57
def to_s
  [language, script, region].compact.join('-')
end
valid?() click to toggle source

@return true if language is a valid ISO 639-1 language

# File lib/translatomatic/locale.rb, line 52
def valid?
  VALID_LANGUAGES.include?(language)
end