Skip to content

Commit

Permalink
normalization of i18n naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Mar 8, 2021
1 parent b3f78b8 commit aecb565
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/api/frontend.md
Expand Up @@ -152,7 +152,7 @@ If you need to add some HTML attribute to the page links, you can pass some extr
**WARNING**: we use only strings for performance, so the attribute strings get concatenated level after level, but not merged!
Be careful not to pass the same attribute at different levels multiple times. That would generate a duplicated HTML attribute which is illegal html (although handled by all mayor browsers by ignoring all the duplicates but the first)

### pagy_t(path, vars={})
### pagy_t(key, vars={})

This method is similar to the `I18n.t` and its equivalent rails `t` helper. It is called internally (from helpers and templates) in order to get the interpolated strings out of a YAML dictionary file. _(see I18n below)_

Expand Down
6 changes: 3 additions & 3 deletions lib/locales/utils/i18n.rb
Expand Up @@ -9,10 +9,10 @@
# eval: we don't need to keep the loader proc in memory
eval(Pagy.root.join('locales', 'utils', 'loader.rb').read).call(i18n_hash, *load_args) #rubocop:disable Security/Eval
end
i18n_hash.define_singleton_method(:t) do |locale, path, vars={}|
i18n_hash.define_singleton_method(:t) do |locale, key, **opts|
data, pluralize = self[locale]
translate = data[path] || vars[:count] && data[path+=".#{pluralize.call(vars[:count])}"] or return %([translation missing: "#{path}"])
translate.call(vars)
translate = data[key] || opts[:count] && data[key+=".#{pluralize.call(opts[:count])}"] or return %([translation missing: "#{key}"])
translate.call(opts)
end
i18n_hash.load(locale: 'en')
end
6 changes: 3 additions & 3 deletions lib/pagy/frontend.rb
Expand Up @@ -48,10 +48,10 @@ def pagy_nav(pagy)

# Return examples: "Displaying items 41-60 of 324 in total" of "Displaying Products 41-60 of 324 in total"
def pagy_info(pagy, item_name=nil)
path = if (count = pagy.count) == 0 ; 'pagy.info.no_items'
key = if (count = pagy.count) == 0 ; 'pagy.info.no_items'
else pagy.pages == 1 ? 'pagy.info.single_page' : 'pagy.info.multiple_pages'
end
pagy_t(path, item_name: item_name || pagy_t(pagy.vars[:i18n_key], count: count), count: count, from: pagy.from, to: pagy.to)
pagy_t(key, item_name: item_name || pagy_t(pagy.vars[:i18n_key], count: count), count: count, from: pagy.from, to: pagy.to)
end

# Returns a performance optimized proc to generate the HTML links
Expand All @@ -66,7 +66,7 @@ def pagy_link_proc(pagy, link_extra='')

# Similar to I18n.t: just ~18x faster using ~10x less memory
# (@pagy_locale explicitly initilized in order to avoid warning)
def pagy_t(path, vars={}) = Pagy::I18n.t(@pagy_locale||=nil, path, vars)
def pagy_t(key, **opts) = Pagy::I18n.t(@pagy_locale||=nil, key, **opts)

end
end
2 changes: 1 addition & 1 deletion test/pagy/extras/i18n_test.rb
Expand Up @@ -35,7 +35,7 @@
_(view.pagy_info(Pagy.new count: 100, page: 3)).must_equal "Displaying items <b>41-60</b> of <b>100</b> in total"
end

it 'renders with existing i18n path' do
it 'renders with existing i18n key' do
::I18n.locale = 'en'
custom_dictionary = File.join(File.dirname(__FILE__), 'i18n.yml')
::I18n.load_path += [custom_dictionary]
Expand Down
6 changes: 3 additions & 3 deletions test/pagy/frontend_test.rb
Expand Up @@ -66,7 +66,7 @@
_(view.pagy_t('pagy.info.multiple_pages', item_name: 'Products', count: 300, from: 101, to: 125)).must_equal "Displaying Products <b>101-125</b> of <b>300</b> in total"
end

it 'handles missing paths' do
it 'handles missing keys' do
_(view.pagy_t('pagy.nav.not_here')).must_equal '[translation missing: "pagy.nav.not_here"]'
_(view.pagy_t('pagy.nav.gap.not_here')).must_equal '[translation missing: "pagy.nav.gap.not_here"]'
end
Expand Down Expand Up @@ -108,14 +108,14 @@

describe "#pagy_info" do

it 'renders without i18n path' do
it 'renders without i18n key' do
_(view.pagy_info(Pagy.new count: 0)).must_equal "No items found"
_(view.pagy_info(Pagy.new count: 1)).must_equal "Displaying <b>1</b> item"
_(view.pagy_info(Pagy.new count: 13)).must_equal "Displaying <b>13</b> items"
_(view.pagy_info(Pagy.new count: 100, page: 3)).must_equal "Displaying items <b>41-60</b> of <b>100</b> in total"
end

it 'renders with existing i18n path' do
it 'renders with existing i18n key' do
Pagy::I18n['en'][0]['pagy.info.product.one'] = lambda{|_| 'Product'}
Pagy::I18n['en'][0]['pagy.info.product.other'] = lambda{|_| 'Products'}
_(view.pagy_info(Pagy.new count: 0, i18n_key: 'pagy.info.product')).must_equal "No Products found"
Expand Down

0 comments on commit aecb565

Please sign in to comment.