Module: Pagy::I18n

Extended by:
I18n
Included in:
I18n
Defined in:
lib/pagy/i18n.rb

Overview

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

Defined Under Namespace

Modules: P11n

Constant Summary collapse

DATA =

Stores the i18n DATA structure for each loaded locale default on the first locale DATA

Hash.new { |hash, _| hash.first[1] }

Instance Method Summary collapse

Instance Method Details

#load(*locales) ⇒ Object

Public method to configure the locales: overrides the default, build the DATA and freezes it



145
146
147
148
149
# File 'lib/pagy/i18n.rb', line 145

def load(*locales)
  DATA.clear
  build(*locales)
  DATA.freeze
end

#t(locale, key, **opts) ⇒ Object

Translate and pluralize the key with the locale DATA



152
153
154
155
156
157
# File 'lib/pagy/i18n.rb', line 152

def t(locale, key, **opts)
  data, pluralize = DATA[locale]
  translation = data[key] || (opts[:count] && data[key += ".#{pluralize.call(opts[:count])}"]) \
                  or return %([translation missing: "#{key}"])
  translation.gsub(/%{[^}]+?}/) { |match| opts[:"#{match[2..-2]}"] || match }
end