Skip to content

Commit

Permalink
Refactoring I18N to fix #39:
Browse files Browse the repository at this point in the history
- moved I18N from Pagy to Pagy::Frontend
- I18N_DATA moved to Pagy::Frontend::I18N[:data]
- I18N[:file] removed
- added I18N.load_file method
- updated initializer and doc
  • Loading branch information
ddnexus committed Jun 1, 2018
1 parent 2a1499e commit d85791e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
19 changes: 9 additions & 10 deletions docs/api/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,20 @@ By default, the `pagy_t` method uses the pagy implementation of I18n, which does

If you need full blown I18n, you should require the `i18n` extra, which will override the `pagy_t` method to use directly `::I18n.t`.

### I18n Global Variables
### Pagy::Frontend::I18N Constant

These are the `Pagy::I18N` globally accessible variables used to configure the pagy I18n implementation. They have no effect if you use the `i18n` extra (which uses the `I18n.t` method directly). They are not merged with the pagy object and used only at require time.
The `Pagy::Frontend::I18N` constant is the core of the pagy I18n implementation. It has no effect if you use the `i18n` extra (which uses the `I18n.t` method directly). It allows to control the dictionary file to load and the pluralization proc.

| Variable | Description | Default |
| ---------- | -------------------------------------------------------------- | :------------------------------------------- |
| `:file` | The I18n YAML file | `Pagy.root.join('locales', 'pagy.yml').to_s` |
| `:plurals` | The proc that returns the plural key based on the passed count | `Proc` for English |
#### Pagy::Frontend::I18N.load_file(file)

#### Pagy::I18N[:file]
This method allow to load a custom dictionary file, different from `Pagy.root.join('locales', 'pagy.yml')`. If the `i18n` extra is used it has no effect. It is tipically used in the initializer file. For example:

This variable contains the path of the YAML file to load: set this variable only if you moved the file from `Pagy.root.join('locales', 'pagy.yml')`.
```ruby
Pagy::Frontend::I18N.load_file('path/to/dictionary.yml')
```

#### Pagy::I18N[:plurals]
#### Pagy::Frontend::I18N[:plurals]

This variable controls the internal pluralization. If `pagy_t` is defined to use `I18n.t` it has no effect.
This variable controls the internal pluralization. If the `i18n` extra is used it has no effect.

By default the variable is set to a proc that receives the `count` as the single argument and returns the plural type string (e.g. something like `'zero'`, `'one'` or `'other'`, depending on the count). You should customize it only for pluralization types different than English.
3 changes: 0 additions & 3 deletions lib/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ def self.root; Pathname.new(__FILE__).dirname end
# default core vars
VARS = { page:1, items:20, outset:0, size:[1,4,4,1], page_param: :page, params: {} }

# default I18n vars
zero_one = [:zero, :one] ; I18N = { file: Pagy.root.join('locales', 'pagy.yml').to_s, plurals: -> (c) {(zero_one[c] || :other).to_s.freeze} }

attr_reader :count, :page, :items, :vars, :pages, :last, :offset, :from, :to, :prev, :next

# merge and validate the options, do some simple aritmetic and set the instance variables
Expand Down
8 changes: 4 additions & 4 deletions lib/pagy/extras/initializer_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
# Pagy::VARS[:breakpoints] = { 0 => [1,2,2,1], 350 => [2,3,3,2], 550 => [3,4,4,3] } # example of width/size pairs


# I18n Variables
# See https://ddnexus.github.io/pagy/api/frontend#pagy_tpath-vars
# Pagy::I18N[:file] = Pagy.root.join('locales', 'pagy.yml').to_s # default
# Pagy::I18N[:plurals] = -> (c) {([:zero, :one][c] || :other).to_s # default
# Pagy::Frontend::I18N Constant
# See https://ddnexus.github.io/pagy/api/frontend#i18n
# Pagy::Frontend::I18N[:plurals] = -> (c) {([:zero, :one][c] || :other).to_s # default
# Pagy::Frontend::I18N.load_file('path/to/dictionary.yml') # load a custom file


# Rails: extras assets path required by compact or responsive extras
Expand Down
8 changes: 5 additions & 3 deletions lib/pagy/frontend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ def pagy_link_proc(pagy, lx=''.freeze) # "lx" means "link extra"
end


# load data from the first locale in the file
I18N_DATA = YAML.load_file(I18N[:file]).first[1].freeze
# Pagy::Frontend::I18N constant
zero_one = [:zero, :one]; I18N = { plurals: -> (c) {(zero_one[c] || :other).to_s.freeze}, data: {}}
def I18N.load_file(file) I18N[:data].replace(YAML.load_file(file).first[1]) end
I18N[:data] = I18N.load_file(Pagy.root.join('locales', 'pagy.yml'))

# Similar to I18n.t for interpolation and pluralization but without translation
# Use only for single-language apps: it is specialized for pagy and 5x faster than I18n.t
# See also https://ddnexus.github.io/pagy/extras/i18n to use the standard I18n gem instead
def pagy_t(path, vars={})
value = I18N_DATA.dig(*path.to_s.split('.'.freeze)) or return %(translation missing: "#{path}")
value = I18N[:data].dig(*path.to_s.split('.'.freeze)) or return %(translation missing: "#{path}")
if value.is_a?(Hash)
vars.key?(:count) or return value
plural = I18N[:plurals].call(vars[:count])
Expand Down
6 changes: 3 additions & 3 deletions test/pagy/frontend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def test_link_extras

describe "#pagy_t" do
def test_data
assert_equal "‹ Prev", Pagy::Frontend::I18N_DATA['pagy']['nav']['prev']
assert_equal "…", Pagy::Frontend::I18N_DATA['pagy']['nav']['gap']
assert_equal "‹ Prev", Pagy::Frontend::I18N[:data]['pagy']['nav']['prev']
assert_equal "…", Pagy::Frontend::I18N[:data]['pagy']['nav']['gap']
end

def test_translation
Expand Down Expand Up @@ -161,7 +161,7 @@ def test_render_info_no_118n_key
end

def test_render_info_with_existing_118n_key
Pagy::Frontend::I18N_DATA['pagy']['info']['product'] = { 'zero' => 'Products',
Pagy::Frontend::I18N[:data]['pagy']['info']['product'] = { 'zero' => 'Products',
'one' => 'Product',
'other' => 'Products' }
pagy = Pagy.new count: 0, item_path: 'pagy.info.product'
Expand Down

0 comments on commit d85791e

Please sign in to comment.