Class: Rosette::Core::Locale
- Inherits:
-
Object
- Object
- Rosette::Core::Locale
- Defined in:
- lib/rosette/core/extractor/locale.rb
Overview
Base class for representing locales. Locales are defined as a combination of language and territory. For example, the BCP-47 locale code for English from the United States is “en-US”. The first part, “en”, stands for “English”, while the second part, “US”, stands for “United States”.
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_FORMAT =
The default locale format. A locale format defines how a locale code should be formatted. There are a number of formats, including BCP-47, ISO-639-1, ISO-639-2, etc.
:bcp_47
Instance Attribute Summary collapse
-
#language ⇒ String
readonly
The locale’s language component.
-
#territory ⇒ String
readonly
The locale’s territory component.
Class Method Summary collapse
-
.parse(locale_code, format = DEFAULT_FORMAT) ⇒ Locale
Using the given format, separate the locale code into language and territory.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
A synonym for #eql?.
-
#eql?(other) ⇒ Boolean
Determines if this locale is equal to another.
-
#initialize(language, territory = nil) ⇒ Locale
constructor
Creates a new locale.
Constructor Details
#initialize(language, territory = nil) ⇒ Locale
Creates a new locale.
48 49 50 51 52 |
# File 'lib/rosette/core/extractor/locale.rb', line 48 def initialize(language, territory = nil) @language = language @territory = territory after_initialize end |
Instance Attribute Details
#language ⇒ String (readonly)
Returns the locale’s language component.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rosette/core/extractor/locale.rb', line 18 class Locale # The default locale format. A locale format defines how a locale code # should be formatted. There are a number of formats, including BCP-47, # ISO-639-1, ISO-639-2, etc. DEFAULT_FORMAT = :bcp_47 class << self # Using the given format, separate the locale code into language and # territory. # # @param [String] locale_code The locale code to parse. # @param [Symbol] format The format of the locale. # @return [Locale] def parse(locale_code, format = DEFAULT_FORMAT) format_str = "#{StringUtils.camelize(format.to_s)}Locale" if Rosette::Core.const_defined?(format_str) Rosette::Core.const_get(format_str).parse(locale_code) else raise ArgumentError, "locale format '#{format}' wasn't recognized" end end end attr_reader :language, :territory # Creates a new locale. # # @param [String] language the locale's language component. # @param [nil, String] territory The locale's territory component. def initialize(language, territory = nil) @language = language @territory = territory after_initialize end # Determines if this locale is equal to another. In order for locales # to be equal, both the language and territory must be equal. This method # ignores casing. # # @param [Locale] other The locale to compare to this one. # @return [Boolean] true if +other+ and this locale are equivalent, false # otherwise. def eql?(other) other.is_a?(self.class) && downcase(other.language) == downcase(language) && downcase(other.territory) == downcase(territory) end # A synonym for {#eql?}. # # @param [Locale] other # @return [Boolean] def ==(other) eql?(other) end private def after_initialize; end def downcase(str) (str || '').downcase end end |
#territory ⇒ String (readonly)
Returns the locale’s territory component.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rosette/core/extractor/locale.rb', line 18 class Locale # The default locale format. A locale format defines how a locale code # should be formatted. There are a number of formats, including BCP-47, # ISO-639-1, ISO-639-2, etc. DEFAULT_FORMAT = :bcp_47 class << self # Using the given format, separate the locale code into language and # territory. # # @param [String] locale_code The locale code to parse. # @param [Symbol] format The format of the locale. # @return [Locale] def parse(locale_code, format = DEFAULT_FORMAT) format_str = "#{StringUtils.camelize(format.to_s)}Locale" if Rosette::Core.const_defined?(format_str) Rosette::Core.const_get(format_str).parse(locale_code) else raise ArgumentError, "locale format '#{format}' wasn't recognized" end end end attr_reader :language, :territory # Creates a new locale. # # @param [String] language the locale's language component. # @param [nil, String] territory The locale's territory component. def initialize(language, territory = nil) @language = language @territory = territory after_initialize end # Determines if this locale is equal to another. In order for locales # to be equal, both the language and territory must be equal. This method # ignores casing. # # @param [Locale] other The locale to compare to this one. # @return [Boolean] true if +other+ and this locale are equivalent, false # otherwise. def eql?(other) other.is_a?(self.class) && downcase(other.language) == downcase(language) && downcase(other.territory) == downcase(territory) end # A synonym for {#eql?}. # # @param [Locale] other # @return [Boolean] def ==(other) eql?(other) end private def after_initialize; end def downcase(str) (str || '').downcase end end |
Class Method Details
.parse(locale_code, format = DEFAULT_FORMAT) ⇒ Locale
Using the given format, separate the locale code into language and territory.
31 32 33 34 35 36 37 38 39 |
# File 'lib/rosette/core/extractor/locale.rb', line 31 def parse(locale_code, format = DEFAULT_FORMAT) format_str = "#{StringUtils.camelize(format.to_s)}Locale" if Rosette::Core.const_defined?(format_str) Rosette::Core.const_get(format_str).parse(locale_code) else raise ArgumentError, "locale format '#{format}' wasn't recognized" end end |
Instance Method Details
#==(other) ⇒ Boolean
A synonym for #eql?.
71 72 73 |
# File 'lib/rosette/core/extractor/locale.rb', line 71 def ==(other) eql?(other) end |
#eql?(other) ⇒ Boolean
Determines if this locale is equal to another. In order for locales to be equal, both the language and territory must be equal. This method ignores casing.
61 62 63 64 65 |
# File 'lib/rosette/core/extractor/locale.rb', line 61 def eql?(other) other.is_a?(self.class) && downcase(other.language) == downcase(language) && downcase(other.territory) == downcase(territory) end |