Skip to content

Commit

Permalink
Merge branch 'locales-normalization' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Mar 23, 2020
2 parents 00bd312 + 032615c commit c424466
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/locales/pt-br.yml → lib/locales/pt-BR.yml
@@ -1,6 +1,6 @@
# :one_other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)

pt-br:
pt-BR:
pagy:

item_name:
Expand Down
23 changes: 23 additions & 0 deletions lib/locales/sv-SE.yml
@@ -0,0 +1,23 @@
# :one_two_other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)

sv-SE:
pagy:

item_name:
one: "resultat"
two: "resultat"
other: "resultat"

nav:
prev: "‹ Föregående"
next: "Nästa ›"
gap: "…"

info:
no_items: "Inga %{item_name} hittade"
single_page: "Visar <b>%{count}</b> %{item_name}"
multiple_pages: "Visar %{item_name} <b>%{from}-%{to}</b> av <b>%{count}</b> totalt"

combo_nav_js: "Sida %{page_input} av %{pages}"

items_selector_js: "Visa %{items_input} %{item_name} per sida"
2 changes: 1 addition & 1 deletion lib/locales/se.yml → lib/locales/sv.yml
@@ -1,6 +1,6 @@
# :one_two_other pluralization (see https://github.com/ddnexus/pagy/blob/master/lib/locales/utils/p11n.rb)

se:
sv:
pagy:

item_name:
Expand Down
17 changes: 12 additions & 5 deletions lib/locales/utils/i18n.rb
Expand Up @@ -4,15 +4,22 @@

# this file returns the I18n hash used as default alternative to the i18n gem

Hash.new{|h,_| h.first[1]}.tap do |i18n| # first loaded locale used as default
i18n.define_singleton_method(:load) do |*args|
Pagy::DEPRECATED_LOCALES = {'pt-br' =>'pt-BR', 'se' => 'sv-SE'}

