bridgetown-routes 1.2.0.beta3 → 1.2.0.beta5

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: b5949b1ebe4c5f24232ac0488c4f605218927c3da20c26bdce731552db4ede14
4
- data.tar.gz: '0811c661436d875745219faccb16509c17109eda2ebb3616bb8e1d250bdd2845'
3
+ metadata.gz: a1851e1044647a20b32d1f926092099180a85094a725a64b448ac4a2fe5c8da2
4
+ data.tar.gz: fce918df055844cf76a60cef1724e430e93ee8a169049123ae12a892636c75dd
5
5
  SHA512:
6
- metadata.gz: fd962ecf01401df33260d541bf8762589f0e0011f13dd91798f81cf8b081f72cc41b0926f5a4c475ad41287e83483bea6c5f1c9abf28df2151d051b4f7e68143
7
- data.tar.gz: be0aa19c3a01bbc1549382c2a54a006733f55c9638d5bdb1dc4dbfb94d57b26aa8a43e9db3f681112c747b9bb100a16ac5dc82fc9d61411790f25358c3a3e412
6
+ metadata.gz: 6da6e86ae502760c25f2e7ff293ed48ba3b2519cf3ad8b6cd6625388de0df3c8bb73df99b4845ab4e8eac044078c03971a82db141c53de4a360861c438f7caa5
7
+ data.tar.gz: ad704062b6288e2428a2bb925d61987d46142900f802d898856297527bc9a2fac404270bca27901de0f440d576f962604a958632f6ed32606436fec47dc876e6
@@ -32,11 +32,15 @@ module Bridgetown
32
32
  ":#{Regexp.last_match(1)}"
33
33
  end
34
34
 
35
- [file, file_slug, segment_keys]
35
+ # generate localized file slugs
36
+ localized_file_slugs = generate_localized_file_slugs(site, file_slug)
37
+
38
+ [file, localized_file_slugs, segment_keys]
36
39
  end
37
40
 
38
41
  new_manifest += sort_routes!(routes)
39
42
  end
43
+
40
44
  @route_manifest ||= {}
41
45
  @route_manifest[site.label] = new_manifest
42
46
  end
@@ -44,9 +48,9 @@ module Bridgetown
44
48
  def sort_routes!(routes)
45
49
  routes.sort! do |route_a, route_b|
46
50
  # @type [String]
47
- _, slug_a = route_a
51
+ slug_a = route_a[1][0]
48
52
  # @type [String]
49
- _, slug_b = route_b
53
+ slug_b = route_b[1][0]
50
54
 
51
55
  # @type [Integer]
52
56
  weight1 = slug_a.count("/") <=> slug_b.count("/")
@@ -57,6 +61,28 @@ module Bridgetown
57
61
  end
58
62
  end.reverse!
59
63
  end
64
+
65
+ def locale_for(slug, site)
66
+ possible_locale_segment = slug.split("/").first.to_sym
67
+
68
+ if site.config.available_locales.include? possible_locale_segment
69
+ possible_locale_segment
70
+ else
71
+ site.config.default_locale
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def generate_localized_file_slugs(site, file_slug)
78
+ site.config.available_locales.map do |locale|
79
+ if locale == site.config.default_locale && !site.config.prefix_default_locale
80
+ file_slug
81
+ else
82
+ "#{locale}/#{file_slug}"
83
+ end
84
+ end
85
+ end
60
86
  end
61
87
  end
62
88
  end
@@ -16,27 +16,40 @@ module Bridgetown
16
16
  end
17
17
 
18
18
  Bridgetown::Routes::Manifest.generate_manifest(bridgetown_site).each do |route|
19
- file, file_slug, segment_keys = route
20
-
21
- r.on file_slug do |*segment_values|
22
- response["X-Bridgetown-SSR"] = "1"
23
- # eval_route_file caches when Bridgetown.env.production?
24
- Bridgetown::Routes::CodeBlocks.eval_route_file file, file_slug, @_roda_app
25
-
26
- segment_values.each_with_index do |value, index|
27
- r.params[segment_keys[index]] ||= value
28
- end
29
-
30
- route_block = Bridgetown::Routes::CodeBlocks.route_block(file_slug)
31
- response.instance_variable_set(
32
- :@_route_file_code, route_block.instance_variable_get(:@_route_file_code)
33
- ) # could be nil
34
- @_roda_app.instance_exec(r, &route_block)
19
+ file, localized_file_slugs, segment_keys = route
20
+
21
+ localized_file_slugs.each do |file_slug|
22
+ add_route(r, file, file_slug, segment_keys)
35
23
  end
36
24
  end
37
25
 
38
26
  nil
39
27
  end
28
+
29
+ private
30
+
31
+ def add_route(route, file, file_slug, segment_keys)
32
+ route.on file_slug do |*segment_values|
33
+ response["X-Bridgetown-SSR"] = "1"
34
+ # eval_route_file caches when Bridgetown.env.production?
35
+ Bridgetown::Routes::CodeBlocks.eval_route_file file, file_slug, @_roda_app
36
+
37
+ segment_values.each_with_index do |value, index|
38
+ route.params[segment_keys[index]] ||= value
39
+ end
40
+
41
+ # set route locale
42
+ locale = Bridgetown::Routes::Manifest.locale_for(file_slug, bridgetown_site)
43
+ I18n.locale = locale
44
+ route.params[:locale] = locale
45
+
46
+ route_block = Bridgetown::Routes::CodeBlocks.route_block(file_slug)
47
+ response.instance_variable_set(
48
+ :@_route_file_code, route_block.instance_variable_get(:@_route_file_code)
49
+ ) # could be nil
50
+ @_roda_app.instance_exec(route, &route_block)
51
+ end
52
+ end
40
53
  end
41
54
  end
42
55
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bridgetown-routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0.beta3
4
+ version: 1.2.0.beta5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bridgetown Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-09 00:00:00.000000000 Z
11
+ date: 2023-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bridgetown-core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.2.0.beta3
19
+ version: 1.2.0.beta5
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.2.0.beta3
26
+ version: 1.2.0.beta5
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: roda-route_list
29
29
  requirement: !ruby/object:Gem::Requirement