route_translator 6.0.0 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 453269553066b53ab821eb7c4aa8bce1d9ad4aca1d34d1bc212dc7ca9d70e76a
4
- data.tar.gz: '087901c13e29e93f71dc374755afb08f4376d6a4e0c43ccf06bc4a1640325dcf'
3
+ metadata.gz: b48ef0fed8291cf19c3f8bbbc03276061bbebfeca5ebe69f7cec013e1ec2e41c
4
+ data.tar.gz: 7601140f98d82f866b9f32a801c1057f0949390df7a0832f8cf8331e1c54c2fd
5
5
  SHA512:
6
- metadata.gz: 6c43b4217c536688e249f2669c4c814514f5d753caca7fd4185a4b83b8695be73aded8ae39864b59fcc2dfee60007c731bb38df7e52a1c71105d2a6388b1b5b0
7
- data.tar.gz: 96cb347e03fcd211d9ba41ee90e2b6d36db6f7b1881d65a609787bc73c17cb1cdddd017cd413c5230f58c9a02f23eebafe7f09de79a996ca65757057f9f5e57b
6
+ metadata.gz: 823890c7d82fe2918f5eef719ecd3ba204875d68f25dc5659f29870e80db5246926225c47dc79f2af46e64b936a82d9d508bcdfa2eeb1e7be8b0bf63a1ad3414
7
+ data.tar.gz: 3a0b3401cedb34b0fe9884cbc15f26a3decea8c3984152512869b00ea0cf9d3f26c2db7c275ec110db94338774796cee1417eba97e8b7837ebb045e0ef3856f4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 7.0.0 / 2019-07-30
4
+
5
+ * [FEATURE] Add Rails > 6.0.0.rc1 compatibility
6
+ * [FEATURE] Do not set locale from url by default
7
+ * [FEATURE] Do not raise InvalidLocale errors
8
+ * [ENHANCEMENT] Update development dependencies
9
+
3
10
  ## 6.0.0 / 2019-05-16
4
11
 
5
12
  * [FEATURE] Drop Ruby 2.2 support
data/README.md CHANGED
@@ -133,14 +133,16 @@ Right now it works with Rails 5.x and Rails 6.0
133
133
 
134
134
  In :production environment, you should either set `config.i18n.fallbacks = false` or set up translations for your routes in every languages.
135
135
 
136
- 5. Your I18n.locale will be set up automatically from the url param when it's available.
137
-
138
- To disable it add this to your controller:
136
+ 5. If you want to set `I18n.locale` from the url parameter locale, add
137
+ the following line in your `ApplicationController` or in the controllers
138
+ that have translated content:
139
139
 
140
140
  ```ruby
141
- skip_around_action :set_locale_from_url
141
+ around_action :set_locale_from_url
142
142
  ```
143
143
 
144
+ Note: you might be tempted to use `before_action` instead of `around_action`: just don't. That could lead to [thread-related issues](https://github.com/enriclluelles/route_translator/issues/44).
145
+
144
146
 
145
147
  ### Namespaces
146
148
 
@@ -252,50 +254,28 @@ end
252
254
 
253
255
  ### Available Configurations
254
256
 