Hash.new{|h,_| h.first[1]}.tap do |i18n_hash| # first loaded locale used as default
i18n_hash.define_singleton_method(:load) do |*load_args|
# eval: we don't need to keep the loader proc in memory
eval(Pagy.root.join('locales', 'utils', 'loader.rb').read).call(i18n, *args) #rubocop:disable Security/Eval
eval(Pagy.root.join('locales', 'utils', 'loader.rb').read).call(i18n_hash, *load_args) #rubocop:disable Security/Eval
end
i18n.define_singleton_method(:t) do |locale, path, vars={}|
i18n_hash.define_singleton_method(:t) do |locale, path, vars={}|
if Pagy::DEPRECATED_LOCALES.key?(locale)
new_locale = Pagy::DEPRECATED_LOCALES[locale]
$stderr.puts("WARNING: the Pagy locale '#{locale}' is deprecated; use '#{new_locale}' instead")
locale = new_locale
end
data, pluralize = self[locale]
translate = data[path] || vars[:count] && data[path+=".#{pluralize.call(vars[:count])}"] or return %([translation missing: "#{path}"])
translate.call(vars)
end
i18n.load(locale: 'en')
i18n_hash.load(locale: 'en')
end
5 changes: 5 additions & 0 deletions lib/locales/utils/loader.rb
Expand Up @@ -20,6 +20,11 @@
lambda do |i18n, *args|
i18n.clear
args.each do |arg|
if Pagy::DEPRECATED_LOCALES.key?(arg[:locale])
new_locale = Pagy::DEPRECATED_LOCALES[arg[:locale]]
$stderr.puts("WARNING: the Pagy locale '#{arg[:locale]}' is deprecated; use '#{new_locale}' instead")
arg[:locale] = new_locale
end
arg[:filepath] ||= Pagy.root.join('locales', "#{arg[:locale]}.yml")
arg[:pluralize] ||= plurals[arg[:locale]]
hash = YAML.load(File.read(arg[:filepath], encoding: 'UTF-8')) #rubocop:disable Security/YAMLLoad
Expand Down
17 changes: 9 additions & 8 deletions lib/locales/utils/p11n.rb
Expand Up @@ -60,14 +60,15 @@
# The default pluralization for locales not explicitly listed here
# is the :one_other pluralization proc (used for English)
plurals = Hash.new(p11n[:one_other]).tap do |hash|
hash['id'] = p11n[:other]
hash['fr'] = p11n[:one_upto_two_other]
hash['ja'] = p11n[:other]
hash['ko'] = p11n[:other]
hash['pl'] = p11n[:polish]
hash['ru'] = p11n[:east_slavic]
hash['se'] = p11n[:one_two_other]
hash['tr'] = p11n[:other]
hash['id'] = p11n[:other]
hash['fr'] = p11n[:one_upto_two_other]
hash['ja'] = p11n[:other]
hash['ko'] = p11n[:other]
hash['pl'] = p11n[:polish]
hash['ru'] = p11n[:east_slavic]
hash['sv'] = p11n[:one_two_other]
hash['sv-SE'] = p11n[:one_two_other]
hash['tr'] = p11n[:other]
hash['zh-CN'] = p11n[:other]
hash['zh-HK'] = p11n[:other]
hash['zh-TW'] = p11n[:other]
Expand Down
21 changes: 19 additions & 2 deletions test/pagy/frontend_test.rb
Expand Up @@ -21,7 +21,6 @@
"<nav class=\"pagy-nav pagination\" role=\"navigation\" aria-label=\"pager\"><span class=\"page prev\"><a href=\"/foo?page=2\" rel=\"prev\" aria-label=\"previous\">&lsaquo;&nbsp;Prev</a></span> <span class=\"page\"><a href=\"/foo?page=1\" >1</a></span> <span class=\"page\"><a href=\"/foo?page=2\" rel=\"prev\" >2</a></span> <span class=\"page active\">3</span> <span class=\"page\"><a href=\"/foo?page=4\" rel=\"next\" >4</a></span> <span class=\"page\"><a href=\"/foo?page=5\" >5</a></span> <span class=\"page\"><a href=\"/foo?page=6\" >6</a></span> <span class=\"page next\"><a href=\"/foo?page=4\" rel=\"next\" aria-label=\"next\">Next&nbsp;&rsaquo;</a></span></nav>"
end


it 'renders page 6' do
pagy = Pagy.new count: 103, page: 6
_(view.pagy_nav(pagy)).must_equal \
Expand Down Expand Up @@ -101,7 +100,24 @@
_(view.pagy_t('pagy.item_name', count: 1)).must_equal "Eintrag"
view.instance_variable_set(:'@pagy_locale', 'unknown')
_(view.pagy_t('pagy.item_name', count: 1)).must_equal "Eintrag" # silently serves the first loaded locale
Pagy::I18n.load(locale: 'en') # reset for other tests
Pagy::I18n.load(locale: 'en') # reset for other tests
view.instance_variable_set(:'@pagy_locale', nil) # reset for other tests
end

end

describe "Pagy::I18n deprecation" do

it 'handles deprecated locales' do
_(proc {Pagy::I18n.load({locale: 'se'}, {locale: 'pt-br'})}).must_output '', /^WARNING:/

view.instance_variable_set(:'@pagy_locale', 'se')
_(proc {view.pagy_t('pagy.item_name', count: 1).must_equal 'resultat'}).must_output '', /^WARNING:/

view.instance_variable_set(:'@pagy_locale', 'pt-br')
_(proc {view.pagy_t('pagy.item_name', count: 1).must_equal 'item'}).must_output '', /^WARNING:/

Pagy::I18n.load(locale: 'en') # reset for other tests
view.instance_variable_set(:'@pagy_locale', nil) # reset for other tests
end

Expand All @@ -125,6 +141,7 @@
_(view.pagy_info(Pagy.new count: 100, i18n_key: 'pagy.info.product', page: 3)).must_equal "Displaying Products <b>41-60</b> of <b>100</b> in total"
Pagy::I18n.load(locale: 'en') # reset for other tests
end

end

describe '#pagy_url_for' do
Expand Down

0 comments on commit c424466

Please sign in to comment.