route_translator 4.1.0 → 4.2.1
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 +11 -0
- data/LICENSE +21 -0
- data/README.md +231 -0
- data/lib/route_translator/extensions/action_controller.rb +5 -2
- data/lib/route_translator/extensions/mapper.rb +5 -5
- data/lib/route_translator/extensions/route_set.rb +1 -1
- data/lib/route_translator/host.rb +4 -5
- data/lib/route_translator/translator.rb +17 -13
- data/lib/route_translator/version.rb +1 -1
- data/lib/route_translator.rb +1 -1
- metadata +92 -59
- data/test/dummy/Rakefile +0 -7
- data/test/dummy/app/controllers/dummy_controller.rb +0 -11
- data/test/dummy/app/views/dummy/show.html.erb +0 -1
- data/test/dummy/config/application.rb +0 -21
- data/test/dummy/config/boot.rb +0 -0
- data/test/dummy/config/environment.rb +0 -5
- data/test/dummy/config/initializers/secret_token.rb +0 -7
- data/test/dummy/config/initializers/session_store.rb +0 -8
- data/test/dummy/config/locales/all.yml +0 -15
- data/test/dummy/config/routes.rb +0 -12
- data/test/dummy/config.ru +0 -4
- data/test/dummy/dummy_mounted_app.rb +0 -5
- data/test/dummy/script/rails +0 -6
- data/test/host_test.rb +0 -108
- data/test/integration/generated_path_test.rb +0 -37
- data/test/integration/host_locales_test.rb +0 -59
- data/test/integration/routing_test.rb +0 -22
- data/test/integration/thread_safety_test.rb +0 -16
- data/test/locales/routes.yml +0 -13
- data/test/routing_test.rb +0 -664
- data/test/support/assertion_helper.rb +0 -27
- data/test/support/configuration_helper.rb +0 -52
- data/test/support/i18n_helper.rb +0 -24
- data/test/support/integration_helper.rb +0 -6
- data/test/support/routes_helper.rb +0 -60
- data/test/test_helper.rb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52a2ffb8a027023143d004c7b407ec955fe2e007
|
4
|
+
data.tar.gz: 081e4f0721ab2dc82412dde2a0e9eddd9276d570
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16c18fd1dbc61beb9ac9d632d32d9b72ee2b9a998aa55a076066d6391379ef0761ac366ceeae8042f0f6362473f9f99d02fdc259e1d0da59e55469f166a890fc
|
7
|
+
data.tar.gz: 52f6d6569aeb42d5c6b6b7c257aef073cd25e1e45d0fa320e12f2e509f51b4356503051e376730221737a98db4ac0e4a72e231708a07ff75d0645de189ba91fc
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2007 Raul Murciano [http://raul.murciano.net], Domestika INTERNET S.L. [http://domestika.org], 2015 Enric Lluelles [http://enric.lluell.es]
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,231 @@
|
|
1
|
+
# RouteTranslator
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/route_translator)
|
4
|
+
[](https://travis-ci.org/enriclluelles/route_translator)
|
5
|
+
[](https://gemnasium.com/enriclluelles/route_translator)
|
6
|
+
[](https://codeclimate.com/github/enriclluelles/route_translator)
|
7
|
+
[](https://coveralls.io/github/enriclluelles/route_translator?branch=master)
|
8
|
+
|
9
|
+
RouteTranslator is a gem to allow you to manage the translations of your app routes with a simple dictionary format.
|
10
|
+
|
11
|
+
It started as a fork of the awesome [translate_routes](https://github.com/raul/translate_routes) plugin by [Raúl Murciano](https://github.com/raul).
|
12
|
+
|
13
|
+
Right now it works with all the different flavours of rails 3-4 (3.2, 4.0, 4.1, 4.2).
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
## Quick Start
|
18
|
+
|
19
|
+
1. If you have this `routes.rb` file originally:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
MyApp::Application.routes.draw do
|
23
|
+
|
24
|
+
namespace :admin do
|
25
|
+
resources :cars
|
26
|
+
end
|
27
|
+
|
28
|
+
resources :cars
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
The output of `rake routes.rb` would be:
|
33
|
+
|
34
|
+
```
|
35
|
+
admin_cars GET /admin/cars(.:format) admin/cars#index
|
36
|
+
POST /admin/cars(.:format) admin/cars#create
|
37
|
+
new_admin_car GET /admin/cars/new(.:format) admin/cars#new
|
38
|
+
edit_admin_car GET /admin/cars/:id/edit(.:format) admin/cars#edit
|
39
|
+
admin_car GET /admin/cars/:id(.:format) admin/cars#show
|
40
|
+
PUT /admin/cars/:id(.:format) admin/cars#update
|
41
|
+
DELETE /admin/cars/:id(.:format) admin/cars#destroy
|
42
|
+
cars GET /cars(.:format) cars#index
|
43
|
+
POST /cars(.:format) cars#create
|
44
|
+
new_car GET /cars/new(.:format) cars#new
|
45
|
+
edit_car GET /cars/:id/edit(.:format) cars#edit
|
46
|
+
car GET /cars/:id(.:format) cars#show
|
47
|
+
PUT /cars/:id(.:format) cars#update
|
48
|
+
DELETE /cars/:id(.:format) cars#destroy
|
49
|
+
```
|
50
|
+
|
51
|
+
2. Add the gem to your `Gemfile`:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
gem 'route_translator'
|
55
|
+
```
|
56
|
+
|
57
|
+
And execute `bundle install`
|
58
|
+
|
59
|
+
3. Wrap the groups of routes that you want to translate inside a `localized` block:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
MyApp::Application.routes.draw do
|
63
|
+
|
64
|
+
namespace :admin do
|
65
|
+
resources :cars
|
66
|
+
end
|
67
|
+
|
68
|
+
localized do
|
69
|
+
resources :cars
|
70
|
+
|
71
|
+
get 'pricing', to: 'home#pricing', as: :pricing
|
72
|
+
end
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
And add the translations to your locale files, for example:
|
77
|
+
|
78
|
+
```yml
|
79
|
+
es:
|
80
|
+
routes:
|
81
|
+
cars: coches
|
82
|
+
new: nuevo
|
83
|
+
pricing: precios
|
84
|
+
fr:
|
85
|
+
routes:
|
86
|
+
cars: voitures
|
87
|
+
new: nouveau
|
88
|
+
pricing: prix
|
89
|
+
```
|
90
|
+
|
91
|
+
4. Your routes are translated! Here's the output of your `rake routes` now:
|
92
|
+
|
93
|
+
```
|
94
|
+
admin_cars GET /admin/cars(.:format) admin/cars#index
|
95
|
+
POST /admin/cars(.:format) admin/cars#create
|
96
|
+
new_admin_car GET /admin/cars/new(.:format) admin/cars#new
|
97
|
+
edit_admin_car GET /admin/cars/:id/edit(.:format) admin/cars#edit
|
98
|
+
admin_car GET /admin/cars/:id(.:format) admin/cars#show
|
99
|
+
PUT /admin/cars/:id(.:format) admin/cars#update
|
100
|
+
DELETE /admin/cars/:id(.:format) admin/cars#destroy
|
101
|
+
cars_en GET /cars(.:format) cars#index {:locale=>"en"}
|
102
|
+
cars_es GET /es/coches(.:format) cars#index {:locale=>"es"}
|
103
|
+
cars_fr GET /fr/voitures(.:format) cars#index {:locale=>"fr"}
|
104
|
+
POST /cars(.:format) cars#create {:locale=>"en"}
|
105
|
+
POST /es/coches(.:format) cars#create {:locale=>"es"}
|
106
|
+
POST /fr/voitures(.:format) cars#create {:locale=>"fr"}
|
107
|
+
new_car_en GET /cars/new(.:format) cars#new {:locale=>"en"}
|
108
|
+
new_car_es GET /es/coches/nuevo(.:format) cars#new {:locale=>"es"}
|
109
|
+
new_car_fr GET /fr/voitures/nouveau(.:format) cars#new {:locale=>"fr"}
|
110
|
+
edit_car_en GET /cars/:id/edit(.:format) cars#edit {:locale=>"en"}
|
111
|
+
edit_car_es GET /es/coches/:id/edit(.:format) cars#edit {:locale=>"es"}
|
112
|
+
edit_car_fr GET /fr/voitures/:id/edit(.:format) cars#edit {:locale=>"fr"}
|
113
|
+
car_en GET /cars/:id(.:format) cars#show {:locale=>"en"}
|
114
|
+
car_es GET /es/coches/:id(.:format) cars#show {:locale=>"es"}
|
115
|
+
car_fr GET /fr/voitures/:id(.:format) cars#show {:locale=>"fr"}
|
116
|
+
PUT /cars/:id(.:format) cars#update {:locale=>"en"}
|
117
|
+
PUT /es/coches/:id(.:format) cars#update {:locale=>"es"}
|
118
|
+
PUT /fr/voitures/:id(.:format) cars#update {:locale=>"fr"}
|
119
|
+
DELETE /cars/:id(.:format) cars#destroy {:locale=>"en"}
|
120
|
+
DELETE /es/coches/:id(.:format) cars#destroy {:locale=>"es"}
|
121
|
+
DELETE /fr/voitures/:id(.:format) cars#destroy {:locale=>"fr"}
|
122
|
+
```
|
123
|
+
|
124
|
+
Note that only the routes inside a `localized` block are translated.
|
125
|
+
|
126
|
+
In :development environment, I18n is configured by default to not use fallback language.
|
127
|
+
When a translation is missing, it uses the translation key last segment as fallback (`cars` and `new` in this example).
|
128
|
+
|
129
|
+
In :production environment, you should either set `config.i18n.fallbacks = false` or set up translations for your routes in every languages.
|
130
|
+
|
131
|
+
5. Your I18n.locale will be set up automatically from the url param when it's available.
|
132
|
+
|
133
|
+
To disable it add this to your controller:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
skip_around_filter :set_locale_from_url
|
137
|
+
```
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
## Configuration
|
142
|
+
|
143
|
+
You can configure RouteTranslator via an initializer or using the different environment config files.
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
RouteTranslator.config do |config|
|
147
|
+
config.force_locale = true
|
148
|
+
config.locale_param_key = :my_locale
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
### Available Configurations
|
155
|
+
|
156
|
+
* **force_locale**
|
157
|
+
Set this options to `true` to force the locale to be added to all generated route paths, even for the default locale.
|
158
|
+
Defaults to `false`.
|
159
|
+
* **hide_locale**
|
160
|
+
Set this options to `true` to force the locale to be hidden on generated route paths.
|
161
|
+
Defaults to `false`.
|
162
|
+
* **generate_unlocalized_routes**
|
163
|
+
Set this option to `true` to add translated routes without deleting original unlocalized versions.
|
164
|
+
Autosets `force_locale=true`.
|
165
|
+
Defaults to `false`.
|
166
|
+
* **generate_unnamed_unlocalized_routes**
|
167
|
+
Set this option to `true` to add the behavior of **force_locale**, but with a named default route which behaves as if **generate_unlocalized_routes** was `true`.
|
168
|
+
`root_path` will redirect to `/en` or `/es` depending on the value of `I18n.locale`.
|
169
|
+
Defaults to `false`.
|
170
|
+
* **locale_param_key**
|
171
|
+
The param key that will be used to set the locale to the newly generated routes.
|
172
|
+
Defaults to `:locale`
|
173
|
+
* **host_locales**
|
174
|
+
Optional hash to set `I18n.default_locale` based on `request.host`.
|
175
|
+
Useful for apps accepting requests from more than one domain.
|
176
|
+
See below for more details.
|
177
|
+
* **disable_fallback**
|
178
|
+
Set this option to `true` to create only the routes for each locale that have translations.
|
179
|
+
For example if we have `/examples` and a translation is not provided for ES, a route helper of `examples_es` will not be created.
|
180
|
+
Defaults to `false`.
|
181
|
+
Useful when one uses this with a locale route constraint, so non-ES routes can 404 on a Spanish website.
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
### Host-based Locale
|
186
|
+
|
187
|
+
If you have an application serving requests from more than one domain, you might want to set `I18n.default_locale` dynamically based on which domain the request is coming from.
|
188
|
+
|
189
|
+
The `host_locales` option is a hash mapping hosts to locales, with full wildcard support to allow matching multiple domains/subdomains/tlds.
|
190
|
+
Host matching is case insensitive.
|
191
|
+
|
192
|
+
When a request hits your app from a domain matching one of the wild-card matchers defined in `host_locales`, the default_locale will be set to the specified locale.
|
193
|
+
Unless you specified the `force_locale` configuration option to `true`, that locale will be hidden from routes (acting like a dynamic `hide_locale` option).
|
194
|
+
|
195
|
+
Here are a few examples of possible mappings:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
RouteTranslator.config.host_locales =
|
199
|
+
{ # Matches:
|
200
|
+
'*.es' => :es, # TLD: ['domain.es', 'subdomain.domain.es', 'www.long.string.of.subdomains.es'] etc.
|
201
|
+
'ru.wikipedia.*' => :ru, # Subdomain: ['ru.wikipedia.org', 'ru.wikipedia.net', 'ru.wikipedia.com'] etc.
|
202
|
+
'*.subdomain.domain.*' => :ru, # Mixture: ['subdomain.domain.org', 'www.subdomain.domain.net'] etc.
|
203
|
+
'news.bbc.co.uk' => :en, # Exact match: ['news.bbc.co.uk'] only
|
204
|
+
}
|
205
|
+
```
|
206
|
+
|
207
|
+
In the case of a host matching more than once, the order in which the matchers are defined will be taken into account, like so:
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
RouteTranslator.config.host_locales = { 'russia.*' => :ru, '*.com' => :en } # 'russia.com' will have locale :ru
|
211
|
+
RouteTranslator.config.host_locales = { '*.com' => :en, 'russia.*' => :ru } # 'russia.com' will have locale :en
|
212
|
+
```
|
213
|
+
|
214
|
+
If `host_locales` option is set, the following options will be forced (even if you set to true):
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
@config.generate_unlocalized_routes = false
|
218
|
+
@config.generate_unnamed_unlocalized_routes = false
|
219
|
+
@config.force_locale = false
|
220
|
+
@config.hide_locale = false
|
221
|
+
```
|
222
|
+
|
223
|
+
This is to avoid odd behaviour brought about by route conflicts and because `host_locales` forces and hides the host-locale dynamically.
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
## Contributing
|
228
|
+
|
229
|
+
Please read through our [contributing guidelines](CONTRIBUTING.md). Included are directions for opening issues, coding standards, and notes on development.
|
230
|
+
|
231
|
+
More over, if your pull request contains patches or features, you must include relevant unit tests.
|
@@ -28,7 +28,10 @@ module ActionController
|
|
28
28
|
class TestCase
|
29
29
|
include ActionController::UrlFor
|
30
30
|
|
31
|
-
delegate :env, :request, :
|
32
|
-
|
31
|
+
delegate :env, :request, to: :@controller
|
32
|
+
|
33
|
+
def _routes
|
34
|
+
@routes
|
35
|
+
end
|
33
36
|
end
|
34
37
|
end
|
@@ -9,12 +9,12 @@ module ActionDispatch
|
|
9
9
|
@localized = false
|
10
10
|
end
|
11
11
|
|
12
|
-
if instance_methods.map(&:to_s).include?('add_route')
|
12
|
+
if instance_methods.map(&:to_s).include?('add_route'.freeze)
|
13
13
|
def add_route(action, options) # :nodoc:
|
14
14
|
path = path_for_action(action, options.delete(:path))
|
15
15
|
|
16
|
-
if action.to_s =~
|
17
|
-
options[:action] ||= action unless action.to_s.include?(
|
16
|
+
if action.to_s =~ %r{^[\w\/]+$}.freeze
|
17
|
+
options[:action] ||= action unless action.to_s.include?('/'.freeze)
|
18
18
|
else
|
19
19
|
action = nil
|
20
20
|
end
|
@@ -27,7 +27,7 @@ module ActionDispatch
|
|
27
27
|
|
28
28
|
begin
|
29
29
|
mapping = Mapping.new(@set, @scope, path, options)
|
30
|
-
rescue ArgumentError
|
30
|
+
rescue ArgumentError
|
31
31
|
mapping = Mapping.build(@scope, @set, URI.parser.escape(path), options.delete(:as), options)
|
32
32
|
end
|
33
33
|
|
@@ -39,7 +39,7 @@ module ActionDispatch
|
|
39
39
|
end
|
40
40
|
else
|
41
41
|
module Base
|
42
|
-
def match(path, options=nil)
|
42
|
+
def match(path, options = nil)
|
43
43
|
mapping = Mapping.new(@set, @scope, path, options || {})
|
44
44
|
app, conditions, requirements, defaults, as, anchor = mapping.to_route
|
45
45
|
if @localized
|
@@ -1,9 +1,8 @@
|
|
1
1
|
module RouteTranslator
|
2
2
|
module Host
|
3
3
|
def self.locale_from_host(host)
|
4
|
-
locales = RouteTranslator.config.host_locales.
|
5
|
-
|
6
|
-
locales
|
4
|
+
locales = RouteTranslator.config.host_locales.each_with_object([]) do |(pattern, locale), result|
|
5
|
+
result << locale.to_sym if host =~ regex_for(pattern)
|
7
6
|
end
|
8
7
|
locales &= I18n.available_locales
|
9
8
|
(locales.first || I18n.default_locale).to_sym
|
@@ -15,11 +14,11 @@ module RouteTranslator
|
|
15
14
|
end
|
16
15
|
|
17
16
|
def native_locale?(locale)
|
18
|
-
|
17
|
+
locale.to_s.match(/native_/).present?
|
19
18
|
end
|
20
19
|
|
21
20
|
def native_locales
|
22
|
-
config.host_locales.values.map {|locale| :"native_#{locale}" }
|
21
|
+
config.host_locales.values.map { |locale| :"native_#{locale}" }
|
23
22
|
end
|
24
23
|
end
|
25
24
|
end
|
@@ -6,7 +6,7 @@ module RouteTranslator
|
|
6
6
|
# I18n.locale = :fr
|
7
7
|
# people_path -> people_fr_path
|
8
8
|
def self.add_untranslated_helpers_to_controllers_and_views(old_name, named_route_collection)
|
9
|
-
if
|
9
|
+
if named_route_collection.respond_to?(:url_helpers_module)
|
10
10
|
url_helpers_module = named_route_collection.url_helpers_module
|
11
11
|
path_helpers_module = named_route_collection.path_helpers_module
|
12
12
|
url_helpers_list = named_route_collection.helper_names
|
@@ -55,7 +55,7 @@ module RouteTranslator
|
|
55
55
|
new_defaults = defaults.merge(RouteTranslator.locale_param_key => locale.to_s.gsub('native_', ''))
|
56
56
|
new_requirements = requirements.merge(RouteTranslator.locale_param_key => locale.to_s)
|
57
57
|
new_route_name = translate_name(route_name, locale)
|
58
|
-
new_route_name = nil if new_route_name && route_set.named_routes.routes[new_route_name.to_sym] #TODO: Investigate this :(
|
58
|
+
new_route_name = nil if new_route_name && route_set.named_routes.routes[new_route_name.to_sym] # TODO: Investigate this :(
|
59
59
|
block.call(app, new_conditions, new_requirements, new_defaults, new_route_name, anchor)
|
60
60
|
end
|
61
61
|
if RouteTranslator.config.generate_unnamed_unlocalized_routes
|
@@ -66,6 +66,7 @@ module RouteTranslator
|
|
66
66
|
end
|
67
67
|
|
68
68
|
private
|
69
|
+
|
69
70
|
def self.available_locales
|
70
71
|
available_locales = config_locales || I18n.available_locales.dup
|
71
72
|
available_locales.push(*RouteTranslator.native_locales) if RouteTranslator.native_locales.present?
|
@@ -77,15 +78,15 @@ module RouteTranslator
|
|
77
78
|
|
78
79
|
def self.config_locales
|
79
80
|
if RouteTranslator.config.available_locales
|
80
|
-
RouteTranslator.config.available_locales.map
|
81
|
+
RouteTranslator.config.available_locales.map(&:to_sym)
|
81
82
|
end
|
82
83
|
end
|
83
84
|
|
84
85
|
# Translates a path and adds the locale prefix.
|
85
86
|
def self.translate_path(path, locale)
|
86
87
|
new_path = path.dup
|
87
|
-
final_optional_segments = new_path.slice!(
|
88
|
-
translated_segments = new_path.split(
|
88
|
+
final_optional_segments = new_path.slice!(%r{(\([^\/]+\))$})
|
89
|
+
translated_segments = new_path.split(%r{\/|\.}).map { |seg| translate_path_segment(seg, locale) }.select { |seg| !seg.blank? }
|
89
90
|
|
90
91
|
if display_locale?(locale) && !locale_param_present?(new_path)
|
91
92
|
translated_segments.unshift(locale.to_s.downcase)
|
@@ -96,7 +97,7 @@ module RouteTranslator
|
|
96
97
|
memo << separator << segment
|
97
98
|
end
|
98
99
|
|
99
|
-
"/#{joined_segments}#{final_optional_segments}".gsub(
|
100
|
+
"/#{joined_segments}#{final_optional_segments}".gsub(%r{\/\(\/}, '(/')
|
100
101
|
end
|
101
102
|
|
102
103
|
def self.display_locale?(locale)
|
@@ -121,28 +122,31 @@ module RouteTranslator
|
|
121
122
|
# segment is blank, begins with a ":" (param key) or "*" (wildcard),
|
122
123
|
# the segment is returned untouched
|
123
124
|
def self.translate_path_segment(segment, locale)
|
124
|
-
return segment if segment.blank?
|
125
|
+
return segment if segment.blank?
|
126
|
+
named_param, hyphenized = segment.split('-'.freeze, 2) if segment.starts_with?(':'.freeze)
|
127
|
+
return "#{named_param}-#{translate_path_segment(hyphenized.dup, locale)}" if hyphenized
|
128
|
+
return segment if segment.starts_with?('('.freeze) || segment.starts_with?('*'.freeze) || segment.include?(':'.freeze)
|
125
129
|
|
126
130
|
appended_part = segment.slice!(/(\()$/)
|
127
|
-
match = TRANSLATABLE_SEGMENT.match(segment)[1]
|
131
|
+
match = TRANSLATABLE_SEGMENT.match(segment)[1] if TRANSLATABLE_SEGMENT.match(segment)
|
128
132
|
|
129
133
|
(translate_string(match, locale) || segment) + appended_part.to_s
|
130
134
|
end
|
131
135
|
|
132
136
|
def self.translate_string(str, locale)
|
133
137
|
locale = "#{locale}".gsub('native_', '')
|
134
|
-
opts = {:
|
138
|
+
opts = { scope: :routes, locale: locale }
|
135
139
|
if RouteTranslator.config.disable_fallback && locale.to_s != I18n.default_locale.to_s
|
136
140
|
opts[:fallback] = true
|
137
141
|
else
|
138
142
|
opts[:default] = str
|
139
143
|
end
|
140
|
-
res
|
144
|
+
res = I18n.translate(str, opts)
|
141
145
|
URI.escape(res)
|
142
146
|
end
|
143
147
|
|
144
148
|
def self.locale_param_present?(path)
|
145
|
-
!(path.split('/').detect { |segment| segment.to_s == ":#{RouteTranslator.locale_param_key
|
149
|
+
!(path.split('/').detect { |segment| segment.to_s == ":#{RouteTranslator.locale_param_key}" }.nil?)
|
146
150
|
end
|
147
151
|
|
148
152
|
def self.host_locales_option?
|
@@ -150,8 +154,8 @@ module RouteTranslator
|
|
150
154
|
end
|
151
155
|
|
152
156
|
def self.route_name_for(args, old_name, suffix, kaller)
|
153
|
-
args_hash
|
154
|
-
args_locale
|
157
|
+
args_hash = args.detect { |arg| arg.is_a?(Hash) }
|
158
|
+
args_locale = host_locales_option? && args_hash && args_hash[:locale]
|
155
159
|
current_locale_name = I18n.locale.to_s.underscore
|
156
160
|
|
157
161
|
locale = if args_locale
|
data/lib/route_translator.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.1
|
4
|
+
version: 4.2.1
|
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: 2015-
|
12
|
+
date: 2015-12-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -52,19 +52,101 @@ dependencies:
|
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '5.0'
|
54
54
|
- !ruby/object:Gem::Dependency
|
55
|
-
name:
|
55
|
+
name: appraisal
|
56
56
|
requirement: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
58
|
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version:
|
60
|
+
version: '2.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: '2.1'
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: coveralls
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 0.8.10
|
75
|
+
type: :development
|
76
|
+
prerelease: false
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: 0.8.10
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: minitest
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 4.7.5
|
89
|
+
- - "<"
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: 6.0.0
|
92
|
+
type: :development
|
93
|
+
prerelease: false
|
94
|
+
version_requirements: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 4.7.5
|
99
|
+
- - "<"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 6.0.0
|
102
|
+
- !ruby/object:Gem::Dependency
|
103
|
+
name: rails
|
104
|
+
requirement: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '3.2'
|
109
|
+
- - "<"
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '5.0'
|
112
|
+
type: :development
|
113
|
+
prerelease: false
|
114
|
+
version_requirements: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '3.2'
|
119
|
+
- - "<"
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '5.0'
|
122
|
+
- !ruby/object:Gem::Dependency
|
123
|
+
name: rubocop
|
124
|
+
requirement: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - "~>"
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0.35'
|
129
|
+
type: :development
|
130
|
+
prerelease: false
|
131
|
+
version_requirements: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - "~>"
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0.35'
|
136
|
+
- !ruby/object:Gem::Dependency
|
137
|
+
name: simplecov
|
138
|
+
requirement: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - "~>"
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: 0.11.1
|
143
|
+
type: :development
|
144
|
+
prerelease: false
|
145
|
+
version_requirements: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - "~>"
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: 0.11.1
|
68
150
|
description: Translates the Rails routes of your application into the languages defined
|
69
151
|
in your locale files
|
70
152
|
email: enric@lluell.es
|
@@ -72,6 +154,9 @@ executables: []
|
|
72
154
|
extensions: []
|
73
155
|
extra_rdoc_files: []
|
74
156
|
files:
|
157
|
+
- CHANGELOG.md
|
158
|
+
- LICENSE
|
159
|
+
- README.md
|
75
160
|
- lib/route_translator.rb
|
76
161
|
- lib/route_translator/extensions.rb
|
77
162
|
- lib/route_translator/extensions/action_controller.rb
|
@@ -80,32 +165,6 @@ files:
|
|
80
165
|
- lib/route_translator/host.rb
|
81
166
|
- lib/route_translator/translator.rb
|
82
167
|
- lib/route_translator/version.rb
|
83
|
-
- test/dummy/Rakefile
|
84
|
-
- test/dummy/app/controllers/dummy_controller.rb
|
85
|
-
- test/dummy/app/views/dummy/show.html.erb
|
86
|
-
- test/dummy/config.ru
|
87
|
-
- test/dummy/config/application.rb
|
88
|
-
- test/dummy/config/boot.rb
|
89
|
-
- test/dummy/config/environment.rb
|
90
|
-
- test/dummy/config/initializers/secret_token.rb
|
91
|
-
- test/dummy/config/initializers/session_store.rb
|
92
|
-
- test/dummy/config/locales/all.yml
|
93
|
-
- test/dummy/config/routes.rb
|
94
|
-
- test/dummy/dummy_mounted_app.rb
|
95
|
-
- test/dummy/script/rails
|
96
|
-
- test/host_test.rb
|
97
|
-
- test/integration/generated_path_test.rb
|
98
|
-
- test/integration/host_locales_test.rb
|
99
|
-
- test/integration/routing_test.rb
|
100
|
-
- test/integration/thread_safety_test.rb
|
101
|
-
- test/locales/routes.yml
|
102
|
-
- test/routing_test.rb
|
103
|
-
- test/support/assertion_helper.rb
|
104
|
-
- test/support/configuration_helper.rb
|
105
|
-
- test/support/i18n_helper.rb
|
106
|
-
- test/support/integration_helper.rb
|
107
|
-
- test/support/routes_helper.rb
|
108
|
-
- test/test_helper.rb
|
109
168
|
homepage: http://github.com/enriclluelles/route_translator
|
110
169
|
licenses:
|
111
170
|
- MIT
|
@@ -126,34 +185,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
185
|
version: '0'
|
127
186
|
requirements: []
|
128
187
|
rubyforge_project:
|
129
|
-
rubygems_version: 2.4.
|
188
|
+
rubygems_version: 2.4.3
|
130
189
|
signing_key:
|
131
190
|
specification_version: 4
|
132
191
|
summary: Translate your Rails routes in a simple manner
|
133
|
-
test_files:
|
134
|
-
- test/dummy/Rakefile
|
135
|
-
- test/dummy/app/controllers/dummy_controller.rb
|
136
|
-
- test/dummy/app/views/dummy/show.html.erb
|
137
|
-
- test/dummy/config.ru
|
138
|
-
- test/dummy/config/application.rb
|
139
|
-
- test/dummy/config/boot.rb
|
140
|
-
- test/dummy/config/environment.rb
|
141
|
-
- test/dummy/config/initializers/secret_token.rb
|
142
|
-
- test/dummy/config/initializers/session_store.rb
|
143
|
-
- test/dummy/config/locales/all.yml
|
144
|
-
- test/dummy/config/routes.rb
|
145
|
-
- test/dummy/dummy_mounted_app.rb
|
146
|
-
- test/dummy/script/rails
|
147
|
-
- test/host_test.rb
|
148
|
-
- test/integration/generated_path_test.rb
|
149
|
-
- test/integration/host_locales_test.rb
|
150
|
-
- test/integration/routing_test.rb
|
151
|
-
- test/integration/thread_safety_test.rb
|
152
|
-
- test/locales/routes.yml
|
153
|
-
- test/routing_test.rb
|
154
|
-
- test/support/assertion_helper.rb
|
155
|
-
- test/support/configuration_helper.rb
|
156
|
-
- test/support/i18n_helper.rb
|
157
|
-
- test/support/integration_helper.rb
|
158
|
-
- test/support/routes_helper.rb
|
159
|
-
- test/test_helper.rb
|
192
|
+
test_files: []
|