route_translator 15.0.0 → 15.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a393716835c2c542d7f158782dcf1dcbaf163185bb4a1f85ff1be5507accbbf
4
- data.tar.gz: 9ea86dcbebec6a9cea19b280da5a821cfccd6951e5d4c5b79ca43582053c8869
3
+ metadata.gz: 5cca7e784d193668fe69a3b9eae57116700bc4699c1930a79f720de4ce206fbe
4
+ data.tar.gz: a852fa9a6c8c00bad0223199b1e3ec8159f5bf70774a5bf7b531519c25ccaf4c
5
5
  SHA512:
6
- metadata.gz: b1fe1869d56070ea8de79618f8c6cd8fe5e6360b6be50637ac94ac9a13db13d6ce44f807a4745b4973cef5126049b1d7314d2c46401d7aa6af1d081cb63c9f84
7
- data.tar.gz: d91df79e0dabed0f438757b364e467d85c1d1691c324e9098b5c1446805e6735a594e5d425d48345c47dcdb01f6438a30358a09a52195de93a46c425b5ccc944
6
+ metadata.gz: d85e32c58543f4ef062777a7257f5377f661acc37e4a003c05076824d487020f0c70742c7256262b824596b4247f88fab3ab3ae2f12edccd1610c3cff3236091
7
+ data.tar.gz: bc6d14c007375d9e0196f965781431c890793588293a23fe4e8ea95c669eca7afab4488e9a3bee3b0f95be72c044081b68fc1f0c5da4651ad68eca5eac64979f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 15.1.0 / 2025-06-15
4
+
5
+ * [BUGFIX] Ensure `@localized` is always reset after yield ([#333](https://github.com/enriclluelles/route_translator/issues/333))
6
+ * [ENHANCEMENT] Optimize host locale detection ([#337](https://github.com/enriclluelles/route_translator/pull/337))
7
+ * [ENHANCEMENT] Add `locale_from_request` method ([#337](https://github.com/enriclluelles/route_translator/pull/337))
8
+
3
9
  ## 15.0.0 / 2025-06-12
4
10
 
5
11
  * [FEATURE] Drop Ruby < 3.1 support
data/README.md CHANGED
@@ -385,6 +385,39 @@ scope ':country/:locale' do
385
385
  end
386
386
  ```
387
387
 
388
+ ## Using RouteTranslator with Devise
389
+
390
+ When using RouteTranslator with Devise, you may notice that some authentication-related flash messages (such as errors after failed logins) are shown in the wrong language. This happens because Devise builds some messages in middleware, after your controller’s actions have completed. If you use RouteTranslator’s `around_action` (or legacy `around_filter`) to set the locale, the locale is reset after the controller yields, and middleware like Devise will see the default locale.
391
+
392
+ This only affects you if Devise is mounted inside a localized block (for example, `localized { devise_for :users }`). If you do not use localized Devise routes, you do not need to change anything.
393
+
394
+ To ensure Devise’s middleware uses the correct locale, some users suggest replacing RouteTranslator’s `around_action` with a `before_action`. However, this is discouraged, as it can leave your application in the wrong locale after the request and may cause subtle bugs.
395
+
396
+ A better approach is to explicitly set the locale for Devise’s failure responses. Starting from Devise version 4.9.4, you can customize how Devise determines the locale for error messages, thanks to the ability to override the `i18n_locale` method in the failure app. This allows you to set the locale for Devise’s middleware without changing RouteTranslator’s recommended usage.
397
+
398
+ Here’s an example:
399
+
400
+ ```rb
401
+ # config/initializers/devise.rb
402
+
403
+ Devise.setup do |config|
404
+ # ...
405
+
406
+ ActiveSupport.on_load(:devise_failure_app) do
407
+ def i18n_locale
408
+ RouteTranslator.locale_from_request(request)
409
+ end
410
+ end
411
+ end
412
+ ```
413
+
414
+ ### Summary
415
+
416
+ - You only need this workaround if Devise is mounted in a localized block.
417
+ - Do not replace RouteTranslator’s `around_action` with a `before_action`.
418
+ - Instead, set the locale for Devise failure responses as shown above.
419
+ - The solution above requires Devise version 4.9.4 or higher, which allows customizing the `i18n_locale` method in the failure app.
420
+
388
421
  ## Testing
389
422
  Testing your controllers with routes-translator is easy, just add a locale parameter as `String` for your localized routes. Otherwise, an `ActionController::UrlGenerationError` will raise.
390
423
 
@@ -9,7 +9,8 @@ module RouteTranslator
9
9
  private
10
10
 
11
11
  def set_locale_from_url
12
- locale_from_url = RouteTranslator.locale_from_params(params) || RouteTranslator::Host.locale_from_host(request.host)
12
+ locale_from_url = RouteTranslator.locale_from_request(request)
13
+
13
14
  if locale_from_url
14
15
  old_locale = I18n.locale
15
16
  I18n.locale = locale_from_url
@@ -8,6 +8,7 @@ module ActionDispatch
8
8
  def localized
9
9
  @localized = true
10
10
  yield
11
+ ensure
11
12
  @localized = false
12
13
  end
13
14
 
@@ -18,11 +18,11 @@ module RouteTranslator
18
18
  module_function
19
19
 
20
20
  def locale_from_host(host)
21
- locales = RouteTranslator.config.host_locales.each_with_object([]) do |(pattern, locale), result|
22
- result << locale.to_sym if host&.match?(regex_for(pattern))
23
- end
24
- locales &= I18n.available_locales
25
- locales.first&.to_sym
21
+ available_locales = I18n.available_locales
22
+
23
+ RouteTranslator.config.host_locales.find do |pattern, locale|
24
+ host&.match?(regex_for(pattern)) && available_locales.include?(locale&.to_sym)
25
+ end&.last&.to_sym
26
26
  end
27
27
 
28
28
  def lambdas_for_locale(locale)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RouteTranslator
4
- VERSION = '15.0.0'
4
+ VERSION = '15.1.0'
5
5
  end
@@ -81,6 +81,10 @@ module RouteTranslator
81
81
  locale if I18n.available_locales.include?(locale)
82
82
  end
83
83
 
84
+ def locale_from_request(request)
85
+ locale_from_params(request.params) || Host.locale_from_host(request.host)
86
+ end
87
+
84
88
  def deprecator
85
89
  @deprecator ||= ActiveSupport::Deprecation.new(RouteTranslator::VERSION, 'RouteTranslator')
86
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: route_translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 15.0.0
4
+ version: 15.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geremia Taglialatela
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
- rubygems_version: 3.6.7
91
+ rubygems_version: 3.6.9
92
92
  specification_version: 4
93
93
  summary: Translate your Rails routes in a simple manner
94
94
  test_files: []