seiten 1.1.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f11b589f94032def9f0ef35b5fd457e1acb0f3508cf39f1ac63fb0338ee8217a
4
- data.tar.gz: 71e0543f85e054dac2cf348e6e9ecf1eebde83d1659921dca8c4460a734665b6
3
+ metadata.gz: '09cdd9e8323ed21cafe0eb5f92544ee05a8e78bc86b421a0fb2bfc6c865ecd8b'
4
+ data.tar.gz: f9860ca8c46a06a36136a992e078be502c81fba45373c5feae6682a5fb431e8e
5
5
  SHA512:
6
- metadata.gz: 186fe751f440f15e30944acc416a9dfac931bcf1afd0f65c4f58ba5b6e7c8a0ebd3fab9e8ad39260c39aebc26de7769cb5b829c4b05c7f35c30407806a97577a
7
- data.tar.gz: 133f9dd5ac075f2805088865e309183c453a2e889b0cc090c970db1e94076e603f4a60a8f249685b97d674741b857a6e78112ce33f356c0e3aaaa64e73b1d8b5
6
+ metadata.gz: 4ab559c9fb74b3373ddb9b31485dd4d78b56bcab9dbd1c1e863bafcf0a60c1a4cfb502728ea763db6e7b81ca9b1ff9d3c17d529fcf5b7b8c0bf226cca5316e6d
7
+ data.tar.gz: 4525be586db9bba6d0c8b1589d7297680b411523874c4fae5ed73dbb3ebff0445a65c7a27c22c65612c29c2dd525a30de5c845482c2cafef044824c8e7acc426
@@ -3,5 +3,8 @@ module Seiten
3
3
  config.action_dispatch.rescue_responses.merge!(
4
4
  'Seiten::Errors::RoutingError' => :not_found
5
5
  )
6
+
7
+ # Navigations built from I18n follow reloaded translations in development.
8
+ config.to_prepare { Seiten.reset_i18n_navigations! }
6
9
  end
7
10
  end
@@ -1,3 +1,3 @@
1
1
  module Seiten
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/seiten.rb CHANGED
@@ -21,6 +21,7 @@ module Seiten
21
21
  config_dir: File.join('config', 'navigations'),
22
22
  pages_dir: File.join('app', 'pages'),
23
23
  root_page: 'home',
24
+ i18n_scope: 'navigations',
24
25
  default_layout: 'application',
25
26
  html: {
26
27
  navigation: {
@@ -45,6 +46,9 @@ module Seiten
45
46
  }
46
47
 
47
48
  @navigations = []
49
+ @i18n_files = {}
50
+ @i18n_navigations = nil
51
+ @i18n_lock = Mutex.new
48
52
 
49
53
  class << self
50
54
  def config
@@ -52,6 +56,7 @@ module Seiten
52
56
  end
53
57
 
54
58
  def navigations
59
+ build_i18n_navigations unless @i18n_navigations
55
60
  @navigations
56
61
  end
57
62
 
@@ -59,14 +64,67 @@ module Seiten
59
64
  @navigations = navigations
60
65
  end
61
66
 
67
+ # config/navigations/<name>.<locale>.yml holds one locale's navigation; <name>.yml holds the pages
68
+ # of every locale, with titles and slugs from I18n (built on first use, see #i18n_pages).
62
69
  def initialize_navigations
63
70
  Dir[File.join(Rails.root, Seiten.config[:config_dir], "*.yml")].each do |file|
64
- id = File.basename(file, '.yml')
65
- name = id.gsub(/\..*/, '')
66
- locale = id.gsub(/.*\./, '')
67
- navigation = Seiten::Navigation.new(name: name, locale: locale, dir: File.join(Rails.root, Seiten.config[:pages_dir], name, locale))
68
- navigation.page_collection.build(pages: YAML.load_file(navigation.config))
69
- Seiten.navigations << navigation
71
+ id = File.basename(file, '.yml')
72
+ if id.include?('.')
73
+ name, locale = id.split('.', 2)
74
+ navigation = Seiten::Navigation.new(name: name, locale: locale, dir: File.join(Rails.root, Seiten.config[:pages_dir], name, locale))
75
+ navigation.page_collection.build(pages: YAML.load_file(navigation.config))
76
+ @navigations << navigation
77
+ else
78
+ @i18n_files[id] = file
79
+ end
80
+ end
81
+ end
82
+
83
+ # Drops the navigations built from I18n, so they're built again with the current translations
84
+ # (on every code or translation reload in development).
85
+ def reset_i18n_navigations!
86
+ @i18n_lock.synchronize do
87
+ @navigations -= @i18n_navigations if @i18n_navigations
88
+ @i18n_navigations = nil
89
+ end
90
+ end
91
+
92
+ private
93
+
94
+ def build_i18n_navigations
95
+ @i18n_lock.synchronize do
96
+ return if @i18n_navigations
97
+
98
+ @i18n_navigations = @i18n_files.flat_map do |name, file|
99
+ structure = YAML.load_file(file)
100
+ I18n.available_locales.map do |locale|
101
+ navigation = Seiten::Navigation.new(name: name, locale: locale, config: file, dir: File.join(Rails.root, Seiten.config[:pages_dir], name, locale.to_s))
102
+ navigation.page_collection.build(pages: i18n_pages(structure, locale, [Seiten.config[:i18n_scope], name]))
103
+ navigation
104
+ end
105
+ end
106
+ @navigations.concat(@i18n_navigations)
107
+ end
108
+ end
109
+
110
+ # The pages of a locale: every page names its `key`; its title and slug are the translations
111
+ # <i18n_scope>.<navigation>.<parent keys…>.<key>.title and .slug (the slug defaults to the
112
+ # parameterized title). The page's key becomes its full key, e.g. "account.manage", which is the
113
+ # same page in every locale.
114
+ def i18n_pages(nodes, locale, scope)
115
+ nodes.map do |node|
116
+ node = node.dup
117
+ key = node['key'] or raise Errors::PageError, "Every page of #{scope[1]}.yml needs a key"
118
+ page_scope = [*scope, key.to_s]
119
+ title = I18n.t('title', scope: page_scope, locale: locale, default: nil)
120
+ slug = I18n.t('slug', scope: page_scope, locale: locale, default: nil)
121
+ raise Errors::PageError, "#{page_scope.join('.')} needs a title or a slug in #{locale}" unless title || slug || node.key?('url')
122
+
123
+ node['title'] = title
124
+ node['url'] = slug if slug && !node.key?('url')
125
+ node['key'] = page_scope.drop(2).join('.')
126
+ node['nodes'] = i18n_pages(node['nodes'], locale, page_scope) if node['nodes']
127
+ node
70
128
  end
71
129
  end
72
130
  end
@@ -0,0 +1,13 @@
1
+ de:
2
+ navigations:
3
+ guide:
4
+ start:
5
+ title: Start
6
+ account:
7
+ title: Dein Account
8
+ manage:
9
+ title: Account verwalten
10
+ security:
11
+ title: Sicherheit
12
+ billing:
13
+ title: Rechnungen
@@ -0,0 +1,14 @@
1
+ en:
2
+ navigations:
3
+ guide:
4
+ start:
5
+ title: Start
6
+ account:
7
+ title: Your account
8
+ manage:
9
+ title: Manage your account
10
+ security:
11
+ title: Security
12
+ slug: account-security
13
+ billing:
14
+ title: Billing
@@ -0,0 +1,7 @@
1
+ - key: start
2
+ url: '/'
3
+ - key: account
4
+ nodes:
5
+ - key: manage
6
+ - key: security
7
+ - key: billing