Module: Pagy::I18n

Extended by:
I18n
Included in:
I18n
Defined in:
lib/pagy/modules/i18n/i18n.rb,
lib/pagy/modules/i18n/p11n.rb,
lib/pagy/modules/i18n/p11n/other.rb,
lib/pagy/modules/i18n/p11n/arabic.rb,
lib/pagy/modules/i18n/p11n/polish.rb,
lib/pagy/modules/i18n/p11n/one_other.rb,
lib/pagy/modules/i18n/p11n/east_slavic.rb,
lib/pagy/modules/i18n/p11n/west_slavic.rb,
lib/pagy/modules/i18n/p11n/one_upto_two_other.rb

Overview

Pagy i18n implementation, compatible with the I18n gem, just a lot faster and lighter

Defined Under Namespace

Modules: P11n Classes: KeyError

Instance Method Summary collapse

Instance Method Details

#localeObject



26
27
28
# File 'lib/pagy/modules/i18n/i18n.rb', line 26

def locale
  Thread.current[:pagy_locale] || 'en'
end

#locale=(value) ⇒ Object

Store the variable for the duration of a single request



22
23
24
# File 'lib/pagy/modules/i18n/i18n.rb', line 22

def locale=(value)
  Thread.current[:pagy_locale] = value.to_s
end

#localesObject



17
18
19
# File 'lib/pagy/modules/i18n/i18n.rb', line 17

def locales
  @locales ||= {}
end

#pathnamesObject



13
14
15
# File 'lib/pagy/modules/i18n/i18n.rb', line 13

def pathnames
  @pathnames ||= [ROOT.join('locales')]
end

#translate(key, **options) ⇒ Object

Translate and pluralize the key with the locale entries



31
32
33
34
35
36
37
38
# File 'lib/pagy/modules/i18n/i18n.rb', line 31

def translate(key, **options)
  data, p11n = locales[locale] || self.load
  key       += ".#{p11n.plural_for(options[:count])}" if !data[key] && options[:count]

  translation = data[key] or return %([translation missing: "#{key}"])

  translation.gsub(/%{[^}]+?}/) { options.fetch(_1[2..-2].to_sym, _1) } # replace the interpolation placeholders
end