route_translator 14.2.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: 133d2558351805c54d4d96231a01b9b1c4e5137a42895e8c1d3b2eaff55d03c0
4
- data.tar.gz: 648a700ae5378e5e11b94034ae5f43808519cf9f931f52d49c14596cca3c6337
3
+ metadata.gz: 5cca7e784d193668fe69a3b9eae57116700bc4699c1930a79f720de4ce206fbe
4
+ data.tar.gz: a852fa9a6c8c00bad0223199b1e3ec8159f5bf70774a5bf7b531519c25ccaf4c
5
5
  SHA512:
6
- metadata.gz: a3675a5b82eb29c7436d881718fff7cb82452266a40f7c6d6053dd7f912bab544fd4773c2e8c7ce20bfbd76643d72c5be4ab037e3990ddb84ab4b2c4d20d8cb8
7
- data.tar.gz: 6cba258743408f08ad05e71d05693af3b29740675f093a047990df258904c539400229c7b74c1c5f4282b26a1eefde1cf4268f9b5b1515f3959ef1206467bd23
6
+ metadata.gz: d85e32c58543f4ef062777a7257f5377f661acc37e4a003c05076824d487020f0c70742c7256262b824596b4247f88fab3ab3ae2f12edccd1610c3cff3236091
7
+ data.tar.gz: bc6d14c007375d9e0196f965781431c890793588293a23fe4e8ea95c669eca7afab4488e9a3bee3b0f95be72c044081b68fc1f0c5da4651ad68eca5eac64979f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
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
+
9
+ ## 15.0.0 / 2025-06-12
10
+
11
+ * [FEATURE] Drop Ruby < 3.1 support
12
+ * [BUGFIX] POTENTIAL BREAKING CHANGE: Route helper locale parameters now work without host_locales configuration ([#329](https://github.com/enriclluelles/route_translator/pull/329))
13
+
3
14
  ## 14.2.0 / 2024-11-08
4
15
 
5
16
  * [FEATURE] Rails 8.0 compatibility
data/README.md CHANGED
@@ -275,7 +275,7 @@ end
275
275
  | `generate_unlocalized_routes` | Add translated routes without deleting original unlocalized versions. **Note:** Autosets `force_locale` to `true` | `false` |
276
276
  | `generate_unnamed_unlocalized_routes` | Add the behavior of `force_locale`, but with a named default route which behaves as if `generate_unlocalized_routes` was `true`. `root_path` will redirect to `/en` or `/es`, depending on the value of `I18n.locale` | `false` |
277
277
  | `hide_locale` | Force the locale to be hidden on generated route paths | `false` |
278
- | `host_locales` | Set `I18n.locale` based on `request.host`. Useful for apps accepting requests from more than one domain. See below for more details | `{}` |
278
+ | `host_locales` | Set `I18n.locale` based on `request.host`. Useful for apps accepting requests from more than one domain. The key is a host pattern (supports wildcards for domains, subdomains, and TLDs), and the value is the locale symbol or string to use. See below for more details and examples. | `{}` |
279
279
  | `locale_param_key` | The param key used to set the locale to the newly generated routes | `:locale` |
280
280
  | `locale_segment_proc` | The locale segment of the url will by default be `locale.to_s.downcase`. You can supply your own mechanism via a Proc that takes `locale` as an argument, e.g. `->(locale) { locale.to_s.upcase }` | `false` |
281
281
 
@@ -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
 
@@ -31,7 +31,10 @@ RouteTranslator.config do |config|
31
31
  # config.hide_locale = false
32
32
 
33
33
  # Set `I18n.locale` based on `request.host`. Useful for apps accepting
34
- # requests from more than one domain. See Readme for more details
34
+ # requests from more than one domain. This option expects a hash where each
35
+ # key is a host pattern (as a String, supporting `*` wildcards for subdomains
36
+ # or TLDs), and each value is a locale (as a Symbol or String).
37
+ # See the README for more details.
35
38
  # config.host_locales = {}
36
39
 
37
40
  # The param key used to set the locale to the newly generated routes
@@ -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)
@@ -10,8 +10,6 @@ module RouteTranslator
10
10
  private
11
11
 
12
12
  def locale_from_args(args)
13
- return if RouteTranslator.config.host_locales.blank?
14
-
15
13
  args_hash = args.find { |arg| arg.is_a?(Hash) }
16
14
  args_hash&.fetch(:locale, nil)
17
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RouteTranslator
4
- VERSION = '14.2.0'
4
+ VERSION = '15.1.0'
5
5
  end
@@ -10,7 +10,7 @@ require_relative 'route_translator/version'
10
10
  module RouteTranslator
11
11
  extend RouteTranslator::Host
12
12
 
13
- TRANSLATABLE_SEGMENT = /^([-_a-zA-Z0-9]+)(\()?/.freeze
13
+ TRANSLATABLE_SEGMENT = /^([-_a-zA-Z0-9]+)(\()?/
14
14
 
15
15
  DEFAULT_CONFIGURATION = {
16
16
  available_locales: [],
@@ -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,16 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: route_translator
3
3
  version: !ruby/object:Gem::Version
4
- version: 14.2.0
4
+ version: 15.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geremia Taglialatela
8
8
  - Enric Lluelles
9
9
  - Raul Murciano
10
- autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2024-11-08 00:00:00.000000000 Z
12
+ date: 1980-01-02 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: actionpack
@@ -75,7 +74,6 @@ metadata:
75
74
  bug_tracker_uri: https://github.com/enriclluelles/route_translator/issues
76
75
  changelog_uri: https://github.com/enriclluelles/route_translator/blob/master/CHANGELOG.md
77
76
  source_code_uri: https://github.com/enriclluelles/route_translator
78
- post_install_message:
79
77
  rdoc_options: []
80
78
  require_paths:
81
79
  - lib
@@ -83,15 +81,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
81
  requirements:
84
82
  - - ">="
85
83
  - !ruby/object:Gem::Version
86
- version: '2.7'
84
+ version: '3.1'
87
85
  required_rubygems_version: !ruby/object:Gem::Requirement
88
86
  requirements:
89
87
  - - ">="
90
88
  - !ruby/object:Gem::Version
91
89
  version: '0'
92
90
  requirements: []
93
- rubygems_version: 3.5.18
94
- signing_key:
91
+ rubygems_version: 3.6.9
95
92
  specification_version: 4
96
93
  summary: Translate your Rails routes in a simple manner
97
94
  test_files: []