From 6a31db64e979e46c5245d9afd037bffc430dd19d Mon Sep 17 00:00:00 2001 From: David Wessman Date: Fri, 20 Mar 2020 06:35:17 +0100 Subject: [PATCH 1/5] Swedish locale: add sv-SE, rename se -> sv (#226) - The Swedish locale is referred to as sv in the I18n-gem: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/sv.yml - It can also be referred to as sv-SE. --- lib/locales/sv-SE.yml | 23 +++++++++++++++++++++++ lib/locales/{se.yml => sv.yml} | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 lib/locales/sv-SE.yml rename lib/locales/{se.yml => sv.yml} (99%) diff --git a/lib/locales/sv-SE.yml b/lib/locales/sv-SE.yml new file mode 100644 index 000000000..87d69ea93 --- /dev/null +++ b/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 %{count} %{item_name}" + multiple_pages: "Visar %{item_name} %{from}-%{to} av %{count} totalt" + + combo_nav_js: "Sida %{page_input} av %{pages}" + + items_selector_js: "Visa %{items_input} %{item_name} per sida" diff --git a/lib/locales/se.yml b/lib/locales/sv.yml similarity index 99% rename from lib/locales/se.yml rename to lib/locales/sv.yml index f6cc749c3..86da347e0 100644 --- a/lib/locales/se.yml +++ b/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: From a77dfcf04f352d4650468f02af34f7326d699ab2 Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Fri, 20 Mar 2020 12:39:07 +0700 Subject: [PATCH 2/5] fix for sv and sv-SE pluralization --- lib/locales/utils/p11n.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/locales/utils/p11n.rb b/lib/locales/utils/p11n.rb index e6ad4ccb0..ad5d8c150 100644 --- a/lib/locales/utils/p11n.rb +++ b/lib/locales/utils/p11n.rb @@ -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] From e1bd70c38b8c8d8c7b1e24c1cd459885dc90b3c2 Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Fri, 20 Mar 2020 12:41:09 +0700 Subject: [PATCH 3/5] renamed pt-br -> pt-BR --- lib/locales/{pt-br.yml => pt-BR.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename lib/locales/{pt-br.yml => pt-BR.yml} (98%) diff --git a/lib/locales/pt-br.yml b/lib/locales/pt-BR.yml similarity index 98% rename from lib/locales/pt-br.yml rename to lib/locales/pt-BR.yml index e9de41967..0638a22da 100644 --- a/lib/locales/pt-br.yml +++ b/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: From ebd6151e73b3ccd8441d71463b0a0664129b56f1 Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Fri, 20 Mar 2020 12:42:10 +0700 Subject: [PATCH 4/5] added deprecation remapping --- lib/locales/utils/i18n.rb | 17 ++++++++++++----- lib/locales/utils/loader.rb | 5 +++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/locales/utils/i18n.rb b/lib/locales/utils/i18n.rb index 0b5e35ad7..0ebead539 100644 --- a/lib/locales/utils/i18n.rb +++ b/lib/locales/utils/i18n.rb @@ -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 diff --git a/lib/locales/utils/loader.rb b/lib/locales/utils/loader.rb index 942dfb827..a74e0f34b 100644 --- a/lib/locales/utils/loader.rb +++ b/lib/locales/utils/loader.rb @@ -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 From 032615ce9f3683376d2b5615185edd7d98744ed4 Mon Sep 17 00:00:00 2001 From: Domizio Demichelis Date: Sun, 22 Mar 2020 15:41:58 +0700 Subject: [PATCH 5/5] added Pagy::I18n deprecation tests --- test/pagy/frontend_test.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/test/pagy/frontend_test.rb b/test/pagy/frontend_test.rb index 72af9a9a8..1fd752362 100644 --- a/test/pagy/frontend_test.rb +++ b/test/pagy/frontend_test.rb @@ -21,7 +21,6 @@ "" end - it 'renders page 6' do pagy = Pagy.new count: 103, page: 6 _(view.pagy_nav(pagy)).must_equal \ @@ -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 @@ -125,6 +141,7 @@ _(view.pagy_info(Pagy.new count: 100, i18n_key: 'pagy.info.product', page: 3)).must_equal "Displaying Products 41-60 of 100 in total" Pagy::I18n.load(locale: 'en') # reset for other tests end + end describe '#pagy_url_for' do