route_translator 5.0.5 → 5.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3b566e8efb88b086812e1308e50a25a665b5b1a
4
- data.tar.gz: 43ba2e28fd4e1b24b3451c54e8aae47ad4961693
3
+ metadata.gz: 2e5491f7135951007ef950f929f41ce035e327e5
4
+ data.tar.gz: c92a06d7f82220a625e9efca1efce38630429759
5
5
  SHA512:
6
- metadata.gz: 5588fc472c0c4b192b7610e8a31091fbdaa93c9ba57749e8e281d10744537f52f3a24a8e913837bb83ad2f3230cf8c950ba0e709977da332204376b050cd2bec
7
- data.tar.gz: fe53b0f81af8176c9f1cf53d07b869b0a18da4d3bbe09a29f7868faedcd3416e2999557b9b2b95a17dca1ad48f5632b5e59704050cb2bc60d40ef46bde6f3bf3
6
+ metadata.gz: 0a67bc52ece0fa4a69c7d4b5ccb0407b92104f53182fb0e87a5a143f9fb2c9753772ae763ae1e9f49e756214c91da93f769845022cba850fe2be50dae5f749e2
7
+ data.tar.gz: c06d96a8c9a72f6dab4b6258ee0062f774ca31a4bc24cedab06ba8346ff5afc61e697ac62915b7ab33e6015478753b0d1532ddeeee5cb4c020fb0394cbc70965
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 5.0.6 (2017-01-17)
4
+
5
+ * Test against Ruby 2.4.0
6
+ * Update copyright in LICENSE
7
+ * Improve Translator method
8
+ * Minor improvements
9
+
3
10
  ## 5.0.5 (2016-12-15)
4
11
 
