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: 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: 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 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] 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