Skip to content

Commit

Permalink
WIP: Better calendar localization
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jan 23, 2025
1 parent 5143268 commit 8a7052f
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 86 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-10.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Updated the support for the pagy helpers and keynav pagination. Added the plain
- If you used the `:params` variable set to a lambda, ensure that it modifies the passed `query_params` directly. The returned
value is now ignored for a sligtly better performance.

#### Extras Replacement
#### Extras Changes

##### `arel`, `array`, `boostrap`, `bulma`, `calendar`, `countless`, `pagy`

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ group :test do
gem 'mutex_m' # for RubyMine
gem 'oj', require: false # false is for testing with or without it
gem 'rack'
gem 'rails-i18n'
gem 'rematch'
gem 'rubocop'
gem 'rubocop-minitest'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ GEM
rails-html-sanitizer (1.6.2)
loofah (~> 2.21)
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
rails-i18n (8.0.1)
i18n (>= 0.7, < 2)
railties (>= 8.0.0, < 9)
railties (8.0.1)
actionpack (= 8.0.1)
activesupport (= 8.0.1)
Expand Down Expand Up @@ -365,6 +368,7 @@ DEPENDENCIES
rack
rackup
rails (~> 8.0)
rails-i18n
rake
reline
rematch
Expand Down
35 changes: 24 additions & 11 deletions gem/config/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Pagy initializer file (9.3.3)

##### Pagy::DEFAULT #######################

#
# Customizing the static and frozen Pagy::DEFAULT is NOT SUPPORTED since version 10.0.0.
# Pass the variables to the constructor, or pass your own PAGY_DEFAULT hash.
# For example:
Expand All @@ -12,8 +12,8 @@
# pagy_offset(collection, **PAGY_DEFAULT, ...)


##### Extras #######################

##### Extras ##############################
#
# The extras are almost all converted to autoloaded mixins, or integrated in the core code at zero-cost.
# You can use the methods that you need, and they will just work without the need of any explicit `require`.
#
Expand All @@ -26,24 +26,22 @@
# See https://ddnexus.github.io/pagy/docs/extras/gearbox
# require 'pagy/extras/gearbox'

# Size extra: Enable the Array type for the `:size` variable (e.g. `size: [1,4,4,1]`)
# See https://ddnexus.github.io/pagy/docs/extras/size
# require 'pagy/extras/size' # must be required before the other extras

# I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory
# than the default pagy internal i18n (see below)
# than the default pagy internal i18n (configurable below)
# See https://ddnexus.github.io/pagy/docs/extras/i18n
# require 'pagy/extras/i18n'


# Size extra: Enable the Array type for the `:size` variable (e.g. `size: [1,4,4,1]`)
# See https://ddnexus.github.io/pagy/docs/extras/size
# require 'pagy/extras/size' # must be required before the other extras


##### Pagy::I18n configuration #######################

#
# Pagy internal I18n: ~18x faster using ~10x less memory than the i18n gem
# See https://ddnexus.github.io/pagy/docs/api/i18n
# Notice: No need to configure anything in this section if your app uses only "en"
# or if you use the i18n extra below
# or if you use the i18n extra (configurable above).
#
# Examples:
# load the "de" built-in locale:
Expand All @@ -66,3 +64,18 @@
# { locale: 'xyz', # not built-in
# filepath: 'path/to/pagy-xyz.yml',
# pluralize: lambda{ |count| ... } )


##### Calendar Localization Besides :en ###############
#
# The calendar localization data beside :en (which is included), is provided by the rails-i18n gem,
# which should be requested/added by your Gemfile.
# Notice that the calendar localization does not require you to use the pagy i18n extra,
# which is all about translation and not localization.
# Uncomment the following line to enable it, regardless if you use the I18n extra or not:

# Pagy::Offset::Calendar::Unit.prepend(Module.new { def localize(...) = I18n.l(...) })

# Customize with your locales, and uncomment the following lines only if you don't use Rails:
# path = Pathname.new(Gem.loaded_specs['rails-i18n'].full_gem_path)
# I18n.load_path += %i[en de].map { |locale| path.join("rails/locale/#{locale}.yml") }
2 changes: 1 addition & 1 deletion gem/lib/pagy/offset/calendar/quarter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Quarter < Unit
def label(page: @page, **opts)
starting_time = starting_time_for(page.to_i) # page could be a string
opts[:format] = (opts[:format] || @vars[:format]).gsub('%q') { (starting_time.month / 3.0).ceil }
localize(starting_time, opts)
localize(starting_time, **opts)
end

