class Translatomatic::Locale
Represents a locale @see en.wikipedia.org/wiki/Locale_(computer_software)
Constants
- VALID_LANGUAGES
list of 2 letter country codes
Attributes
@return [Locale] The default locale
@return [String] ISO 639-1 language
@return [String] ISO 3166-1 alpha-2 country code
@return [String] ISO 15924 script
Public Class Methods
@return [Array<String>] A list of ISO 639-1 language codes
# File lib/translatomatic/locale.rb, line 36 def language_codes VALID_LANGUAGES end
@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 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
(see eql?
)
# File lib/translatomatic/locale.rb, line 69 def ==(other) eql?(other) end
@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
@!visibility private
# File lib/translatomatic/locale.rb, line 74 def hash [language, script, region].hash end
@return [String] Locale
as a string
# File lib/translatomatic/locale.rb, line 57 def to_s [language, script, region].compact.join('-') end
@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