Class: Rosette::Core::Bcp47Locale

Inherits:
Locale
  • Object
show all
Defined in:
lib/rosette/core/extractor/locale.rb

Overview

Represents a locale in the BCP-47 format.

Constant Summary

Constants inherited from Locale

Locale::DEFAULT_FORMAT

Instance Attribute Summary

Attributes inherited from Locale

#language, #territory

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Locale

#==, #eql?, #initialize

Constructor Details

This class inherits a constructor from Rosette::Core::Locale

Class Method Details

.parse(locale_code) ⇒ Locale

Separates the locale code into langauge and territory components.

Parameters:

  • locale_code (String)

    The locale code to parse.

Returns:


91
92
93
94
95
96
97
# File 'lib/rosette/core/extractor/locale.rb', line 91

def parse(locale_code)
  if valid?(locale_code)
    new(*locale_code.split(/[-_]/))
  else
    raise InvalidLocaleError, "'#{locale_code}' is not a valid BCP-47 locale"
  end
end

.valid?(locale_code) ⇒ Boolean

Determines if the given locale code is a valid BCP-47 locale.

Parameters:

  • locale_code (String)

    The locale code to validate.

Returns:

  • (Boolean)

    true if locale_code is a valid BCP-47 locale, false otherwise.


104
105
106
# File 'lib/rosette/core/extractor/locale.rb', line 104

def valid?(locale_code)
  !!(locale_code =~ /\A[a-zA-Z]{2,4}(?:[-_][a-zA-Z0-9]{2,5})?\z/)
end

Instance Method Details

#codeString

Constructs a string locale code from the language and territory components.

Returns:

  • (String)

    the language and territory separated by a dash.


112
113
114
# File 'lib/rosette/core/extractor/locale.rb', line 112

def code
  territory ? language + "-#{territory}" : language
end