route_translator 4.0.0 → 4.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 +4 -4
- data/lib/route_translator.rb +4 -2
- data/lib/route_translator/extensions/action_controller.rb +7 -0
- data/lib/route_translator/translator.rb +26 -4
- data/lib/route_translator/version.rb +1 -1
- data/test/routing_test.rb +82 -0
- data/test/support/configuration_helper.rb +9 -8
- data/test/test_helper.rb +1 -0
- metadata +13 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dee4174f252499d6bc2dd422c15ba1c539532b77
|
4
|
+
data.tar.gz: 06c136f125a12a4764145088525eb0b72e672dba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f60b0e973d0258ad00d6bcd1db400fd6850c3450e790880130c6aa3378284b4ac7cb5ce00a7cd3c15564c1406fdda1ca28cc37d5186f34072f40a3086ceac286
|
7
|
+
data.tar.gz: 26e6d96d602c8af382982ef485639fc0b006f2c5e4154649950ad1f338c5630188b5790de20e02860c0f3c1ebe0294a8394e0cc88c018544ba4f7b4cb1d1c494
|
data/lib/route_translator.rb
CHANGED
@@ -11,8 +11,8 @@ module RouteTranslator
|
|
11
11
|
|
12
12
|
Configuration = Struct.new(:force_locale, :hide_locale,
|
13
13
|
:generate_unlocalized_routes, :locale_param_key,
|
14
|
-
:generate_unnamed_unlocalized_routes,
|
15
|
-
:host_locales)
|
14
|
+
:generate_unnamed_unlocalized_routes, :available_locales,
|
15
|
+
:host_locales, :disable_fallback)
|
16
16
|
|
17
17
|
def self.config(&block)
|
18
18
|
@config ||= Configuration.new
|
@@ -22,6 +22,8 @@ module RouteTranslator
|
|
22
22
|
@config.locale_param_key ||= :locale
|
23
23
|
@config.generate_unnamed_unlocalized_routes ||= false
|
24
24
|
@config.host_locales ||= ActiveSupport::OrderedHash.new
|
25
|
+
@config.available_locales ||= nil
|
26
|
+
@config.disable_fallback ||= false
|
25
27
|
yield @config if block
|
26
28
|
resolve_config_conflicts
|
27
29
|
@config
|
@@ -29,6 +29,11 @@ module RouteTranslator
|
|
29
29
|
helper_container.__send__(:define_method, new_helper_name) do |*args|
|
30
30
|
__send__(Translator.route_name_for(args, old_name, suffix, self), *args)
|
31
31
|
end
|
32
|
+
|
33
|
+
# Including the named routes helpers module
|
34
|
+
[ActionController::TestCase, ActionView::TestCase, ActionMailer::TestCase].each do |klass|
|
35
|
+
klass.__send__(:include, helper_container)
|
36
|
+
end
|
32
37
|
end
|
33
38
|
end
|
34
39
|
|
@@ -37,10 +42,15 @@ module RouteTranslator
|
|
37
42
|
|
38
43
|
available_locales.each do |locale|
|
39
44
|
new_conditions = conditions.dup
|
40
|
-
|
45
|
+
begin
|
46
|
+
new_conditions[:path_info] = translate_path(conditions[:path_info], locale)
|
47
|
+
rescue I18n::MissingTranslationData => e
|
48
|
+
raise e unless RouteTranslator.config.disable_fallback
|
49
|
+
next
|
50
|
+
end
|
41
51
|
new_conditions[:parsed_path_info] = ActionDispatch::Journey::Parser.new.parse(new_conditions[:path_info]) if conditions[:parsed_path_info]
|
42
52
|
if new_conditions[:required_defaults] && !new_conditions[:required_defaults].include?(RouteTranslator.locale_param_key)
|
43
|
-
new_conditions[:required_defaults] << RouteTranslator.locale_param_key
|
53
|
+
new_conditions[:required_defaults] << RouteTranslator.locale_param_key
|
44
54
|
end
|
45
55
|
new_defaults = defaults.merge(RouteTranslator.locale_param_key => locale.to_s.gsub('native_', ''))
|
46
56
|
new_requirements = requirements.merge(RouteTranslator.locale_param_key => locale.to_s)
|
@@ -57,7 +67,7 @@ module RouteTranslator
|
|
57
67
|
|
58
68
|
private
|
59
69
|
def self.available_locales
|
60
|
-
available_locales = I18n.available_locales.dup
|
70
|
+
available_locales = config_locales || I18n.available_locales.dup
|
61
71
|
available_locales.push(*RouteTranslator.native_locales) if RouteTranslator.native_locales.present?
|
62
72
|
# Make sure the default locale is translated in last place to avoid
|
63
73
|
# problems with wildcards when default locale is omitted in paths. The
|
@@ -65,6 +75,12 @@ module RouteTranslator
|
|
65
75
|
available_locales.push(available_locales.delete(I18n.default_locale))
|
66
76
|
end
|
67
77
|
|
78
|
+
def self.config_locales
|
79
|
+
if RouteTranslator.config.available_locales
|
80
|
+
RouteTranslator.config.available_locales.map{|l| l.to_sym}
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
68
84
|
# Translates a path and adds the locale prefix.
|
69
85
|
def self.translate_path(path, locale)
|
70
86
|
new_path = path.dup
|
@@ -115,7 +131,13 @@ module RouteTranslator
|
|
115
131
|
|
116
132
|
def self.translate_string(str, locale)
|
117
133
|
locale = "#{locale}".gsub('native_', '')
|
118
|
-
|
134
|
+
opts = {:scope => :routes, :locale => locale}
|
135
|
+
if RouteTranslator.config.disable_fallback && locale.to_s != I18n.default_locale.to_s
|
136
|
+
opts[:fallback] = true
|
137
|
+
else
|
138
|
+
opts[:default] = str
|
139
|
+
end
|
140
|
+
res = I18n.translate(str, opts)
|
119
141
|
URI.escape(res)
|
120
142
|
end
|
121
143
|
|
data/test/routing_test.rb
CHANGED
@@ -537,6 +537,88 @@ class TranslateRoutesTest < ActionController::TestCase
|
|
537
537
|
assert_routing '/path/to/another/product', :controller => 'products', :action => 'show', :id => 'path/to/another/product'
|
538
538
|
end
|
539
539
|
|
540
|
+
def test_config_available_locales
|
541
|
+
config_available_locales [:en, :ru]
|
542
|
+
|
543
|
+
draw_routes do
|
544
|
+
localized do
|
545
|
+
resources :people
|
546
|
+
end
|
547
|
+
end
|
548
|
+
|
549
|
+
assert_routing URI.escape('/ru/люди'), :controller => 'people', :action => 'index', :locale => 'ru'
|
550
|
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
|
551
|
+
assert_unrecognized_route '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
552
|
+
|
553
|
+
config_available_locales nil
|
554
|
+
end
|
555
|
+
|
556
|
+
def test_config_available_locales_handles_strings
|
557
|
+
config_available_locales %w( en ru )
|
558
|
+
|
559
|
+
draw_routes do
|
560
|
+
localized do
|
561
|
+
resources :people
|
562
|
+
end
|
563
|
+
end
|
564
|
+
|
565
|
+
assert_routing URI.escape('/ru/люди'), :controller => 'people', :action => 'index', :locale => 'ru'
|
566
|
+
assert_routing '/people', :controller => 'people', :action => 'index', :locale => 'en'
|
567
|
+
assert_unrecognized_route '/es/gente', :controller => 'people', :action => 'index', :locale => 'es'
|
568
|
+
|
569
|
+
config_available_locales nil
|
570
|
+
end
|
571
|
+
|
572
|
+
def test_disable_fallback_does_not_draw_non_default_routes
|
573
|
+
config_disable_fallback(true)
|
574
|
+
|
575
|
+
draw_routes do
|
576
|
+
localized do
|
577
|
+
get 'tr_param', :to => 'people#index', :as => 'people'
|
578
|
+
end
|
579
|
+
end
|
580
|
+
|
581
|
+
config_disable_fallback(false)
|
582
|
+
|
583
|
+
assert_routing '/tr_param', :controller => 'people', :action => 'index', :locale => 'en'
|
584
|
+
assert_routing '/es/tr_parametro', :controller => 'people', :action => 'index', :locale => 'es'
|
585
|
+
assert_unrecognized_route '/ru/tr_param', :controller => 'people', :action => 'index', :locale => 'ru'
|
586
|
+
end
|
587
|
+
|
588
|
+
def test_action_controller_test_case_reads_default_urls
|
589
|
+
test_case_reads_default_urls(ActionController::TestCase)
|
590
|
+
end
|
591
|
+
|
592
|
+
def test_action_view_test_case_reads_default_urls
|
593
|
+
test_case_reads_default_urls(ActionView::TestCase)
|
594
|
+
end
|
595
|
+
|
596
|
+
def test_action_mailer_test_case_reads_default_urls
|
597
|
+
test_case_reads_default_urls(ActionMailer::TestCase)
|
598
|
+
end
|
599
|
+
|
600
|
+
private
|
601
|
+
|
602
|
+
def test_case_reads_default_urls(klass)
|
603
|
+
config_default_locale_settings 'en'
|
604
|
+
|
605
|
+
draw_routes do
|
606
|
+
localized do
|
607
|
+
resources :person
|
608
|
+
end
|
609
|
+
end
|
610
|
+
|
611
|
+
test_case = klass.new(nil)
|
612
|
+
|
613
|
+
# Not localized
|
614
|
+
assert test_case.respond_to?(:people_path)
|
615
|
+
assert test_case.respond_to?(:new_person_path)
|
616
|
+
|
617
|
+
# Localized
|
618
|
+
assert test_case.respond_to?(:people_en_path)
|
619
|
+
assert test_case.respond_to?(:new_person_en_path)
|
620
|
+
end
|
621
|
+
|
540
622
|
end
|
541
623
|
|
542
624
|
class ProductsControllerTest < ActionController::TestCase
|
@@ -1,7 +1,5 @@
|
|
1
|
-
|
2
1
|
module RouteTranslator
|
3
2
|
module ConfigurationHelper
|
4
|
-
|
5
3
|
def config_reset
|
6
4
|
config_force_locale false
|
7
5
|
config_hide_locale false
|
@@ -39,13 +37,16 @@ module RouteTranslator
|
|
39
37
|
RouteTranslator.config.host_locales = hash
|
40
38
|
end
|
41
39
|
|
42
|
-
def
|
43
|
-
|
44
|
-
::ActiveSupport::OrderedHash.new
|
45
|
-
else
|
46
|
-
::Hash.new
|
47
|
-
end
|
40
|
+
def config_available_locales(arr)
|
41
|
+
RouteTranslator.config.available_locales = arr
|
48
42
|
end
|
49
43
|
|
44
|
+
def config_disable_fallback(boolean)
|
45
|
+
RouteTranslator.config.disable_fallback = boolean
|
46
|
+
end
|
47
|
+
|
48
|
+
def host_locales_config_hash
|
49
|
+
::Hash.new
|
50
|
+
end
|
50
51
|
end
|
51
52
|
end
|
data/test/test_helper.rb
CHANGED
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: 4.
|
4
|
+
version: 4.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Raul Murciano
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-09-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -17,7 +17,7 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '3.
|
20
|
+
version: '3.2'
|
21
21
|
- - "<"
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: '5.0'
|
@@ -27,7 +27,7 @@ dependencies:
|
|
27
27
|
requirements:
|
28
28
|
- - ">="
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
version: '3.
|
30
|
+
version: '3.2'
|
31
31
|
- - "<"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '5.0'
|
@@ -37,7 +37,7 @@ dependencies:
|
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '3.
|
40
|
+
version: '3.2'
|
41
41
|
- - "<"
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: '5.0'
|
@@ -47,7 +47,7 @@ dependencies:
|
|
47
47
|
requirements:
|
48
48
|
- - ">="
|
49
49
|
- !ruby/object:Gem::Version
|
50
|
-
version: '3.
|
50
|
+
version: '3.2'
|
51
51
|
- - "<"
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '5.0'
|
@@ -55,16 +55,16 @@ dependencies:
|
|
55
55
|
name: pry
|
56
56
|
requirement: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- - "
|
58
|
+
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version:
|
60
|
+
version: 0.10.1
|
61
61
|
type: :development
|
62
62
|
prerelease: false
|
63
63
|
version_requirements: !ruby/object:Gem::Requirement
|
64
64
|
requirements:
|
65
|
-
- - "
|
65
|
+
- - "~>"
|
66
66
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
67
|
+
version: 0.10.1
|
68
68
|
description: Translates the Rails routes of your application into the languages defined
|
69
69
|
in your locale files
|
70
70
|
email: enric@lluell.es
|
@@ -107,7 +107,8 @@ files:
|
|
107
107
|
- test/support/routes_helper.rb
|
108
108
|
- test/test_helper.rb
|
109
109
|
homepage: http://github.com/enriclluelles/route_translator
|
110
|
-
licenses:
|
110
|
+
licenses:
|
111
|
+
- MIT
|
111
112
|
metadata: {}
|
112
113
|
post_install_message:
|
113
114
|
rdoc_options: []
|
@@ -125,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
126
|
version: '0'
|
126
127
|
requirements: []
|
127
128
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.4.5.1
|
129
130
|
signing_key:
|
130
131
|
specification_version: 4
|
131
132
|
summary: Translate your Rails routes in a simple manner
|