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



150
151
152
153
154
# File 'lib/pagy/i18n.rb', line 150

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

#translate(locale, key, opts = {}) ⇒ Object Also known as: t

Translate and pluralize the key with the locale DATA



157
158
159
160
161
162
# File 'lib/pagy/i18n.rb', line 157

def translate(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