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

Faster and lighter Pagy i18n implementation, compatible with the I18n gem

Defined Under Namespace

Modules: P11n Classes: KeyError

Constant Summary collapse

LOCALE_PATTERN =

Match only valid locale names. (See www.rfc-editor.org/info/rfc4647/)

/\A[a-zA-Z]{2,8}(-[a-zA-Z0-9]{1,8})*\z/

Instance Method Summary collapse

Instance Method Details

#localeObject



29
30
31
# File 'lib/pagy/modules/i18n/i18n.rb', line 29

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

#locale=(value) ⇒ Object

Set a valid locale or nil for the duration of a single request. Avoid errors/logging.



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

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

#localesObject



20
21
22
# File 'lib/pagy/modules/i18n/i18n.rb', line 20

def locales
  @locales ||= {}
end

#pathnamesObject



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

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

#translate(key, **options) ⇒ Object

Translate and pluralize the key with the locale entries



34
35
36
37
38
39
40
41
# File 'lib/pagy/modules/i18n/i18n.rb', line 34

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