Class: Rosette::Core::Bcp47Locale
- Defined in:
- lib/rosette/core/extractor/locale.rb
Overview
Represents a locale in the BCP-47 format.
Constant Summary
Constants inherited from Locale
Instance Attribute Summary
Attributes inherited from Locale
Class Method Summary collapse
-
.parse(locale_code) ⇒ Locale
Separates the locale code into langauge and territory components.
-
.valid?(locale_code) ⇒ Boolean
Determines if the given locale code is a valid BCP-47 locale.
Instance Method Summary collapse
-
#code ⇒ String
Constructs a string locale code from the language and territory components.
Methods inherited from Locale
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.
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.
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
#code ⇒ String
Constructs a string locale code from the language and territory components.
112 113 114 |
# File 'lib/rosette/core/extractor/locale.rb', line 112 def code territory ? language + "-#{territory}" : language end |