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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#code ⇒ String

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

Returns:

  • (String) —

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



17
18
19
# File 'app/models/spree/locale.rb', line 17

def code
  @code
end

Class Method Details

.language_names ⇒ Hash{String => String}

Each language's name in that language ("Deutsch", "日本語"), keyed by code.

Returns:

  • (Hash{String => String})


35
36
37
# File 'app/models/spree/locale.rb', line 35

def self.language_names
  @language_names ||= ::CSV.read(File.expand_path('../../../config/language_names.csv', __dir__)).to_h { |row| [row[0], row[2]] }
end

Instance Method Details

#<=>(other) ⇒ Object

Compare/equality by code so a Locale slots into string-keyed collections (e.g. locale == "en", [locale].sort).



80
81
82
# File 'app/models/spree/locale.rb', line 80

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



54
55
56
# File 'app/models/spree/locale.rb', line 54

def default?
  store.present? && code.to_s == store.default_locale.to_s
end

#direction ⇒ String

Returns "rtl" or "ltr".

Returns:

  • (String) —

    "rtl" or "ltr"



64
65
66
# File 'app/models/spree/locale.rb', line 64

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  other.is_a?(Spree::Locale) && code.to_s == other.code.to_s
end

#hash ⇒ Object



88
89
90
# File 'app/models/spree/locale.rb', line 88

def hash
  code.to_s.hash
end

#label ⇒ String

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

Returns:

  • (String)


41
42
43
44
45
46
# File 'app/models/spree/locale.rb', line 41

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").

Returns:

  • (String)


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

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.

Returns:

  • (String)


22
23
24
25
26
27
28
29
30
31
# File 'app/models/spree/locale.rb', line 22

def name
  code = self.code.to_s

  if I18n.exists?('spree.i18n.this_file_language', locale: code, fallback: false)
    return normalize_name(I18n.t('spree.i18n.this_file_language', locale: code))
  end

  name = self.class.language_names[code.downcase] || self.class.language_names[language_code]
  name ? normalize_name(name) : 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



59
60
61
# File 'app/models/spree/locale.rb', line 59

def rtl?
  RTL_LANGUAGE_CODES.include?(language_code)
end

#store ⇒ Spree::Store

Returns the current store.

Returns:



49
50
51
# File 'app/models/spree/locale.rb', line 49

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

#to_s ⇒ Object



74
75
76
# File 'app/models/spree/locale.rb', line 74

def to_s
  code.to_s
end