Class: Spree::Locale
- Inherits:
-
Object
- Object
- Spree::Locale
- Includes:
- ActiveModel::Model, Comparable
- Defined in:
- app/models/spree/locale.rb
Overview
Virtual model for a supported locale. Wraps a locale code and exposes its
display name, layout direction, and whether it is a store's default — the
single home for that logic (RTL list, name resolution) instead of being
restated in serializers, helpers, and the admin RTL module.
Constant Summary collapse
- RTL_LANGUAGE_CODES =
ISO 639-1 language codes that use right-to-left scripts.
%w[ar he fa ur yi].freeze
Instance Attribute Summary collapse
-
#code ⇒ String
The locale code, e.g.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare/equality by code so a Locale slots into string-keyed collections (e.g.
locale == "en",[locale].sort). -
#default? ⇒ Boolean
Whether this is the store's default (source) locale.
-
#direction ⇒ String
"rtl" or "ltr".
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#label ⇒ String
Select label, e.g.
-
#language_code ⇒ String
The base ISO 639-1 language code, dropping any region (e.g. "pt-BR" → "pt").
-
#name ⇒ String
Display name, e.g.
-
#rtl? ⇒ Boolean
Whether the locale uses a right-to-left script.
-
#store ⇒ Spree::Store
The current store.
- #to_s ⇒ Object
Instance Attribute Details
#code ⇒ String
Returns the locale code, e.g. "en", "pt-BR".
15 16 17 |
# File 'app/models/spree/locale.rb', line 15 def code @code end |
Instance Method Details
#<=>(other) ⇒ Object
Compare/equality by code so a Locale slots into string-keyed collections
(e.g. locale == "en", [locale].sort).
77 78 79 |
# File 'app/models/spree/locale.rb', line 77 def <=>(other) to_s <=> other.to_s end |
#default? ⇒ Boolean
Returns whether this is the store's default (source) locale.
51 52 53 |
# File 'app/models/spree/locale.rb', line 51 def default? store.present? && code.to_s == store.default_locale.to_s end |
#direction ⇒ String
Returns "rtl" or "ltr".
61 62 63 |
# File 'app/models/spree/locale.rb', line 61 def direction rtl? ? 'rtl' : 'ltr' end |
#eql?(other) ⇒ Boolean
81 82 83 |
# File 'app/models/spree/locale.rb', line 81 def eql?(other) other.is_a?(Spree::Locale) && code.to_s == other.code.to_s end |
#hash ⇒ Object
85 86 87 |
# File 'app/models/spree/locale.rb', line 85 def hash code.to_s.hash end |
#label ⇒ String
Select label, e.g. "EN — English".
38 39 40 41 42 43 |
# File 'app/models/spree/locale.rb', line 38 def label upper = code.to_s.upcase return upper if name.blank? || name.casecmp?(code.to_s) "#{upper} — #{name}" end |
#language_code ⇒ String
The base ISO 639-1 language code, dropping any region (e.g. "pt-BR" → "pt").
67 68 69 |
# File 'app/models/spree/locale.rb', line 67 def language_code code.to_s.downcase.tr('_', '-').split('-').first end |
#name ⇒ String
Display name, e.g. "English", "Deutsch". Falls back to the code for an unknown locale.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/models/spree/locale.rb', line 20 def name code = self.code.to_s if I18n.exists?('spree.i18n.this_file_language', locale: code, fallback: false) return normalize_name(Spree.t('i18n.this_file_language', locale: code)) end if defined?(SpreeI18n::Locale) && (name = SpreeI18n::Locale.local_language_name(code)) return normalize_name(name) end return 'English' if code == 'en' code end |
#rtl? ⇒ Boolean
Returns whether the locale uses a right-to-left script.
56 57 58 |
# File 'app/models/spree/locale.rb', line 56 def rtl? RTL_LANGUAGE_CODES.include?(language_code) end |
#store ⇒ Spree::Store
Returns the current store.
46 47 48 |
# File 'app/models/spree/locale.rb', line 46 def store @store ||= Spree::Store.current end |
#to_s ⇒ Object
71 72 73 |
# File 'app/models/spree/locale.rb', line 71 def to_s code.to_s end |