Skip to content

Commit

Permalink
Merge pull request #285 from timfsw/feature/translator_lookup
Browse files Browse the repository at this point in the history
Add support for i18n lookup with the controller path instead of nested keys
  • Loading branch information
tagliala committed Jul 28, 2023
2 parents f518539 + 00c1576 commit 2a70e9c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +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_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
16 changes: 15 additions & 1 deletion lib/route_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module RouteTranslator
hide_locale: false,
host_locales: {},
locale_param_key: :locale,
locale_segment_proc: false
locale_segment_proc: false,
i18n_use_slash_separator: false
}.freeze

Configuration = Struct.new(*DEFAULT_CONFIGURATION.keys)
Expand All @@ -34,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 @@ -48,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
6 changes: 5 additions & 1 deletion lib/route_translator/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def initialize(route_set, path, name, options_constraints, options, mapping)
def scope
@scope ||=
if mapping.defaults[:controller]
%i[routes controllers].concat mapping.defaults[:controller].split('/').map(&:to_sym)
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)
end
else
%i[routes controllers]
end
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
4 changes: 3 additions & 1 deletion test/locales/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ es:
products:
products: productos_favoritos
favourites: fans

people/products:
products: productos_new_favoritos
favourites: fans
ru:
routes:
people: люди
Expand Down
19 changes: 19 additions & 0 deletions test/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,25 @@ def test_namespaced_resources
assert_routing '/gente/productos_favoritos', controller: 'people/products', action: 'index', locale: 'es'
end

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

draw_routes do
localized do
resources :products

namespace :people do
resources :products
end
end
end

assert_routing '/productos', controller: 'products', action: 'index', locale: 'es'
assert_routing '/gente/productos_new_favoritos', controller: 'people/products', action: 'index', locale: 'es'
assert_unrecognized_route '/gente/productos_favoritos', controller: 'people/products', action: 'index', locale: 'es'
end

def test_utf8_characters
draw_routes do
localized do
Expand Down

0 comments on commit 2a70e9c

Please sign in to comment.