5
12
  * Fix generated prefix to localized one (#123)
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
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], 2016 Geremia Taglialatela
3
+ Copyright (c) 2007 Raul Murciano [http://raul.murciano.net], Domestika INTERNET S.L. [http://domestika.org], 2015 Enric Lluelles [http://enric.lluell.es], 2017 Geremia Taglialatela
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -89,34 +89,42 @@ Right now it works with Rails 5.0
89
89
  4. Your routes are translated! Here's the output of your `rake routes` now:
90
90
 
91
91
  ```
92
+ Prefix Verb URI Pattern Controller#Action
92
93
  admin_cars GET /admin/cars(.:format) admin/cars#index
93
94
  POST /admin/cars(.:format) admin/cars#create
94
95
  new_admin_car GET /admin/cars/new(.:format) admin/cars#new
95
96
  edit_admin_car GET /admin/cars/:id/edit(.:format) admin/cars#edit
96
97
  admin_car GET /admin/cars/:id(.:format) admin/cars#show
98
+ PATCH /admin/cars/:id(.:format) admin/cars#update
97
99
  PUT /admin/cars/:id(.:format) admin/cars#update
98
100
  DELETE /admin/cars/:id(.:format) admin/cars#destroy
99
- cars_en GET /cars(.:format) cars#index {:locale=>"en"}
100
- cars_es GET /es/coches(.:format) cars#index {:locale=>"es"}
101
101
  cars_fr GET /fr/voitures(.:format) cars#index {:locale=>"fr"}
102
- POST /cars(.:format) cars#create {:locale=>"en"}
103
- POST /es/coches(.:format) cars#create {:locale=>"es"}
102
+ cars_es GET /es/coches(.:format) cars#index {:locale=>"es"}
103
+ cars_en GET /cars(.:format) cars#index {:locale=>"en"}
104
104
  POST /fr/voitures(.:format) cars#create {:locale=>"fr"}
105
- new_car_en GET /cars/new(.:format) cars#new {:locale=>"en"}
106
- new_car_es GET /es/coches/nuevo(.:format) cars#new {:locale=>"es"}
105
+ POST /es/coches(.:format) cars#create {:locale=>"es"}
106
+ POST /cars(.:format) cars#create {:locale=>"en"}
107
107
  new_car_fr GET /fr/voitures/nouveau(.:format) cars#new {:locale=>"fr"}
108
- edit_car_en GET /cars/:id/edit(.:format) cars#edit {:locale=>"en"}
109
- edit_car_es GET /es/coches/:id/edit(.:format) cars#edit {:locale=>"es"}
108
+ new_car_es GET /es/coches/nuevo(.:format) cars#new {:locale=>"es"}
109
+ new_car_en GET /cars/new(.:format) cars#new {:locale=>"en"}
110
110
  edit_car_fr GET /fr/voitures/:id/edit(.:format) cars#edit {:locale=>"fr"}
111
- car_en GET /cars/:id(.:format) cars#show {:locale=>"en"}
112
- car_es GET /es/coches/:id(.:format) cars#show {:locale=>"es"}
111
+ edit_car_es GET /es/coches/:id/edit(.:format) cars#edit {:locale=>"es"}
112
+ edit_car_en GET /cars/:id/edit(.:format) cars#edit {:locale=>"en"}
113
113
  car_fr GET /fr/voitures/:id(.:format) cars#show {:locale=>"fr"}
114
- PUT /cars/:id(.:format) cars#update {:locale=>"en"}
115
- PUT /es/coches/:id(.:format) cars#update {:locale=>"es"}
114
+ car_es GET /es/coches/:id(.:format) cars#show {:locale=>"es"}
115
+ car_en GET /cars/:id(.:format) cars#show {:locale=>"en"}
116
+ PATCH /fr/voitures/:id(.:format) cars#update {:locale=>"fr"}
117
+ PATCH /es/coches/:id(.:format) cars#update {:locale=>"es"}
118
+ PATCH /cars/:id(.:format) cars#update {:locale=>"en"}
116
119
  PUT /fr/voitures/:id(.:format) cars#update {:locale=>"fr"}
117
- DELETE /cars/:id(.:format) cars#destroy {:locale=>"en"}
118
- DELETE /es/coches/:id(.:format) cars#destroy {:locale=>"es"}
120
+ PUT /es/coches/:id(.:format) cars#update {:locale=>"es"}
121
+ PUT /cars/:id(.:format) cars#update {:locale=>"en"}
119
122
  DELETE /fr/voitures/:id(.:format) cars#destroy {:locale=>"fr"}
123
+ DELETE /es/coches/:id(.:format) cars#destroy {:locale=>"es"}
124
+ DELETE /cars/:id(.:format) cars#destroy {:locale=>"en"}
125
+ pricing_fr GET /fr/prix(.:format) home#pricing {:locale=>"fr"}
126
+ pricing_es GET /es/precios(.:format) home#pricing {:locale=>"es"}
127
+ pricing_en GET /pricing(.:format) home#pricing {:locale=>"en"}
120
128
  ```
121
129
 
122
130
  Note that only the routes inside a `localized` block are translated.
@@ -135,6 +143,101 @@ Right now it works with Rails 5.0
135
143
  ```
136
144
 
137
145
 
146
+ ### Namespaces
147
+
148
+ You can translate a namespace route by either its `name` or `path` option:
149
+
150
+ 1. Wrap the namespaces that you want to translate inside a `localized` block:
151
+
152
+ ```ruby
153
+ Rails.application.routes.draw do
154
+ localized do
155
+ namespace :admin do
156
+ resources :cars, only: :index
157
+ end
158
+
159
+ namespace :sold_cars, path: :sold do
160
+ resources :cars, only: :index
161
+ end
162
+ end
163
+ end
164
+ ```
165
+
166
+ And add the translations to your locale files, for example:
167
+
168
+ ```yml
169
+ es:
170
+ routes:
171
+ admin: administrador
172
+ cars: coches
173
+ new: nuevo
174
+ pricing: precios
175
+ sold: vendidos
176
+ fr:
177
+ routes:
178
+ admin: administrateur
179
+ cars: voitures
180
+ new: nouveau
181
+ pricing: prix
182
+ sold: vendues
183
+ ```
184
+
185
+ 4. Your namespaces are translated! Here's the output of your `rake routes` now:
186
+
187
+ ```
188
+ Prefix Verb URI Pattern Controller#Action
189
+ admin_cars_fr GET /fr/administrateur/voitures(.:format) admin/cars#index {:locale=>"fr"}
190
+ admin_cars_es GET /es/administrador/coches(.:format) admin/cars#index {:locale=>"es"}
191
+ admin_cars_en GET /admin/cars(.:format) admin/cars#index {:locale=>"en"}
192
+ sold_cars_cars_fr GET /fr/vendues/voitures(.:format) sold_cars/cars#index {:locale=>"fr"}
193
+ sold_cars_cars_es GET /es/vendidos/coches(.:format) sold_cars/cars#index {:locale=>"es"}
194
+ sold_cars_cars_en GET /sold/cars(.:format) sold_cars/cars#index {:locale=>"en"}
195
+ ```
196
+
197
+
198
+ ### Inflections
199
+
200
+ At the moment inflections are not supported, but you can use the following workaround:
201
+
202
+ ```ruby
203
+ localized do
204
+ resources :categories, path_names: { new: 'new_category' }
205
+ end
206
+ ```
207
+
208
+ ```yml
209
+ en:
210
+ routes:
211
+ category: category
212
+ new_category: new
213
+
214
+ es:
215
+ routes:
216
+ category: categoria
217
+ new_category: nueva
218
+ ```
219
+
220
+ ```
221
+ Prefix Verb URI Pattern Controller#Action
222
+ categories_es GET /es/categorias(.:format) categories#index {:locale=>"es"}
223
+ categories_en GET /categories(.:format) categories#index {:locale=>"en"}
224
+ POST /es/categorias(.:format) categories#create {:locale=>"es"}
225
+ POST /categories(.:format) categories#create {:locale=>"en"}
226
+ new_category_es GET /es/categorias/nueva(.:format) categories#new {:locale=>"es"}
227
+ new_category_en GET /categories/new(.:format) categories#new {:locale=>"en"}
228
+ edit_category_es GET /es/categorias/:id/edit(.:format) categories#edit {:locale=>"es"}
229
+ edit_category_en GET /categories/:id/edit(.:format) categories#edit {:locale=>"en"}
230
+ category_es GET /es/categorias/:id(.:format) categories#show {:locale=>"es"}
231
+ category_en GET /categories/:id(.:format) categories#show {:locale=>"en"}
232
+ PATCH /es/categorias/:id(.:format) categories#update {:locale=>"es"}
233
+ PATCH /categories/:id(.:format) categories#update {:locale=>"en"}
234
+ PUT /es/categorias/:id(.:format) categories#update {:locale=>"es"}
235
+ PUT /categories/:id(.:format) categories#update {:locale=>"en"}
236
+ DELETE /es/categorias/:id(.:format) categories#destroy {:locale=>"es"}
237
+ DELETE /categories/:id(.:format) categories#destroy {:locale=>"en"}
238
+ ```
239
+
240
+
138
241
 
139
242
  ## Configuration
140
243
 
@@ -148,7 +251,6 @@ end
148
251
  ```
149
252
 
150
253
 
151
-
152
254
  ### Available Configurations
153
255
 
154
256
  * **force_locale**
@@ -224,7 +326,9 @@ If `host_locales` option is set, the following options will be forced (even if y
224
326
 
225
327
  This is to avoid odd behaviour brought about by route conflicts and because `host_locales` forces and hides the host-locale dynamically.
226
328
 
227
- ### Testing
329
+
330
+
331
+ ## Testing
228
332
  Testing your controllers with routes-translator is easy, just add a locale parameter for your localized routes. Otherwise, an ActionController::UrlGenerationError will raise.
229
333
 
230
334
  ```ruby
@@ -237,6 +341,8 @@ describe 'GET index' do
237
341
  end
238
342
  ```
239
343
 
344
+
345
+
240
346
  ## Contributing
241
347
 
242
348
  Please read through our [contributing guidelines](CONTRIBUTING.md). Included are directions for opening issues, coding standards, and notes on development.
@@ -9,19 +9,19 @@ module RouteTranslator
9
9
 
10
10
  TRANSLATABLE_SEGMENT = /^([-_a-zA-Z0-9]+)(\()?/
11
11
 
12
- Configuration = Struct.new(:force_locale, :hide_locale,
13
- :generate_unlocalized_routes, :locale_param_key,
14
- :generate_unnamed_unlocalized_routes, :available_locales,
15
- :host_locales, :disable_fallback, :locale_segment_proc)
12
+ Configuration = Struct.new(:available_locales, :disable_fallback, :force_locale,
13
+ :hide_locale, :host_locales, :generate_unlocalized_routes,
14
+ :generate_unnamed_unlocalized_routes, :locale_param_key,
15
+ :locale_segment_proc)
16
16
 
17
17
  class << self
18
18
  private
19
19
 
20
20
  def resolve_host_locale_config_conflicts
21
- @config.generate_unlocalized_routes = false
22
- @config.generate_unnamed_unlocalized_routes = false
23
21
  @config.force_locale = false
24
22
  @config.hide_locale = false
23
+ @config.generate_unlocalized_routes = false
24
+ @config.generate_unnamed_unlocalized_routes = false
25
25
  end
26
26
  end
27
27
 
@@ -29,22 +29,21 @@ module RouteTranslator
29
29
 
30
30
  def config(&block)
31
31
  @config ||= Configuration.new
32
+ @config.available_locales ||= []
33
+ @config.disable_fallback ||= false
32
34
  @config.force_locale ||= false
33
35
  @config.hide_locale ||= false
36
+ @config.host_locales ||= ActiveSupport::OrderedHash.new
34
37
  @config.generate_unlocalized_routes ||= false
35
- @config.locale_param_key ||= :locale
36
38
  @config.generate_unnamed_unlocalized_routes ||= false
37
- @config.host_locales ||= ActiveSupport::OrderedHash.new
38
- @config.available_locales ||= []
39
- @config.disable_fallback ||= false
39
+ @config.locale_param_key ||= :locale
40
40
  @config.locale_segment_proc ||= nil
41
+
41
42
  yield @config if block
43
+
42
44
  resolve_host_locale_config_conflicts unless @config.host_locales.empty?
43
- @config
44
- end
45
45
 
46
- def locale_param_key
47
- config.locale_param_key
46
+ @config
48
47
  end
49
48
 
50
49
  def available_locales
@@ -56,4 +55,8 @@ module RouteTranslator
56
55
  I18n.available_locales.dup
57
56
  end
58
57
  end
58
+
59
+ def locale_param_key
60
+ config.locale_param_key
61
+ end
59
62
  end
@@ -37,7 +37,6 @@ module ActionDispatch
37
37
  ast = Journey::Parser.parse path
38
38
 
39
39
  mapping = Mapping.build(@scope, @set, ast, controller, default_action, to, via, formatted, options_constraints, anchor, options)
40
-
41
40
  @set.add_localized_route(mapping, ast, as, anchor, @scope, path, controller, default_action, to, via, formatted, options_constraints, options)
42
41
  end
43
42
  # rubocop:enable Lint/UnderscorePrefixedVariableName
@@ -13,13 +13,36 @@ module RouteTranslator
13
13
  end
14
14
 
15
15
  def translate_name(name, locale, named_routes_names)
16
- return unless name.present?
16
+ return if name.blank?
17
17
  translated_name = "#{name}_#{locale.to_s.underscore}"
18
- if named_routes_names.include?(translated_name.to_sym)
19
- nil
20
- else
21
- translated_name
18
+
19
+ translated_name if named_routes_names.exclude?(translated_name.to_sym)
20
+ end
21
+
22
+ def translate_options(options, locale)
23
+ translated_options = options.dup
24
+
25
+ if translated_options.exclude?(RouteTranslator.locale_param_key)
26
+ translated_options[RouteTranslator.locale_param_key] = locale.to_s.gsub('native_', '')
22
27
  end
28
+
29
+ translated_options
30
+ end
31
+
32
+ def translate_options_constraints(options_constraints, locale)
33
+ translated_options_constraints = options_constraints.dup
34
+
35
+ if translated_options_constraints.respond_to?(:[]=)
36
+ translated_options_constraints[RouteTranslator.locale_param_key] = locale.to_s
37
+ end
38
+
39
+ translated_options_constraints
40
+ end
41
+
42
+ def translate_path(path, locale)
43
+ RouteTranslator::Translator::Path.translate(path, locale)
44
+ rescue I18n::MissingTranslationData => e
45
+ raise e unless RouteTranslator.config.disable_fallback
23
46
  end
24
47
  end
25
48
 
@@ -39,23 +62,12 @@ module RouteTranslator
39
62
  RouteTranslator::Translator::RouteHelpers.add name, route_set.named_routes
40
63
 
41
64
  available_locales.each do |locale|
42
- begin
43
- translated_path = RouteTranslator::Translator::Path.translate(path, locale)
44
- rescue I18n::MissingTranslationData => e
45
- raise e unless RouteTranslator.config.disable_fallback
46
- next
47
- end
48
-
49
- translated_options_constraints = options_constraints.dup
50
- translated_options = options.dup
51
-
52
- if translated_options_constraints.respond_to?(:[]=)
53
- translated_options_constraints[RouteTranslator.locale_param_key] = locale.to_s
54
- end
55
-
56
- translated_options[RouteTranslator.locale_param_key] = locale.to_s.gsub('native_', '') unless translated_options.include?(RouteTranslator.locale_param_key)
65
+ translated_path = translate_path(path, locale)
66
+ next unless translated_path
57
67
 
58
- translated_name = translate_name(name, locale, route_set.named_routes.names)
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)
59
71
 
60
72
  yield translated_name, translated_path, translated_options_constraints, translated_options
61
73
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module RouteTranslator
3
- VERSION = '5.0.5'.freeze
3
+ VERSION = '5.0.6'.freeze
4
4
  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.5
4
+ version: 5.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geremia Taglialatela
@@ -10,48 +10,36 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-12-15 00:00:00.000000000 Z
13
+ date: 2017-01-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - ">="
19
+ - - "~>"
20
20
  - !ruby/object:Gem::Version
21
21
  version: 5.0.0
22
- - - "<"
23
- - !ruby/object:Gem::Version
24
- version: '5.1'
25
22
  type: :runtime
26
23
  prerelease: false
27
24
  version_requirements: !ruby/object:Gem::Requirement
28
25
  requirements:
29
- - - ">="
26
+ - - "~>"
30
27
  - !ruby/object:Gem::Version
31
28
  version: 5.0.0
32
- - - "<"
33
- - !ruby/object:Gem::Version
34
- version: '5.1'
35
29
  - !ruby/object:Gem::Dependency
36
30
  name: actionpack
37
31
  requirement: !ruby/object:Gem::Requirement
38
32
  requirements:
39
- - - ">="
33
+ - - "~>"
40
34
  - !ruby/object:Gem::Version
41
35
  version: 5.0.0
42
- - - "<"
43
- - !ruby/object:Gem::Version
44
- version: '5.1'
45
36
  type: :runtime
46
37
  prerelease: false
47
38
  version_requirements: !ruby/object:Gem::Requirement
48
39
  requirements:
49
- - - ">="
40
+ - - "~>"
50
41
  - !ruby/object:Gem::Version
51
42
  version: 5.0.0
52
- - - "<"
53
- - !ruby/object:Gem::Version
54
- version: '5.1'
55
43
  - !ruby/object:Gem::Dependency
56
44
  name: appraisal
57
45
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +74,14 @@ dependencies:
86
74
  requirements:
87
75
  - - "~>"
88
76
  - !ruby/object:Gem::Version
89
- version: 0.8.17
77
+ version: 0.8.18
90
78
  type: :development
91
79
  prerelease: false
92
80
  version_requirements: !ruby/object:Gem::Requirement
93
81
  requirements:
94
82
  - - "~>"
95
83
  - !ruby/object:Gem::Version
96
- version: 0.8.17
84
+ version: 0.8.18
97
85
  - !ruby/object:Gem::Dependency
98
86
  name: minitest
99
87
  requirement: !ruby/object:Gem::Requirement
@@ -118,22 +106,16 @@ dependencies:
118
106
  name: rails
119
107
  requirement: !ruby/object:Gem::Requirement
120
108
  requirements:
121
- - - ">="
109
+ - - "~>"
122
110
  - !ruby/object:Gem::Version
123
111
  version: 5.0.0
124
- - - "<"
125
- - !ruby/object:Gem::Version
126
- version: '5.1'
127
112
  type: :development
128
113
  prerelease: false
129
114
  version_requirements: !ruby/object:Gem::Requirement
130
115
  requirements:
131
- - - ">="
116
+ - - "~>"
132
117
  - !ruby/object:Gem::Version
133
118
  version: 5.0.0
134
- - - "<"
135
- - !ruby/object:Gem::Version
136
- version: '5.1'
137
119
  - !ruby/object:Gem::Dependency
138
120
  name: rake
139
121
  requirement: !ruby/object:Gem::Requirement
@@ -154,14 +136,14 @@ dependencies:
154
136
  requirements:
155
137
  - - "~>"
156
138
  - !ruby/object:Gem::Version
157
- version: 0.46.0
139
+ version: 0.47.0
158
140
  type: :development
159
141
  prerelease: false
160
142
  version_requirements: !ruby/object:Gem::Requirement
161
143
  requirements:
162
144
  - - "~>"
163
145
  - !ruby/object:Gem::Version
164
- version: 0.46.0
146
+ version: 0.47.0
165
147
  - !ruby/object:Gem::Dependency
166
148
  name: simplecov
167
149
  requirement: !ruby/object:Gem::Requirement
@@ -219,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
201
  version: '0'
220
202
  requirements: []
221
203
  rubyforge_project:
222
- rubygems_version: 2.5.2
204
+ rubygems_version: 2.6.8
223
205
  signing_key:
224
206
  specification_version: 4
225
207
  summary: Translate your Rails routes in a simple manner