255
- * **force_locale**
256
- Set this options to `true` to force the locale to be added to all generated route paths, even for the default locale.
257
- Defaults to `false`.
258
- * **hide_locale**
259
- Set this options to `true` to force the locale to be hidden on generated route paths.
260
- Defaults to `false`.
261
- * **generate_unlocalized_routes**
262
- Set this option to `true` to add translated routes without deleting original unlocalized versions.
263
- Autosets `force_locale=true`.
264
- Defaults to `false`.
265
- * **generate_unnamed_unlocalized_routes**
266
- 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`.
267
- `root_path` will redirect to `/en` or `/es` depending on the value of `I18n.locale`.
268
- Defaults to `false`.
269
- * **locale_param_key**
270
- The param key that will be used to set the locale to the newly generated routes.
271
- Defaults to `:locale`
272
- * **host_locales**
273
- Optional hash to set `I18n.default_locale` based on `request.host`.
274
- Useful for apps accepting requests from more than one domain.
275
- See below for more details.
276
- * **disable_fallback**
277
- Set this option to `true` to create only the routes for each locale that have translations.
278
- For example if we have `/examples` and a translation is not provided for ES, a route helper of `examples_es` will not be created.
279
- Defaults to `false`.
280
- Useful when one uses this with a locale route constraint, so non-ES routes can 404 on a Spanish website.
281
- * **available_locales**
282
- Use this to limit the locales for which URLs should be generated for. Accepts an array of strings or symbols.
283
- * **locale_segment_proc**
284
- The locale segment of the url will by default be `locale.to_s.downcase`
285
- You can supply your own mechanism via a Proc that takes `locale` as an argument, e.g. `config.locale_segment_proc = ->(locale) { locale.to_s.upcase }`
286
- * **verify_host_path_consistency**
287
- By default, if you use different hosts to translate your application, all translated paths will work on all hosts. Set this option to `true` to force
288
- a matching of the host associated locale with the translated path locale as part of the route definition.
289
- Defaults to `false`.
257
+ | Option | Description | Default |
258
+ | ------ | ----------- |-------- |
259
+ | `available_locales` | Limits the locales for which URLs should be generated for. Accepts an array of strings or symbols. When empty, translations will be generated for all `I18n.available_locales` | `[]` |
260
+ | `disable_fallback` | Creates routes only for locales that have translations. For example, if we have `/examples` and a translation is not provided for `es`, the route helper of `examples_es` will not be created. Useful when one uses this with a locale route constraint, so non-`es` routes can return a `404` on a Spanish website | `false` |
261
+ | `force_locale` | Forces the locale to be added to all generated route paths, even for the default locale | `false` |
262
+ | `generate_unlocalized_routes` | Adds translated routes without deleting original unlocalized versions. **Note:** Autosets `force_locale` to `true` | `false` |
263
+ | `generate_unnamed_unlocalized_routes` | Adds the behavior of `force_locale`, but with a named default route which behaves as if `generate_unlocalized_routes` was `true`. `root_path` will redirect to `/en` or `/es`, depending on the value of `I18n.locale` | `false` |
264
+ | `hide_locale` | Forces the locale to be hidden on generated route paths | `false` |
265
+ | `host_locales` | Sets `I18n.locale` based on `request.host`. Useful for apps accepting requests from more than one domain. See below for more details | `{}` |
266
+ | `locale_param_key` | The param key used to set the locale to the newly generated routes | `:locale` |
267
+ | `locale_segment_proc` | The locale segment of the url will by default be `locale.to_s.downcase`. You can supply your own mechanism via a Proc that takes `locale` as an argument, e.g. `->(locale) { locale.to_s.upcase }` | `false` |
268
+ | `verify_host_path_consistency` | Forces a matching of the host associated locale with the translated path locale as part of the route definition. By default, if you use different hosts to translate your application, all translated paths will work on all hosts | `false` |
269
+
290
270
 
291
271
  ### Host-based Locale
292
272
 
293
- 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.
273
+ If you have an application serving requests from more than one domain, you might want to set `I18n.locale` dynamically based on which domain the request is coming from.
294
274
 
295
275
  The `host_locales` option is a hash mapping hosts to locales, with full wildcard support to allow matching multiple domains/subdomains/tlds.
296
276
  Host matching is case insensitive.
297
277
 
298
- 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.
278
+ When a request hits your app from a domain matching one of the wild-card matchers defined in `host_locales`, the locale will be set to the specified locale.
299
279
  Unless you specified the `force_locale` configuration option to `true`, that locale will be hidden from routes (acting like a dynamic `hide_locale` option).
300
280
 
301
281
  Here are a few examples of possible mappings:
@@ -320,14 +300,16 @@ RouteTranslator.config.host_locales = { '*.com' => :en, 'russia.*' => :ru } #
320
300
  If `host_locales` option is set, the following options will be forced (even if you set to true):
321
301
 
322
302
  ```ruby
303
+ @config.force_locale = false
323
304
  @config.generate_unlocalized_routes = false
324
305
  @config.generate_unnamed_unlocalized_routes = false
325
- @config.force_locale = false
326
306
  @config.hide_locale = false
