Class: Spree::Locale

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#codeString

Returns the locale code, e.g. "en", "pt-BR".

Returns:

  • (String)

    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.

Returns:

  • (Boolean)

    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

#directionString

Returns "rtl" or "ltr".

Returns:

  • (String)

    "rtl" or "ltr"



61
62
63
# File 'app/models/spree/locale.rb', line 61

def direction
  rtl? ? 'rtl' : 'ltr'
end

#eql?(other) ⇒ Boolean

Returns:

  • (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

#hashObject



85
86
87
# File 'app/models/spree/locale.rb', line 85

def hash
  code.to_s.hash
end

#labelString

Select label, e.g. "EN — English".

Returns:

  • (String)


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_codeString

The base ISO 639-1 language code, dropping any region (e.g. "pt-BR" → "pt").

Returns:

  • (String)


67
68
69
# File 'app/models/spree/locale.rb', line 67

def language_code
  code.to_s.downcase.tr('_', '-').split('-').first
end

#nameString

Display name, e.g. "English", "Deutsch". Falls back to the code for an unknown locale.

Returns:

  • (String)


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.

Returns:

  • (Boolean)

    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

#storeSpree::Store

Returns the current store.

Returns:



46
47
48
# File 'app/models/spree/locale.rb', line 46

def store
  @store ||= Spree::Store.current
end

#to_sObject



71
72
73
# File 'app/models/spree/locale.rb', line 71

def to_s
  code.to_s
end