route_translator 5.0.6 → 5.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/CHANGELOG.md +5 -0
- data/README.md +32 -0
- data/lib/route_translator/extensions/route_set.rb +3 -1
- data/lib/route_translator/route.rb +23 -0
- data/lib/route_translator/translator.rb +9 -8
- data/lib/route_translator/translator/path.rb +2 -2
- data/lib/route_translator/translator/path/segment.rb +23 -10
- data/lib/route_translator/version.rb +1 -1
- metadata +12 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db2eb895765c0bc6d9331fb86fa65f36ad1e2f11
|
4
|
+
data.tar.gz: 34389bc82a4bed4c1b7fac7e4588710ca4316cac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da58eb381c0ec9f894e5b54457ea3b172902fe778c4d52ceacc708a7a410192552bffc2bc4a3a28ab55963635e82f9cc5df212ede87d2c254411763c40bc7460
|
7
|
+
data.tar.gz: f29622f458c5781277d3835c396f325b13973aec2f5c49496fb23a4bef29b64ce10041301a997f55bfc345c8ccf01887f59720c6bb8ef624de7c225a6bbc0e7e
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -327,6 +327,38 @@ If `host_locales` option is set, the following options will be forced (even if y
|
|
327
327
|
This is to avoid odd behaviour brought about by route conflicts and because `host_locales` forces and hides the host-locale dynamically.
|
328
328
|
|
329
329
|
|
330
|
+
### Translations for similar routes with different namespaces
|
331
|
+
|
332
|
+
If you have routes that (partially) share names in one locale, but must be translated differently in another locale, for example:
|
333
|
+
|
334
|
+
```ruby
|
335
|
+
get 'people/favourites', to: 'people/products#favourites'
|
336
|
+
get 'favourites', to: 'products#favourites'
|
337
|
+
```
|
338
|
+
|
339
|
+
Then it is possible to provide different translations for common parts of those routes by
|
340
|
+
scoping translations by a controller's namespace:
|
341
|
+
|
342
|
+
```yml
|
343
|
+
es:
|
344
|
+
routes:
|
345
|
+
favourites: favoritos
|
346
|
+
controllers:
|
347
|
+
people:
|
348
|
+
products:
|
349
|
+
favourites: fans
|
350
|
+
```
|
351
|
+
|
352
|
+
Routes will be translated as in:
|
353
|
+
|
354
|
+
```
|
355
|
+
people_products_favourites_es GET /people/products/fans(.:format) people/products#favourites {:locale=>"es"}
|
356
|
+
products_favourites_es GET /products/favoritos(.:format) products#favourites {:locale=>"es"}
|
357
|
+
```
|
358
|
+
|
359
|
+
The gem will lookup translations under `controllers` scope first and then lookup translations under `routes` scope.
|
360
|
+
|
361
|
+
|
330
362
|
|
331
363
|
## Testing
|
332
364
|
Testing your controllers with routes-translator is easy, just add a locale parameter for your localized routes. Otherwise, an ActionController::UrlGenerationError will raise.
|
@@ -4,7 +4,9 @@ module ActionDispatch
|
|
4
4
|
module Routing
|
5
5
|
class RouteSet
|
6
6
|
def add_localized_route(mapping, path_ast, name, anchor, scope, path, controller, default_action, to, via, formatted, options_constraints, options)
|
7
|
-
RouteTranslator::
|
7
|
+
route = RouteTranslator::Route.new(self, path, name, options_constraints, options, mapping)
|
8
|
+
|
9
|
+
RouteTranslator::Translator.translations_for(route) do |translated_name, translated_path, translated_options_constraints, translated_options|
|
8
10
|
translated_path_ast = ::ActionDispatch::Journey::Parser.parse(translated_path)
|
9
11
|
translated_mapping = ::ActionDispatch::Routing::Mapper::Mapping.build(scope, self, translated_path_ast, controller, default_action, to, via, formatted, translated_options_constraints, anchor, translated_options)
|
10
12
|
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module RouteTranslator
|
2
|
+
class Route
|
3
|
+
attr_reader :route_set, :path, :name, :options_constraints, :options, :mapping
|
4
|
+
|
5
|
+
def initialize(route_set, path, name, options_constraints, options, mapping)
|
6
|
+
@route_set = route_set
|
7
|
+
@path = path
|
8
|
+
@name = name
|
9
|
+
@options_constraints = options_constraints
|
10
|
+
@options = options
|
11
|
+
@mapping = mapping
|
12
|
+
end
|
13
|
+
|
14
|
+
def scope
|
15
|
+
@scope ||=
|
16
|
+
if mapping.defaults[:controller]
|
17
|
+
[:routes, :controllers].concat mapping.defaults[:controller].split('/').map(&:to_sym)
|
18
|
+
else
|
19
|
+
[:routes, :controllers]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require File.expand_path('../translator/route_helpers', __FILE__)
|
4
4
|
require File.expand_path('../translator/path', __FILE__)
|
5
|
+
require File.expand_path('../route', __FILE__)
|
5
6
|
|
6
7
|
module RouteTranslator
|
7
8
|
module Translator
|
@@ -39,8 +40,8 @@ module RouteTranslator
|
|
39
40
|
translated_options_constraints
|
40
41
|
end
|
41
42
|
|
42
|
-
def translate_path(path, locale)
|
43
|
-
RouteTranslator::Translator::Path.translate(path, locale)
|
43
|
+
def translate_path(path, locale, scope)
|
44
|
+
RouteTranslator::Translator::Path.translate(path, locale, scope)
|
44
45
|
rescue I18n::MissingTranslationData => e
|
45
46
|
raise e unless RouteTranslator.config.disable_fallback
|
46
47
|
end
|
@@ -58,16 +59,16 @@ module RouteTranslator
|
|
58
59
|
locales.push I18n.default_locale
|
59
60
|
end
|
60
61
|
|
61
|
-
def translations_for(
|
62
|
-
RouteTranslator::Translator::RouteHelpers.add name, route_set.named_routes
|
62
|
+
def translations_for(route)
|
63
|
+
RouteTranslator::Translator::RouteHelpers.add route.name, route.route_set.named_routes
|
63
64
|
|
64
65
|
available_locales.each do |locale|
|
65
|
-
translated_path = translate_path(path, locale)
|
66
|
+
translated_path = translate_path(route.path, locale, route.scope)
|
66
67
|
next unless translated_path
|
67
68
|
|
68
|
-
translated_name = translate_name(name, locale, route_set.named_routes.names)
|
69
|
-
translated_options_constraints = translate_options_constraints(options_constraints, locale)
|
70
|
-
translated_options = translate_options(options, locale)
|
69
|
+
translated_name = translate_name(route.name, locale, route.route_set.named_routes.names)
|
70
|
+
translated_options_constraints = translate_options_constraints(route.options_constraints, locale)
|
71
|
+
translated_options = translate_options(route.options, locale)
|
71
72
|
|
72
73
|
yield translated_name, translated_path, translated_options_constraints, translated_options
|
73
74
|
end
|
@@ -38,11 +38,11 @@ module RouteTranslator
|
|
38
38
|
module_function
|
39
39
|
|
40
40
|
# Translates a path and adds the locale prefix.
|
41
|
-
def translate(path, locale)
|
41
|
+
def translate(path, locale, scope)
|
42
42
|
new_path = path.dup
|
43
43
|
final_optional_segments = new_path.slice!(%r{(\([^\/]+\))$})
|
44
44
|
translated_segments = new_path.split('/').map do |seg|
|
45
|
-
seg.split('.').map { |phrase| Segment.translate(phrase, locale) }.join('.')
|
45
|
+
seg.split('.').map { |phrase| Segment.translate(phrase, locale, scope) }.join('.')
|
46
46
|
end
|
47
47
|
translated_segments.reject!(&:empty?)
|
48
48
|
|
@@ -5,16 +5,29 @@ module RouteTranslator
|
|
5
5
|
class << self
|
6
6
|
private
|
7
7
|
|
8
|
-
def
|
9
|
-
locale = locale.to_s.gsub('native_', '')
|
10
|
-
opts = { scope: :routes, locale: locale }
|
8
|
+
def fallback_options(str, locale)
|
11
9
|
if RouteTranslator.config.disable_fallback && locale.to_s != I18n.default_locale.to_s
|
12
|
-
|
10
|
+
{ scope: :routes, fallback: true }
|
11
|
+
else
|
12
|
+
{ scope: :routes, default: str }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def translate_resource(str, locale, opts)
|
17
|
+
handler = proc { |exception| exception }
|
18
|
+
|
19
|
+
if I18n.translate(str, opts.merge(exception_handler: handler)).is_a?(I18n::MissingTranslation)
|
20
|
+
I18n.translate(str, opts.merge(fallback_options(str, locale)))
|
13
21
|
else
|
14
|
-
opts
|
22
|
+
I18n.translate(str, opts)
|
15
23
|
end
|
16
|
-
|
17
|
-
|
24
|
+
end
|
25
|
+
|
26
|
+
def translate_string(str, locale, scope)
|
27
|
+
locale = locale.to_s.gsub('native_', '')
|
28
|
+
translated_resource = translate_resource(str, locale, scope: scope, locale: locale)
|
29
|
+
|
30
|
+
URI.escape translated_resource
|
18
31
|
end
|
19
32
|
end
|
20
33
|
|
@@ -26,16 +39,16 @@ module RouteTranslator
|
|
26
39
|
# "people(.:format)", only "people" will be translated.
|
27
40
|
# If there is no translation, the path segment is blank, begins with a
|
28
41
|
# ":" (param key) or "*" (wildcard), the segment is returned untouched.
|
29
|
-
def translate(segment, locale)
|
42
|
+
def translate(segment, locale, scope)
|
30
43
|
return segment if segment.empty?
|
31
44
|
named_param, hyphenized = segment.split('-'.freeze, 2) if segment.starts_with?(':'.freeze)
|
32
|
-
return "#{named_param}-#{translate(hyphenized.dup, locale)}" if hyphenized
|
45
|
+
return "#{named_param}-#{translate(hyphenized.dup, locale, scope)}" if hyphenized
|
33
46
|
return segment if segment.starts_with?('('.freeze) || segment.starts_with?('*'.freeze) || segment.include?(':'.freeze)
|
34
47
|
|
35
48
|
appended_part = segment.slice!(/(\()$/)
|
36
49
|
match = TRANSLATABLE_SEGMENT.match(segment)[1] if TRANSLATABLE_SEGMENT.match(segment)
|
37
50
|
|
38
|
-
(translate_string(match, locale) || segment) + appended_part.to_s
|
51
|
+
(translate_string(match, locale, scope) || segment) + appended_part.to_s
|
39
52
|
end
|
40
53
|
end
|
41
54
|
end
|
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: 5.0
|
4
|
+
version: 5.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Geremia Taglialatela
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2017-01-
|
13
|
+
date: 2017-01-24 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -74,34 +74,28 @@ dependencies:
|
|
74
74
|
requirements:
|
75
75
|
- - "~>"
|
76
76
|
- !ruby/object:Gem::Version
|
77
|
-
version: 0.8.
|
77
|
+
version: 0.8.19
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
80
|
version_requirements: !ruby/object:Gem::Requirement
|
81
81
|
requirements:
|
82
82
|
- - "~>"
|
83
83
|
- !ruby/object:Gem::Version
|
84
|
-
version: 0.8.
|
84
|
+
version: 0.8.19
|
85
85
|
- !ruby/object:Gem::Dependency
|
86
86
|
name: minitest
|
87
87
|
requirement: !ruby/object:Gem::Requirement
|
88
88
|
requirements:
|
89
|
-
- - "
|
90
|
-
- !ruby/object:Gem::Version
|
91
|
-
version: 4.7.5
|
92
|
-
- - "<"
|
89
|
+
- - "~>"
|
93
90
|
- !ruby/object:Gem::Version
|
94
|
-
version:
|
91
|
+
version: '5.10'
|
95
92
|
type: :development
|
96
93
|
prerelease: false
|
97
94
|
version_requirements: !ruby/object:Gem::Requirement
|
98
95
|
requirements:
|
99
|
-
- - "
|
100
|
-
- !ruby/object:Gem::Version
|
101
|
-
version: 4.7.5
|
102
|
-
- - "<"
|
96
|
+
- - "~>"
|
103
97
|
- !ruby/object:Gem::Version
|
104
|
-
version:
|
98
|
+
version: '5.10'
|
105
99
|
- !ruby/object:Gem::Dependency
|
106
100
|
name: rails
|
107
101
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,14 +130,14 @@ dependencies:
|
|
136
130
|
requirements:
|
137
131
|
- - "~>"
|
138
132
|
- !ruby/object:Gem::Version
|
139
|
-
version: 0.47.
|
133
|
+
version: 0.47.1
|
140
134
|
type: :development
|
141
135
|
prerelease: false
|
142
136
|
version_requirements: !ruby/object:Gem::Requirement
|
143
137
|
requirements:
|
144
138
|
- - "~>"
|
145
139
|
- !ruby/object:Gem::Version
|
146
|
-
version: 0.47.
|
140
|
+
version: 0.47.1
|
147
141
|
- !ruby/object:Gem::Dependency
|
148
142
|
name: simplecov
|
149
143
|
requirement: !ruby/object:Gem::Requirement
|
@@ -176,6 +170,7 @@ files:
|
|
176
170
|
- lib/route_translator/extensions/mapper.rb
|
177
171
|
- lib/route_translator/extensions/route_set.rb
|
178
172
|
- lib/route_translator/host.rb
|
173
|
+
- lib/route_translator/route.rb
|
179
174
|
- lib/route_translator/translator.rb
|
180
175
|
- lib/route_translator/translator/path.rb
|
181
176
|
- lib/route_translator/translator/path/segment.rb
|
@@ -201,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
201
196
|
version: '0'
|
202
197
|
requirements: []
|
203
198
|
rubyforge_project:
|
204
|
-
rubygems_version: 2.6.
|
199
|
+
rubygems_version: 2.6.9
|
205
200
|
signing_key:
|
206
201
|
specification_version: 4
|
207
202
|
summary: Translate your Rails routes in a simple manner
|