327
307
  ```
328
308
 
329
309
  This is to avoid odd behaviour brought about by route conflicts and because `host_locales` forces and hides the host-locale dynamically.
330
310
 
311
+ NOTE: locale from parameters has priority over the one from hosts.
312
+
331
313
 
332
314
  ### Translations for similar routes with different namespaces
333
315
 
@@ -13,36 +13,40 @@ module RouteTranslator
13
13
 
14
14
  TRANSLATABLE_SEGMENT = /^([-_a-zA-Z0-9]+)(\()?/.freeze
15
15
 
16
- Configuration = Struct.new(:available_locales, :disable_fallback, :force_locale,
17
- :hide_locale, :host_locales, :generate_unlocalized_routes,
18
- :generate_unnamed_unlocalized_routes, :locale_param_key,
19
- :locale_segment_proc, :verify_host_path_consistency)
16
+ DEFAULT_CONFIGURATION = {
17
+ available_locales: [],
18
+ disable_fallback: false,
19
+ force_locale: false,
20
+ generate_unlocalized_routes: false,
21
+ generate_unnamed_unlocalized_routes: false,
22
+ hide_locale: false,
23
+ host_locales: {},
24
+ locale_param_key: :locale,
25
+ locale_segment_proc: false,
26
+ verify_host_path_consistency: false
27
+ }.freeze
28
+
29
+ Configuration = Struct.new(*DEFAULT_CONFIGURATION.keys)
20
30
 
21
31
  class << self
22
32
  private
23
33
 
24
34
  def resolve_host_locale_config_conflicts
25
35
  @config.force_locale = false
26
- @config.hide_locale = false
27
36
  @config.generate_unlocalized_routes = false
28
37
  @config.generate_unnamed_unlocalized_routes = false
38
+ @config.hide_locale = false
29
39
  end
30
40
  end
31
41
 
32
42
  module_function
33
43
 
34
44
  def config(&block)
35
- @config ||= Configuration.new
36
- @config.available_locales ||= []
37
- @config.disable_fallback ||= false
38
- @config.force_locale ||= false
39
- @config.hide_locale ||= false
40
- @config.host_locales ||= ActiveSupport::OrderedHash.new
41
- @config.generate_unlocalized_routes ||= false
42
- @config.generate_unnamed_unlocalized_routes ||= false
43
- @config.locale_param_key ||= :locale
44
- @config.locale_segment_proc ||= nil
45
- @config.verify_host_path_consistency ||= false
45
+ @config ||= Configuration.new
46
+
47
+ DEFAULT_CONFIGURATION.each do |option, value|
48
+ @config[option] ||= value
49
+ end
46
50
 
47
51
  yield @config if block
48
52
 
@@ -64,4 +68,9 @@ module RouteTranslator
64
68
  def locale_param_key
65
69
  config.locale_param_key
66
70
  end
71
+
72
+ def locale_from_params(params)
73
+ locale = params[config.locale_param_key]&.to_sym
74
+ locale if I18n.available_locales.include?(locale)
75
+ end
67
76
  end
@@ -7,22 +7,18 @@ module RouteTranslator
7
7
  module Controller
8
8
  extend ActiveSupport::Concern
9
9
 
10
- included do
11
- around_action :set_locale_from_url
12
- end
13
-
14
10
  private
15
11
 
16
12
  def set_locale_from_url
17
- tmp_locale = params[RouteTranslator.locale_param_key] || RouteTranslator::Host.locale_from_host(request.host)
18
- if tmp_locale
19
- current_locale = I18n.locale
20
- I18n.locale = tmp_locale
13
+ locale_from_url = RouteTranslator.locale_from_params(params) || RouteTranslator::Host.locale_from_host(request.host)
14
+ if locale_from_url
15
+ old_locale = I18n.locale
16
+ I18n.locale = locale_from_url
21
17
  end
22
18
 
23
19
  yield
24
20
  ensure
25
- I18n.locale = current_locale if tmp_locale
21
+ I18n.locale = old_locale if locale_from_url
26
22
  end
27
23
  end
28
24
 
@@ -25,18 +25,19 @@ module ActionDispatch
25
25
  private
26
26
 
27
27
  def translate_mapping(locale, route_set, translated_options, translated_path_ast, scope, controller, default_action, to, formatted, via, translated_options_constraints, anchor)
28
- options = scope[:options] ? scope[:options].merge(translated_options) : translated_options
29
-
30
- defaults = (scope[:defaults] || {}).dup
31
- scope_constraints = scope[:constraints] || {}
32
-
33
- blocks = scope[:blocks] ? scope[:blocks].dup : []
28
+ scope_params = {
29
+ blocks: scope[:blocks] || [],
30
+ constraints: scope[:constraints] || {},
31
+ defaults: (scope[:defaults] || {}).dup,
32
+ module: scope[:module],
33
+ options: scope[:options] ? scope[:options].merge(translated_options) : translated_options
34
+ }
34
35
 
35
36
  if RouteTranslator.config.verify_host_path_consistency
36
- blocks.push RouteTranslator::HostPathConsistencyLambdas.for_locale(locale)
37
+ scope_params[:blocks].push RouteTranslator::HostPathConsistencyLambdas.for_locale(locale)
37
38
  end
38
39
 
39
- ::ActionDispatch::Routing::Mapper::Mapping.new(route_set, translated_path_ast, defaults, controller, default_action, scope[:module], to, formatted, scope_constraints, blocks, via, translated_options_constraints, anchor, options)
40
+ ::ActionDispatch::Routing::Mapper::Mapping.build scope_params, route_set, translated_path_ast, controller, default_action, to, via, formatted, translated_options_constraints, anchor, translated_options
40
41
  end
41
42
 
42
43
  def add_route_to_set(mapping, path_ast, name, anchor)
@@ -26,7 +26,7 @@ module RouteTranslator
26
26
  result << locale.to_sym if host =~ regex_for(pattern)
27
27
  end
28
28
  locales &= I18n.available_locales
29
- (locales.first || I18n.default_locale).to_sym
29
+ locales.first&.to_sym
30
30
  end
31
31
  end
32
32
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RouteTranslator
4
- VERSION = '6.0.0'
4
+ VERSION = '7.0.0'
5
5
  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: 6.0.0
4
+ version: 7.0.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: 2019-05-16 00:00:00.000000000 Z
13
+ date: 2019-07-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionpack
@@ -92,14 +92,14 @@ dependencies:
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: 0.12.0
95
+ version: 0.13.2
96
96
  type: :development
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
- version: 0.12.0
102
+ version: 0.13.2
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: minitest
105
105
  requirement: !ruby/object:Gem::Requirement
@@ -154,42 +154,56 @@ dependencies:
154
154
  requirements:
155
155
  - - "~>"
156
156
  - !ruby/object:Gem::Version
157
- version: 0.69.0
157
+ version: 0.73.0
158
158
  type: :development
159
159
  prerelease: false
160
160
  version_requirements: !ruby/object:Gem::Requirement
161
161
  requirements:
162
162
  - - "~>"
163
163
  - !ruby/object:Gem::Version
164
- version: 0.69.0
164
+ version: 0.73.0
165
165
  - !ruby/object:Gem::Dependency
166
166
  name: rubocop-performance
167
167
  requirement: !ruby/object:Gem::Requirement
168
168
  requirements:
169
169
  - - "~>"
170
170
  - !ruby/object:Gem::Version
171
- version: '1.3'
171
+ version: '1.4'
172
172
  type: :development
173
173
  prerelease: false
174
174
  version_requirements: !ruby/object:Gem::Requirement
175
175
  requirements:
176
176
  - - "~>"
177
177
  - !ruby/object:Gem::Version
178
- version: '1.3'
178
+ version: '1.4'
179
+ - !ruby/object:Gem::Dependency
180
+ name: rubocop-rails
181
+ requirement: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - "~>"
184
+ - !ruby/object:Gem::Version
185
+ version: '2.2'
186
+ type: :development
187
+ prerelease: false
188
+ version_requirements: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - "~>"
191
+ - !ruby/object:Gem::Version
192
+ version: '2.2'
179
193
  - !ruby/object:Gem::Dependency
180
194
  name: simplecov
181
195
  requirement: !ruby/object:Gem::Requirement
182
196
  requirements:
183
197
  - - "~>"
184
198
  - !ruby/object:Gem::Version
185
- version: 0.16.1
199
+ version: 0.17.0
186
200
  type: :development
187
201
  prerelease: false
188
202
  version_requirements: !ruby/object:Gem::Requirement
189
203
  requirements:
190
204
  - - "~>"
191
205
  - !ruby/object:Gem::Version
192
- version: 0.16.1
206
+ version: 0.17.0
193
207
  description: Translates the Rails routes of your application into the languages defined
194
208
  in your locale files
195
209
  email: