Bug: Pagy::I18n.locale should work with Symbol values
bug
### 👀 Before submitting...
- [x] I upgraded to pagy version 43.2.4
- [x] I asked [Pagy AI](https://gurubase.io/g/pagy) (Good starting point for further investigation).
- [x] I searched through the [Documentation](https://ddnexus.github.io/pagy/), [Issues](https://github.com/ddnexus/pagy/issues), and [Q&A](https://github.com/ddnexus/pagy/discussions/categories/q-a)
### 🧐 REQUIREMENTS
- [x] I am providing a VALID code file that confirms the bug
- [x] I am NOT posting any of the USELESS THINGS listed above
- [x] I am aware that this issue will be automatically closed if the code file is missing or INVALID
### 💬 Description
## Problem
When setting `Pagy::I18n.locale` with a symbol (e.g., `:en`), an error occurs when using `@pagy.info_tag`.
The error message is:
```
NoMethodError: undefined method '[]' for nil
```
## Environment
- Pagy version: 43.2.4
- Rails version: 8.1.2
- Ruby version: 3.4.8
## Steps to Reproduce
1. Configure Rails with a symbol locale:
```ruby
# config/application.rb
config.i18n.default_locale = :en
```
2. Set `Pagy::I18n.locale` using `I18n.default_locale`:
```ruby
# app/controllers/application_controller.rb
include Pagy::Method
before_action { Pagy::I18n.locale = I18n.default_locale }
```
3. Use `@pagy.info_tag` in a view:
```erb
<%== @pagy.info_tag %>
```
4. The error occurs: `NoMethodError: undefined method '[]' for nil`
In most cases, `I18n.default_locale` in Rails is typically represented as a symbol (e.g., `:en`).
While the [Pagy I18n Documentation](https://ddnexus.github.io/pagy/resources/i18n/#configuration-for-multilingual-applications) mentions this configuration for "multiple locales", this issue also affects single-locale applications.
In applications that support a single language, rather than receiving the locale from parameters like `params[:locale]`, implementations tend to reference the default value of `I18n.default_locale`.
Therefore, I expect that symbols are passed to `Pagy::I18n.locale` in many cases as a natural flow of the code.
The documentation shows setting `Pagy::I18n.locale = params[:locale]`, which typically returns a string, but doesn't mention the incompatibility with symbol locales often used in Rails applications.
## Expected Behavior
`Pagy::I18n.locale` should accept both String and Symbol values, automatically converting symbols to strings internally.
## Actual Behavior
`Pagy::I18n.locale` only accepts String values. When a Symbol is passed, the locale dictionary lookup fails, resulting in `nil` being returned, which then causes `undefined method '[]' for nil` errors when trying to access translation keys.
## Workaround
Currently, we need to explicitly convert the symbol to a string:
```ruby
before_action { Pagy::I18n.locale = I18n.default_locale.to_s }
```
However, this requires an explicit `to_s` conversion and is inconvenient when `I18n.default_locale` (which returns a symbol) is used directly.
## Proposed Solution
Modify the `locale=` method in `Pagy::I18n` to accept both String and Symbol values, converting symbols to strings internally.
https://github.com/ddnexus/pagy/blob/43.2.4/gem/lib/pagy/modules/i18n/i18n.rb#L15-L17
---
Please note that my understanding of the Pagy implementation and documentation may not be entirely accurate.
If there are any misunderstandings in this issue, I would appreciate your guidance.
0 条评论