protected
Expand Down
13 changes: 8 additions & 5 deletions gem/lib/pagy/offset/calendar/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def check_overflow
# The label for any page (it can pass along the I18n gem opts when it's used with the i18n extra)
def label(page: @page, **opts)
opts[:format] ||= @vars[:format]
localize(starting_time_for(page.to_i), opts) # page could be a string
localize(starting_time_for(page.to_i), **opts) # page could be a string
end

def calendar? = true
Expand Down Expand Up @@ -81,10 +81,13 @@ def assign_unit_vars
&& @ending.is_a?(ActiveSupport::TimeWithZone) && @starting <= @ending
end

# Apply the strftime format to the time (overridden by the i18n extra when localization is required)
# Calendar overriding for localization (see also the block in the calendar section of the config/pagy.rb initializer)
def localize(time, opts)
defined?(::Pagy::I18nExtra) ? ::I18n.l(time, **opts) : time.strftime(opts[:format])
# Apply the strftime format to the time.
# IMPORTANT: If you need localization of the Calendar beside :en, you must use the rails-I18n gem.
# In order to enable it, uncomment the specific lines at the end of the config/pagy.rb initializer.
# Notice that the calendar localization does not require you to use the pagy i18n extra,
# which is all about translation and not localization.
def localize(time, **opts)
time.strftime(opts[:format])
end

# Number of time units to offset from the @initial time, in order to get the ordered starting time for the page.
Expand Down
60 changes: 0 additions & 60 deletions test/pagy/extras/i18n/calendar_extra_test.rb

This file was deleted.

6 changes: 0 additions & 6 deletions test/pagy/extras/i18n/extra_test.rb.yaml

This file was deleted.

44 changes: 44 additions & 0 deletions test/pagy/extras/i18n_calendar_localization_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require_relative '../../test_helper'
require 'rails-i18n'
require_relative '../../mock_helpers/app' # load after the extra

###### Pagy initializer block ###########
# module LocalizePagyCalendar
# def localize(...) = ::I18n.l(...)
# end
# Pagy::Offset::Calendar::Unit.prepend LocalizePagyCalendar
# module LocalizePagyCalendar
# def localize(...) = ::I18n.l(...)
# end
Pagy::Offset::Calendar::Unit.prepend(Module.new { def localize(...) = I18n.l(...) })

path = Pathname.new(Gem.loaded_specs['rails-i18n']&.full_gem_path)
I18n.load_path += %i[en de].map { |locale| path.join("rails/locale/#{locale}.yml") }
#########################################

Time.zone = 'EST'
Date.beginning_of_week = :sunday

## Test not needed: Now it's a manual patch in the pagy.rb initializer
describe 'Calendar with I18n.l' do
it 'works in :en' do
pagy = Pagy::Offset::Calendar.send(:create, :month,
period: [Time.zone.local(2021, 10, 21, 13, 18, 23, 0),
Time.zone.local(2023, 11, 13, 15, 43, 40, 0)],
page: 3, format: '%B, %A')
_(pagy.label).must_equal "December, Wednesday"
_(pagy.label(locale: :de)).must_equal "Dezember, Mittwoch"
_(pagy.label(format: '%b')).must_equal "Dec"
_(pagy.label(format: '%b', locale: :de)).must_equal "Dez"
_(pagy.label(page: 5)).must_equal "February, Tuesday"
_(pagy.label(page: 5, locale: :de)).must_equal "Februar, Dienstag"
I18n.locale = :de
_(pagy.label).must_equal "Dezember, Mittwoch"
_(pagy.label(format: '%b')).must_equal "Dez"
_(pagy.label(page: 5)).must_equal "Februar, Dienstag"
_(pagy.label(page: 5, format: '%b')).must_equal "Feb"
I18n.locale = :en
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative '../../../test_helper'
require_relative '../../../mock_helpers/app'
require_relative '../../test_helper'
require_relative '../../mock_helpers/app'
require 'i18n'
require 'pagy/extras/i18n'

Expand Down
File renamed without changes.

0 comments on commit 8a7052f

Please sign in to comment.