route_translator 3.2.4 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/route_translator.rb +5 -5
- data/lib/route_translator/extensions/action_controller.rb +15 -21
- data/lib/route_translator/extensions/mapper.rb +6 -1
- data/lib/route_translator/host.rb +6 -10
- data/lib/route_translator/translator.rb +41 -19
- data/lib/route_translator/version.rb +1 -1
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/controllers/dummy_controller.rb +6 -0
- data/test/dummy/app/views/dummy/show.html.erb +1 -0
- data/test/dummy/config/application.rb +7 -0
- data/test/dummy/config/locales/all.yml +3 -0
- data/test/dummy/config/routes.rb +4 -1
- data/test/host_test.rb +33 -24
- data/test/integration/generated_path_test.rb +37 -0
- data/test/{integration_test.rb → integration/host_locales_test.rb} +19 -30
- data/test/integration/routing_test.rb +22 -0
- data/test/integration/thread_safety_test.rb +16 -0
- data/test/routing_test.rb +93 -65
- data/test/support/assertion_helper.rb +27 -0
- data/test/support/configuration_helper.rb +51 -0
- data/test/support/i18n_helper.rb +24 -0
- data/test/support/integration_helper.rb +6 -0
- data/test/support/routes_helper.rb +60 -0
- data/test/test_helper.rb +3 -96
- metadata +54 -24
- data/test/dummy/config/initializers/route_translator.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 077e37f57ea89aa141aa2e271ba08e832cd85ef5
|
4
|
+
data.tar.gz: d6d9c89e7c57dcf22975bdc9e2194107ddf0b290
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 554ab0400cbdffbfc854c3de668d892513bf1195598d6611df9057bfaa26f764682fc25a370531366ba8e4b1ac06620ee49138cd2584e695516a228303297168
|
7
|
+
data.tar.gz: d5e661b4a03807afe05d7ba8c066fde6979b656b949464cccf667f75855ec083aab15301fdb92c5ee5d3b03f36bf1d4f2bf73a59beeceba18bf13f722f93129f
|
data/lib/route_translator.rb
CHANGED
@@ -1,7 +1,4 @@
|
|
1
1
|
require 'active_support'
|
2
|
-
require 'action_controller'
|
3
|
-
require 'action_mailer'
|
4
|
-
require 'action_dispatch'
|
5
2
|
|
6
3
|
require File.expand_path('../route_translator/extensions', __FILE__)
|
7
4
|
require File.expand_path('../route_translator/translator', __FILE__)
|
@@ -24,7 +21,7 @@ module RouteTranslator
|
|
24
21
|
@config.generate_unlocalized_routes ||= false
|
25
22
|
@config.locale_param_key ||= :locale
|
26
23
|
@config.generate_unnamed_unlocalized_routes ||= false
|
27
|
-
@config.host_locales ||=
|
24
|
+
@config.host_locales ||= ActiveSupport::OrderedHash.new
|
28
25
|
yield @config if block
|
29
26
|
resolve_config_conflicts
|
30
27
|
@config
|
@@ -36,7 +33,10 @@ module RouteTranslator
|
|
36
33
|
@config.generate_unnamed_unlocalized_routes = false
|
37
34
|
@config.force_locale = false
|
38
35
|
@config.hide_locale = false
|
39
|
-
@config.host_locales = @config.host_locales.with_indifferent_access
|
40
36
|
end
|
41
37
|
end
|
38
|
+
|
39
|
+
def self.locale_param_key
|
40
|
+
self.config.locale_param_key
|
41
|
+
end
|
42
42
|
end
|
@@ -4,30 +4,24 @@ module ActionController
|
|
4
4
|
class Base
|
5
5
|
around_filter :set_locale_from_url
|
6
6
|
|
7
|
-
def set_locale_from_url
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def with_host_locale(&block)
|
14
|
-
host_locale = RouteTranslator::Host.locale_from_host(request.host)
|
15
|
-
begin
|
16
|
-
if host_locale
|
17
|
-
original_default = I18n.default_locale
|
18
|
-
original_locale = I18n.locale
|
7
|
+
def set_locale_from_url
|
8
|
+
tmp_default_locale = RouteTranslator::Host.locale_from_host(request.host)
|
9
|
+
if tmp_default_locale
|
10
|
+
current_default_locale = I18n.default_locale
|
11
|
+
I18n.default_locale = tmp_default_locale
|
12
|
+
end
|
19
13
|
|
20
|
-
|
21
|
-
|
22
|
-
|
14
|
+
tmp_locale = params[RouteTranslator.locale_param_key] || tmp_default_locale
|
15
|
+
if tmp_locale
|
16
|
+
current_locale = I18n.locale
|
17
|
+
I18n.locale = tmp_locale
|
18
|
+
end
|
23
19
|
|
24
|
-
|
20
|
+
yield
|
25
21
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
22
|
+
ensure
|
23
|
+
I18n.default_locale = current_default_locale if tmp_default_locale
|
24
|
+
I18n.locale = current_locale if tmp_locale
|
30
25
|
end
|
31
|
-
|
32
26
|
end
|
33
27
|
end
|
@@ -25,7 +25,12 @@ module ActionDispatch
|
|
25
25
|
options[:as] = name_for_action(options[:as], action)
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
begin
|
29
|
+
mapping = Mapping.new(@set, @scope, path, options)
|
30
|
+
rescue ArgumentError => e
|
31
|
+
mapping = Mapping.build(@scope, @set, URI.parser.escape(path), options.delete(:as), options)
|
32
|
+
end
|
33
|
+
|
29
34
|
if @localized
|
30
35
|
@set.add_localized_route(*mapping.to_route)
|
31
36
|
else
|
@@ -1,12 +1,12 @@
|
|
1
1
|
module RouteTranslator
|
2
2
|
module Host
|
3
3
|
def self.locale_from_host(host)
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
locales = RouteTranslator.config.host_locales.reduce([]) do |locales, (pattern, locale)|
|
5
|
+
locales << locale.to_sym if host =~ regex_for(pattern)
|
6
|
+
locales
|
7
|
+
end
|
8
|
+
locales &= I18n.available_locales
|
9
|
+
(locales.first || I18n.default_locale).to_sym
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.regex_for(host_string)
|
@@ -21,9 +21,5 @@ module RouteTranslator
|
|
21
21
|
def native_locales
|
22
22
|
config.host_locales.values.map {|locale| :"native_#{locale}" }
|
23
23
|
end
|
24
|
-
|
25
|
-
def locale_param_key
|
26
|
-
self.config.locale_param_key
|
27
|
-
end
|
28
24
|
end
|
29
25
|
end
|
@@ -5,38 +5,40 @@ module RouteTranslator
|
|
5
5
|
# people_path -> people_de_path
|
6
6
|
# I18n.locale = :fr
|
7
7
|
# people_path -> people_fr_path
|
8
|
-
def self.add_untranslated_helpers_to_controllers_and_views(old_name,
|
9
|
-
|
8
|
+
def self.add_untranslated_helpers_to_controllers_and_views(old_name, named_route_collection)
|
9
|
+
if (named_route_collection.respond_to?(:url_helpers_module))
|
10
|
+
url_helpers_module = named_route_collection.url_helpers_module
|
11
|
+
path_helpers_module = named_route_collection.path_helpers_module
|
12
|
+
url_helpers_list = named_route_collection.helper_names
|
13
|
+
path_helpers_list = named_route_collection.helper_names
|
14
|
+
else
|
15
|
+
url_helpers_module = named_route_collection.module
|
16
|
+
path_helpers_module = named_route_collection.module
|
17
|
+
url_helpers_list = named_route_collection.helpers
|
18
|
+
path_helpers_list = named_route_collection.helpers
|
19
|
+
end
|
20
|
+
|
21
|
+
[
|
22
|
+
['path', path_helpers_module, path_helpers_list],
|
23
|
+
['url', url_helpers_module, url_helpers_list]
|
24
|
+
].each do |suffix, helper_container, helper_list|
|
10
25
|
new_helper_name = "#{old_name}_#{suffix}"
|
11
26
|
|
12
27
|
helper_list.push(new_helper_name.to_sym) unless helper_list.include?(new_helper_name.to_sym)
|
13
28
|
|
14
29
|
helper_container.__send__(:define_method, new_helper_name) do |*args|
|
15
|
-
|
16
|
-
default_locale_suffix = I18n.default_locale.to_s.underscore
|
17
|
-
args_hash = args.select {|arg| arg.is_a?(Hash) }.first
|
18
|
-
args_locale_suffix = args_hash[:locale].to_s.underscore if args_hash.present?
|
19
|
-
if RouteTranslator.config.host_locales.present?
|
20
|
-
if args.blank? || args_locale_suffix == default_locale_suffix
|
21
|
-
__send__("#{old_name}_native_#{default_locale_suffix}_#{suffix}", *args)
|
22
|
-
elsif args_locale_suffix
|
23
|
-
__send__("#{old_name}_#{args_locale_suffix}_#{suffix}", *args)
|
24
|
-
end
|
25
|
-
elsif respond_to?("#{old_name}_#{locale_suffix}_#{suffix}")
|
26
|
-
__send__("#{old_name}_#{locale_suffix}_#{suffix}", *args)
|
27
|
-
else
|
28
|
-
__send__("#{old_name}_#{I18n.default_locale.to_s.underscore}_#{suffix}", *args)
|
29
|
-
end
|
30
|
+
__send__(Translator.route_name_for(args, old_name, suffix, self), *args)
|
30
31
|
end
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
34
35
|
def self.translations_for(app, conditions, requirements, defaults, route_name, anchor, route_set, &block)
|
35
|
-
add_untranslated_helpers_to_controllers_and_views(route_name, route_set.named_routes
|
36
|
+
add_untranslated_helpers_to_controllers_and_views(route_name, route_set.named_routes)
|
36
37
|
|
37
38
|
available_locales.each do |locale|
|
38
39
|
new_conditions = conditions.dup
|
39
40
|
new_conditions[:path_info] = translate_path(conditions[:path_info], locale)
|
41
|
+
new_conditions[:parsed_path_info] = ActionDispatch::Journey::Parser.new.parse(new_conditions[:path_info]) if conditions[:parsed_path_info]
|
40
42
|
if new_conditions[:required_defaults] && !new_conditions[:required_defaults].include?(RouteTranslator.locale_param_key)
|
41
43
|
new_conditions[:required_defaults] << RouteTranslator.locale_param_key if new_conditions[:required_defaults]
|
42
44
|
end
|
@@ -56,7 +58,7 @@ module RouteTranslator
|
|
56
58
|
private
|
57
59
|
def self.available_locales
|
58
60
|
available_locales = I18n.available_locales.dup
|
59
|
-
available_locales.push
|
61
|
+
available_locales.push(*RouteTranslator.native_locales) if RouteTranslator.native_locales.present?
|
60
62
|
# Make sure the default locale is translated in last place to avoid
|
61
63
|
# problems with wildcards when default locale is omitted in paths. The
|
62
64
|
# default routes will catch all paths like wildcard if it is translated first.
|
@@ -120,5 +122,25 @@ module RouteTranslator
|
|
120
122
|
def self.locale_param_present?(path)
|
121
123
|
!(path.split('/').detect { |segment| segment.to_s == ":#{RouteTranslator.locale_param_key.to_s}" }.nil?)
|
122
124
|
end
|
125
|
+
|
126
|
+
def self.host_locales_option?
|
127
|
+
RouteTranslator.config.host_locales.present?
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.route_name_for(args, old_name, suffix, kaller)
|
131
|
+
args_hash = args.detect{|arg| arg.is_a?(Hash)}
|
132
|
+
args_locale = host_locales_option? && args_hash && args_hash[:locale]
|
133
|
+
current_locale_name = I18n.locale.to_s.underscore
|
134
|
+
|
135
|
+
locale = if args_locale
|
136
|
+
args_locale.to_s.underscore
|
137
|
+
elsif kaller.respond_to?("#{old_name}_#{current_locale_name}_#{suffix}")
|
138
|
+
current_locale_name
|
139
|
+
else
|
140
|
+
I18n.default_locale.to_s.underscore
|
141
|
+
end
|
142
|
+
|
143
|
+
"#{old_name}_#{locale}_#{suffix}"
|
144
|
+
end
|
123
145
|
end
|
124
146
|
end
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
3
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
4
|
+
|
5
|
+
require File.expand_path('../config/application', __FILE__)
|
6
|
+
|
7
|
+
Dummy::Application.load_tasks
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= link_to "Show [#{I18n.locale}]", show_path %>
|
data/test/dummy/config/routes.rb
CHANGED
data/test/host_test.rb
CHANGED
@@ -1,24 +1,28 @@
|
|
1
1
|
#encoding: utf-8
|
2
|
-
require File.expand_path(
|
2
|
+
require File.expand_path('../test_helper', __FILE__)
|
3
3
|
|
4
4
|
class TestHostsFromLocale < MiniTest::Unit::TestCase
|
5
|
-
|
5
|
+
|
6
|
+
include RouteTranslator::ConfigurationHelper
|
7
|
+
include RouteTranslator::I18nHelper
|
8
|
+
include RouteTranslator::RoutesHelper
|
9
|
+
|
6
10
|
def setup
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
setup_config
|
12
|
+
setup_i18n
|
13
|
+
|
14
|
+
config = host_locales_config_hash
|
15
|
+
config['*.something.es'] = :es
|
16
|
+
config['*.ru.subdomain.domain.*'] = :ru
|
17
|
+
config['russia.something.net'] = :ru
|
18
|
+
config['*.com'] = :en
|
19
|
+
|
20
|
+
config_host_locales(config)
|
17
21
|
end
|
18
22
|
|
19
23
|
def teardown
|
20
|
-
|
21
|
-
|
24
|
+
teardown_i18n
|
25
|
+
teardown_config
|
22
26
|
end
|
23
27
|
|
24
28
|
def test_wildcard_at_beginning_matches
|
@@ -54,10 +58,16 @@ class TestHostsFromLocale < MiniTest::Unit::TestCase
|
|
54
58
|
end
|
55
59
|
|
56
60
|
def test_precedence_if_more_than_one_match
|
57
|
-
|
61
|
+
config = host_locales_config_hash
|
62
|
+
config['russia.*'] = :ru
|
63
|
+
config['*.com'] = :en
|
64
|
+
config_host_locales(config)
|
58
65
|
assert_equal :ru, RouteTranslator::Host.locale_from_host('russia.com')
|
59
66
|
|
60
|
-
|
67
|
+
config = host_locales_config_hash
|
68
|
+
config['*.com'] = :en
|
69
|
+
config['russia.*'] = :ru
|
70
|
+
config_host_locales(config)
|
61
71
|
assert_equal :en, RouteTranslator::Host.locale_from_host('russia.com')
|
62
72
|
end
|
63
73
|
|
@@ -66,14 +76,13 @@ class TestHostsFromLocale < MiniTest::Unit::TestCase
|
|
66
76
|
end
|
67
77
|
|
68
78
|
def test_readme_examples_work
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
)
|
79
|
+
config = host_locales_config_hash
|
80
|
+
config['*.es'] = :es # matches ['domain.es', 'subdomain.domain.es', 'www.long.string.of.subdomains.es'] etc.
|
81
|
+
config['ru.wikipedia.*'] = :ru # matches ['ru.wikipedia.org', 'ru.wikipedia.net', 'ru.wikipedia.com'] etc.
|
82
|
+
config['*.subdomain.domain.*'] = :ru # matches ['subdomain.domain.org', 'www.subdomain.domain.net'] etc.
|
83
|
+
config['news.bbc.co.uk'] = :en # matches ['news.bbc.co.uk'] only
|
84
|
+
|
85
|
+
config_host_locales(config)
|
77
86
|
|
78
87
|
examples_1 = ['domain.es', 'subdomain.domain.es', 'www.long.string.of.subdomains.es']
|
79
88
|
examples_2 = ['ru.wikipedia.org', 'ru.wikipedia.net', 'ru.wikipedia.com']
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require File.expand_path('../../test_helper', __FILE__)
|
3
|
+
|
4
|
+
class GeneratedPathTest < integration_test_suite_parent_class
|
5
|
+
|
6
|
+
include RouteTranslator::ConfigurationHelper
|
7
|
+
|
8
|
+
def test_path_generated
|
9
|
+
get '/show'
|
10
|
+
assert_response :success
|
11
|
+
assert_tag :tag => "a", :attributes => { :href => "/show" }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_path_translated
|
15
|
+
get '/es/mostrar'
|
16
|
+
assert_response :success
|
17
|
+
assert_tag :tag => "a", :attributes => { :href => "/es/mostrar" }
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_path_translated_after_force
|
21
|
+
config_force_locale true
|
22
|
+
|
23
|
+
get '/es/mostrar'
|
24
|
+
assert_response :success
|
25
|
+
assert_tag :tag => "a", :attributes => { :href => "/es/mostrar" }
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_path_translated_while_generate_unlocalized_routes
|
29
|
+
config_default_locale_settings 'en'
|
30
|
+
config_generate_unlocalized_routes true
|
31
|
+
|
32
|
+
get '/es/mostrar'
|
33
|
+
assert_response :success
|
34
|
+
assert_tag :tag => "a", :attributes => { :href => "/es/mostrar" }
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -1,42 +1,36 @@
|
|
1
1
|
#encoding: utf-8
|
2
|
-
require File.expand_path('
|
2
|
+
require File.expand_path('../../test_helper', __FILE__)
|
3
3
|
|
4
|
-
|
5
|
-
require File.expand_path('../dummy/config/environment', __FILE__)
|
4
|
+
class HostLocalesTest < integration_test_suite_parent_class
|
6
5
|
|
7
|
-
|
8
|
-
class IntegrationTest < class_to_inherit
|
9
|
-
include RouteTranslator::TestHelper
|
6
|
+
include RouteTranslator::ConfigurationHelper
|
10
7
|
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
get '/es/dummy'
|
15
|
-
assert_equal 'es', @response.body
|
16
|
-
assert_response :success
|
8
|
+
def setup
|
9
|
+
config_host_locales('*.es' => 'es', 'ru.*.com' => 'ru')
|
10
|
+
Dummy::Application.reload_routes!
|
17
11
|
end
|
18
12
|
|
19
|
-
def
|
20
|
-
|
21
|
-
|
22
|
-
assert_response :success
|
13
|
+
def teardown
|
14
|
+
config_host_locales({})
|
15
|
+
Dummy::Application.reload_routes!
|
23
16
|
end
|
24
17
|
|
25
|
-
def test_i18n_locale_thread_safe
|
26
|
-
config_default_locale_settings 'en'
|
27
|
-
get '/es/dummy'
|
28
|
-
assert_equal 'es', @response.body
|
29
|
-
|
30
|
-
assert_equal :en, I18n.locale
|
31
|
-
end
|
32
18
|
|
33
|
-
def
|
19
|
+
def test_root_path
|
34
20
|
## root of es com
|
35
21
|
host! 'www.testapp.es'
|
36
22
|
get '/'
|
37
23
|
assert_equal 'es', @response.body
|
38
24
|
assert_response :success
|
39
25
|
|
26
|
+
## root of ru com
|
27
|
+
host! 'ru.testapp.com'
|
28
|
+
get '/'
|
29
|
+
assert_equal 'ru', @response.body
|
30
|
+
assert_response :success
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_explicit_path
|
40
34
|
## native es route on es com
|
41
35
|
host! 'www.testapp.es'
|
42
36
|
get '/dummy'
|
@@ -49,12 +43,6 @@ class IntegrationTest < class_to_inherit
|
|
49
43
|
assert_equal 'ru', @response.body
|
50
44
|
assert_response :success
|
51
45
|
|
52
|
-
## root of ru com
|
53
|
-
host! 'ru.testapp.com'
|
54
|
-
get '/'
|
55
|
-
assert_equal 'ru', @response.body
|
56
|
-
assert_response :success
|
57
|
-
|
58
46
|
## native ru route on ru com
|
59
47
|
host! 'ru.testapp.com'
|
60
48
|
get URI.escape('/манекен')
|
@@ -67,4 +55,5 @@ class IntegrationTest < class_to_inherit
|
|
67
55
|
assert_equal 'es', @response.body
|
68
56
|
assert_response :success
|
69
57
|
end
|
58
|
+
|
70
59
|
end
|