© 2017-2021 Domizio DemichelisMIT License

➡ Chat Support on Gitter ➡

Pagy::I18n

This module provides the Pagy implementation of the i18n translation method used internally (pagy_t). It’s ~18x faster and uses ~10x less memory than the standard i18n gem.

See the i18n extra doc if you need to use the slower standard i18n gem instead.

Configuration

Notice: If your app uses only the default en language or if you use the i18n extra you don’t need to configure anything for this module.

If you need to load different built-in locales, and/or custom dictionary files or even non built-in language and pluralization, you can do it all by passing a few arguments to the Pagy::I18n.load method.

Synopsis

# in the pagy.rb initializer file

# IMPORTANT: use only one load statement or you will get a FrozenError exception

# load the "de" built-in locale:
Pagy::I18n.load(locale: 'de')

# load the "de" locale defined in the custom file at :filepath:
Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml')

# load the "de", "en" and "es" built-in locales:
# the first :locale will be used also as the default locale
Pagy::I18n.load({ locale: 'de' },
                { locale: 'en' },
                { locale: 'es' })

# load the "en" built-in locale, a custom "es" locale, and a totally custom locale complete with the :pluralize proc:
Pagy::I18n.load({ locale: 'en' },
                { locale: 'es',
                  filepath: 'path/to/pagy-es.yml' },
                { locale:    'xyz',  # not built-in
                  filepath:  'path/to/pagy-xyz.yml',
                  pluralize: lambda{ |count| ... } })

Notice: You should use a custom :pluralize proc only for pluralization types not included in the built-in pluralization rules (stored in the Pagy::I18n::P11n::RULE hash).

The :pluralize proc should receive the count as a single argument and should return the plural type string (e.g. something like 'one' or 'other', depending on the passed count).

Setting the request locale in multi-language apps

When you configure multiple locales (i.e. this does not apply to single locale apps), you must also set the locale at each request. You usually do that in the application controller, by checking the :locale param. For example, in a rails app you should do something like:

before_action { @pagy_locale = params[:locale] }

If the @pagy_locale is nil or missing pagy will serve the first locale you set in the configuration.

Adding the model translations

If you use the pagy_info OR pagy_items_selector_js AND you also want to use the specific model names instead of the generic β€œitems” string, THEN you may need to add entries for the models in the pagy dictionary files. For example:

en:
  pagy:
    ...

  # added models strings
  activerecord:
    models:
      product:
        one: Product
        other: Products
      ...

(See also the pagy_info method and How to customize the item name)