Skip to content

Commit

Permalink
Add deprecation message
Browse files Browse the repository at this point in the history
  • Loading branch information
tagliala committed Jul 28, 2023
1 parent fbdccb4 commit 00c1576
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,13 @@ end
| `host_locales` | Sets `I18n.locale` based on `request.host`. Useful for apps accepting requests from more than one domain. See below for more details | `{}` |
| `locale_param_key` | The param key used to set the locale to the newly generated routes | `:locale` |
| `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` |
| `i18n_use_controller_path` | Use the exact controller path `account/foo` for looking up translations instead of nested keys `account.foo` | `false` |
| `i18n_use_slash_separator` | Use the exact controller path `account/foo` for looking up translations instead of nested keys `account.foo` | `false` |
#### Deprecated options
- `i18n_use_slash_separator` is deprecated and will be forced to `true` in the next major release of Route Translator. This only
affects application with nested routes. Please set this option to `true` to remove the deprecation message and ensure
to convert nested routes like `people.products` to the new `people/products`.
### Host-based Locale
Expand Down
15 changes: 14 additions & 1 deletion lib/route_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module RouteTranslator
host_locales: {},
locale_param_key: :locale,
locale_segment_proc: false,
i18n_use_controller_path: false
i18n_use_slash_separator: false
}.freeze

Configuration = Struct.new(*DEFAULT_CONFIGURATION.keys)
Expand All @@ -35,6 +35,18 @@ def resolve_host_locale_config_conflicts
@config.generate_unnamed_unlocalized_routes = false
@config.hide_locale = true
end

def check_deprecations
return if @config.i18n_use_slash_separator

ActiveSupport::Deprecation.warn <<~MSG
`i18n_use_slash_separator` set to `false` is deprecated and will be
removed in the next major release of Route Translator to match
Rails' ActiveRecord nested model syntax.
More information at https://github.com/enriclluelles/route_translator/pull/285
MSG
end
end

module_function
Expand All @@ -49,6 +61,7 @@ def config
yield @config if block_given?

resolve_host_locale_config_conflicts if @config.host_locales.present?
check_deprecations

@config
end
Expand Down
2 changes: 1 addition & 1 deletion lib/route_translator/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(route_set, path, name, options_constraints, options, mapping)
def scope
@scope ||=
if mapping.defaults[:controller]
if RouteTranslator.config.i18n_use_controller_path
if RouteTranslator.config.i18n_use_slash_separator
%i[routes controllers].push mapping.defaults[:controller]
else
%i[routes controllers].concat mapping.defaults[:controller].split('/').map(&:to_sym)
Expand Down
33 changes: 33 additions & 0 deletions test/integration/i18n_slash_separator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'test_helper'

class I18nSlashSeparatorTest < ActionDispatch::IntegrationTest
include RouteTranslator::ConfigurationHelper

def teardown
teardown_config
end

def test_deprecation_when_default
assert_deprecated('i18n_use_slash_separator') do
RouteTranslator.config
end
end

def test_deprecation_when_false
config_i18n_use_slash_separator false

assert_deprecated('i18n_use_slash_separator') do
RouteTranslator.config
end
end

def test_no_deprecation_when_true
config_i18n_use_slash_separator true

assert_not_deprecated do
RouteTranslator.config
end
end
end
2 changes: 1 addition & 1 deletion test/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_namespaced_resources
end

def test_controller_namespaced_resources
config_i18n_use_controller_path(true)
config_i18n_use_slash_separator(true)
I18n.default_locale = :es

draw_routes do
Expand Down

0 comments on commit 00c1576

Please sign in to comment.