autochthon 0.1.0 → 1.0.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 +5 -5
- data/CHANGELOG.md +32 -0
- data/README.md +144 -77
- data/lib/autochthon/backend/active_record.rb +13 -16
- data/lib/autochthon/backend/fetching_all.rb +23 -2
- data/lib/autochthon/backend/redis.rb +11 -4
- data/lib/autochthon/backend/simple.rb +19 -1
- data/lib/autochthon/configuration.rb +56 -0
- data/lib/autochthon/engine.rb +7 -0
- data/lib/autochthon/tasks/rails.rake +9 -10
- data/lib/autochthon/version.rb +3 -1
- data/lib/autochthon/web.rb +67 -20
- data/lib/autochthon.rb +4 -22
- data/lib/generators/autochthon/install/install_generator.rb +36 -0
- data/lib/generators/autochthon/install/templates/create_translations.rb.erb +16 -0
- data/lib/generators/autochthon/install/templates/initializer.rb.erb +21 -0
- metadata +17 -72
- data/.gitignore +0 -14
- data/Gemfile +0 -22
- data/Rakefile +0 -6
- data/autochthon.gemspec +0 -28
- data/bin/console +0 -7
- data/config.ru +0 -3
- data/vendor/assets/javascripts/autochthon/application.js.erb +0 -3
- data/vendor/assets/javascripts/autochthon/autochthon.js +0 -140
- data/web/package.json +0 -22
- data/web/src/Dialog.jsx +0 -61
- data/web/src/Translations.jsx +0 -61
- data/web/src/callbacks.js +0 -17
- data/web/src/main.jsx +0 -54
- data/web/src/store.js +0 -17
- data/web/webpack.config.js +0 -22
- /data/{public → views}/index.erb +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1e62a65404f178be1284e5ab3faee59c25a873e38c53b7165a7b4db7ddb7ca04
|
|
4
|
+
data.tar.gz: 0fa7df6effd8393da8cc2d0b9a362f12a48942637d415a6cb73b3b59f5910ff1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 971da06d72e40cb7f9893f36d00d02513ee1758dd71624d873cd18101bf8b1f3750e881f342a620086371fb3c78b167dd62880b6585f2d20ddd4664839dc44ae
|
|
7
|
+
data.tar.gz: 33be637501954a0572fad4faad5241dd3d83d7e5792146fadd6f171517a13f0797853d2f7092c2e1c7bb78e13dc6279f09384aa3c9145c763da2dd92c9b182c1
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
The "maintained again" release for Rails 8.1.
|
|
6
|
+
|
|
7
|
+
### Breaking changes
|
|
8
|
+
|
|
9
|
+
- **Ruby 3.3+ and Rails 8.1.** Older Ruby and Rails versions are no longer supported. Rails `main` is tested in CI, allowed to fail.
|
|
10
|
+
- **Sinatra 4 and Rack 3.** The gem now requires `sinatra ~> 4.0` (Rack 3, as Rails 8.1 uses); `sinatra-contrib` is no longer a dependency.
|
|
11
|
+
- **In-page editor removed.** The legacy right-click editor for missing translations (loaded with `//= require 'autochthon/application'`) depended on Sprockets and jQuery, so it cannot work on a default Rails 8 app. Remove that `require` line from your JavaScript manifest. In-context editing returns in 1.1.
|
|
12
|
+
- **`autochthon:create` removed.** Its unversioned migration failed on Rails 5+. Run `bin/rails generate autochthon:install` and `bin/rails db:migrate` instead.
|
|
13
|
+
- **Simple backend refuses production.** `Autochthon::Backend::Simple.new` raises `Autochthon::ConfigurationError` in production (`Rails.env`, else `RACK_ENV`). Use the ActiveRecord or Redis backend, or pass `allow_in_production: true`.
|
|
14
|
+
- **Access denied by default outside development and test.** Configure `Autochthon.authorize = ->(request) { ... }` (a truthy result grants access); without it every request gets 403 and a warning is logged at boot.
|
|
15
|
+
- **Same-origin JSON writes.** Write requests must be same-origin (`Sec-Fetch-Site: same-origin`, or a matching `Origin`) and `application/json`; others get 403 (cross-origin) or 415 (wrong content type).
|
|
16
|
+
- **String translations only.** `GET /translations` (and `Backend#all`) lists only String translations on every backend, skipping Arrays, Symbols, Numerics, booleans, nil and Procs such as `date.day_names` or `number.format.precision`. `POST /translations` answers 422 with a JSON `error` and stores nothing unless `locale` and `key` are non-empty Strings and `value` is a String; malformed JSON gets 400. The API does not guard against direct requests that overwrite non-String translations or namespaces; the admin UI only offers String translations.
|
|
17
|
+
- **No sessions.** The app no longer enables Sinatra sessions, and the `ActionDispatch::Request::Session#each` monkeypatch is gone.
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
|
|
21
|
+
- `bin/rails generate autochthon:install` writes a versioned migration for the `translations` table, an initializer that sets the ActiveRecord backend and mount point, and the route mounting `Autochthon::Web`.
|
|
22
|
+
- A release workflow publishing to RubyGems.org with trusted publishing when a `vX.Y.Z` tag is pushed.
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
|
|
26
|
+
- The ActiveRecord backend requires `i18n-active_record` 1.4.x; the Redis backend is tested with `redis-i18n` 0.8 on `redis` 6.1. Both stay optional: add them to your app's Gemfile.
|
|
27
|
+
- `autochthon:import` copies every non-Proc value, including the non-String ones the UI hides, so apps whose only I18n backend is the database or Redis keep Rails' date and number data. It enumerates them with `Backend#leaves`.
|
|
28
|
+
|
|
29
|
+
### Fixed
|
|
30
|
+
|
|
31
|
+
- The Redis backend lists stored values as they are, instead of crashing on nil or false values.
|
|
32
|
+
- The ActiveRecord backend lists booleans as booleans rather than their marker characters, and skips Proc rows instead of listing their source code.
|
data/README.md
CHANGED
|
@@ -1,160 +1,227 @@
|
|
|
1
1
|
# Autochthon
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A Sinatra app, mountable in Rails, for managing I18n translations stored in YAML files, a database (ActiveRecord) or Redis.
|
|
4
4
|
|
|
5
5
|
Look at this as [localeapp](https://www.localeapp.com/) hosted in your application.
|
|
6
6
|
|
|
7
|
+
Video demonstration:
|
|
8
|
+
|
|
9
|
+
[](https://youtu.be/dOM3KTPEIEY)
|
|
10
|
+
|
|
11
|
+
## Requirements
|
|
12
|
+
|
|
13
|
+
* Ruby 3.3 or newer (developed on Ruby 4.1).
|
|
14
|
+
* Rails 8.1. Rails `main` is tested in CI as an allowed-to-fail job, as an early warning.
|
|
15
|
+
|
|
16
|
+
The core is a plain Rack (Sinatra 4) app, but Rails 8.1 is the only officially supported host.
|
|
17
|
+
|
|
18
|
+
Upgrading from 0.1? See the breaking changes in the [CHANGELOG](CHANGELOG.md).
|
|
19
|
+
|
|
7
20
|
## Installation
|
|
8
21
|
|
|
9
|
-
Add
|
|
22
|
+
Add Autochthon and the gems of the backend you pick (see [Backends](#backends)) to your Gemfile:
|
|
10
23
|
|
|
11
24
|
```ruby
|
|
12
|
-
gem 'autochthon'
|
|
25
|
+
gem 'autochthon'
|
|
13
26
|
```
|
|
14
27
|
|
|
15
|
-
##
|
|
28
|
+
## Backends
|
|
29
|
+
|
|
30
|
+
| Backend | Storage | Extra gems in your Gemfile | Use for |
|
|
31
|
+
| --- | --- | --- | --- |
|
|
32
|
+
| `Autochthon::Backend::ActiveRecord` | `translations` table | `gem 'i18n-active_record', '~> 1.4'` | production |
|
|
33
|
+
| `Autochthon::Backend::Redis` | Redis | `gem 'redis-i18n', '~> 0.8'` and `gem 'redis', '~> 6.1'` | production |
|
|
34
|
+
| `Autochthon::Backend::Simple` | memory of each process, loaded from your YAML files | none | development and demos only |
|
|
16
35
|
|
|
17
|
-
|
|
36
|
+
The backend gems are optional: Autochthon loads them only when they are in your bundle and does not depend on them, so add the ones you use to your app's Gemfile.
|
|
18
37
|
|
|
19
|
-
|
|
38
|
+
### ActiveRecord
|
|
20
39
|
|
|
21
|
-
Add
|
|
40
|
+
Add the backend gem and run `bundle install`:
|
|
22
41
|
|
|
23
|
-
```
|
|
24
|
-
gem '
|
|
42
|
+
```ruby
|
|
43
|
+
gem 'autochthon'
|
|
44
|
+
gem 'i18n-active_record', '~> 1.4'
|
|
25
45
|
```
|
|
26
46
|
|
|
27
|
-
|
|
47
|
+
Run the install generator and migrate:
|
|
28
48
|
|
|
29
|
-
```
|
|
30
|
-
|
|
49
|
+
```
|
|
50
|
+
bin/rails generate autochthon:install
|
|
51
|
+
bin/rails db:migrate
|
|
52
|
+
```
|
|
31
53
|
|
|
32
|
-
|
|
54
|
+
The generator writes:
|
|
33
55
|
|
|
34
|
-
|
|
35
|
-
|
|
56
|
+
* a versioned migration creating the `translations` table,
|
|
57
|
+
* `config/initializers/autochthon.rb`, which sets `Autochthon.backend` to the ActiveRecord backend (chained with your YAML files as a fallback for keys not yet in the database), sets `Autochthon.mount_point` to `/autochthon`, and has a commented-out `Autochthon.authorize` example,
|
|
58
|
+
* a `mount Autochthon::Web => Autochthon.mount_point, as: "autochthon"` line in `config/routes.rb`.
|
|
36
59
|
|
|
37
|
-
|
|
60
|
+
Then copy your existing YAML translations into the database:
|
|
38
61
|
|
|
39
|
-
```
|
|
40
|
-
|
|
62
|
+
```
|
|
63
|
+
bin/rails autochthon:import
|
|
64
|
+
```
|
|
41
65
|
|
|
42
|
-
|
|
43
|
-
I18n.backend = Autochthon.backend = Autochthon::Backend::ActiveRecord.new
|
|
44
|
-
I18n.backend.class.include(I18n::Backend::Memoize)
|
|
45
|
-
end
|
|
66
|
+
To avoid a query per lookup you can memoize, in the initializer:
|
|
46
67
|
|
|
47
|
-
|
|
68
|
+
```ruby
|
|
69
|
+
Autochthon::Backend::ActiveRecord.include(I18n::Backend::Memoize)
|
|
48
70
|
```
|
|
49
71
|
|
|
50
|
-
|
|
51
|
-
That is the process in which you'll update the translation will see the new value for it. But other requests might be served by another process which will have the old value.
|
|
52
|
-
If that's the case consider:
|
|
53
|
-
* not using memoization
|
|
54
|
-
* caching the translation with an expiration option
|
|
55
|
-
* using redis backend without memoization
|
|
72
|
+
The memo lives in each Ruby process, so with several processes (e.g. Puma workers) only the process that saved an edit sees it straight away; the others keep the old value. In that case skip memoization, cache with an expiry, or use the Redis backend without memoization.
|
|
56
73
|
|
|
57
|
-
|
|
74
|
+
### Redis
|
|
58
75
|
|
|
59
|
-
|
|
76
|
+
Add the backend gems and run `bundle install`:
|
|
60
77
|
|
|
78
|
+
```ruby
|
|
79
|
+
gem 'autochthon'
|
|
80
|
+
gem 'redis-i18n', '~> 0.8'
|
|
81
|
+
gem 'redis', '~> 6.1'
|
|
61
82
|
```
|
|
62
|
-
|
|
83
|
+
|
|
84
|
+
Create `config/initializers/autochthon.rb`:
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
# With no argument it connects to localhost:6379, database 0.
|
|
88
|
+
I18n.backend = Autochthon.backend = Autochthon::Backend::Redis.new(ENV.fetch("REDIS_URL", "redis://localhost:6379/0"))
|
|
89
|
+
|
|
90
|
+
Autochthon.mount_point = "/autochthon"
|
|
63
91
|
```
|
|
64
92
|
|
|
65
|
-
|
|
93
|
+
and mount the app (see [Mount](#mount)). `bin/rails autochthon:import` copies your YAML translations into Redis.
|
|
66
94
|
|
|
67
|
-
|
|
95
|
+
### Simple (development only)
|
|
68
96
|
|
|
69
|
-
|
|
97
|
+
The Simple backend keeps translations in the memory of each process, loaded from your YAML files. Edits are not written back to the files, are lost on restart and are not shared between processes.
|
|
70
98
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
```
|
|
99
|
+
It is meant for development and demos: constructing it in production (`Rails.env.production?`, or `RACK_ENV=production` outside Rails) raises `Autochthon::ConfigurationError`. For a demo host that runs in the production environment, opt in with `Autochthon::Backend::Simple.new(allow_in_production: true)`.
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
# config/initializers/autochthon.rb
|
|
103
|
+
I18n.backend = Autochthon.backend = Autochthon::Backend::Simple.new
|
|
74
104
|
|
|
75
|
-
|
|
105
|
+
Autochthon.mount_point = "/autochthon"
|
|
106
|
+
```
|
|
76
107
|
|
|
77
|
-
|
|
78
|
-
require 'i18n/backend/redis'
|
|
108
|
+
## Mount
|
|
79
109
|
|
|
80
|
-
|
|
110
|
+
The install generator adds this to `config/routes.rb`; with the Redis or Simple backend add it yourself:
|
|
81
111
|
|
|
82
|
-
|
|
112
|
+
```ruby
|
|
113
|
+
mount Autochthon::Web => Autochthon.mount_point, as: "autochthon"
|
|
83
114
|
```
|
|
84
115
|
|
|
85
|
-
|
|
116
|
+
The admin page is then at `/autochthon`.
|
|
86
117
|
|
|
87
|
-
|
|
118
|
+
## Security
|
|
88
119
|
|
|
89
|
-
|
|
120
|
+
### The `authorize` hook
|
|
90
121
|
|
|
91
|
-
|
|
92
|
-
I18n.backend = Autochthon.backend = Autochthon::Backend::Simple.new
|
|
122
|
+
Every request to the admin page and the JSON endpoints goes through `Autochthon.authorize`. It receives the request (a `Rack::Request`) and grants access when it returns a truthy value:
|
|
93
123
|
|
|
94
|
-
|
|
124
|
+
```ruby
|
|
125
|
+
# config/initializers/autochthon.rb
|
|
126
|
+
Autochthon.authorize = ->(request) { request.env["warden"]&.user&.admin? }
|
|
95
127
|
```
|
|
96
128
|
|
|
97
|
-
|
|
129
|
+
Without a hook, requests are allowed in development and test and get `403 Forbidden` in every other environment. The environment is `Rails.env` when Rails is loaded, else `RACK_ENV` (development when unset). Outside development and test, a missing hook logs a warning at boot under Rails, or when the app is first built under plain Rack.
|
|
98
130
|
|
|
99
|
-
|
|
131
|
+
The static `public/bundle.js` is served without the hook; it holds no translation data.
|
|
100
132
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
133
|
+
### Protect the mount with a route constraint
|
|
134
|
+
|
|
135
|
+
As defence in depth, also restrict the mount in your router, so requests from anyone else never reach Autochthon. Keep the `authorize` hook as well: outside development and test, Autochthon answers 403 without it even behind a constraint.
|
|
104
136
|
|
|
105
|
-
|
|
137
|
+
With [Devise](https://github.com/heartcombo/devise):
|
|
106
138
|
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
|
|
139
|
+
```ruby
|
|
140
|
+
# config/routes.rb
|
|
141
|
+
authenticate :user, ->(user) { user.admin? } do
|
|
142
|
+
mount Autochthon::Web => Autochthon.mount_point, as: "autochthon"
|
|
110
143
|
end
|
|
111
144
|
```
|
|
112
145
|
|
|
113
|
-
|
|
146
|
+
Or with a custom constraint, any object responding to `matches?(request)`:
|
|
114
147
|
|
|
115
|
-
|
|
148
|
+
```ruby
|
|
149
|
+
# app/constraints/admin_constraint.rb
|
|
150
|
+
class AdminConstraint
|
|
151
|
+
def matches?(request)
|
|
152
|
+
User.find_by(id: request.session[:user_id])&.admin?
|
|
153
|
+
end
|
|
154
|
+
end
|
|
116
155
|
|
|
156
|
+
# config/routes.rb
|
|
157
|
+
constraints AdminConstraint.new do
|
|
158
|
+
mount Autochthon::Web => Autochthon.mount_point, as: "autochthon"
|
|
159
|
+
end
|
|
117
160
|
```
|
|
118
|
-
rake autochthon:import
|
|
119
|
-
```
|
|
120
161
|
|
|
121
|
-
|
|
162
|
+
With the custom constraint, a rejected request gets a 404 from the router. With Devise's `authenticate`, a signed-out visitor is redirected to sign in, and a signed-in user the block rejects gets a 404.
|
|
163
|
+
|
|
164
|
+
### Same-origin JSON writes
|
|
165
|
+
|
|
166
|
+
Write requests (POST, PUT, PATCH, DELETE) must be same-origin and `application/json`. Same-origin is proven by `Sec-Fetch-Site: same-origin`, or, when the browser does not send that header, by an `Origin` equal to the app's own scheme, host and port (forwarded headers are honoured, so set your proxy's `X-Forwarded-Proto`/`X-Forwarded-Host`). Other writes get `403 Forbidden`; a same-origin write with another content type gets `415 Unsupported Media Type`.
|
|
167
|
+
|
|
168
|
+
The app does not use a session.
|
|
169
|
+
|
|
170
|
+
## What can be edited
|
|
171
|
+
|
|
172
|
+
Only String translations are listed and editable. Arrays, Symbols, numbers, booleans, nil and Procs (such as `date.day_names` or `number.format.precision`) are not listed, on every backend.
|
|
173
|
+
|
|
174
|
+
`POST /translations` requires a non-empty String `locale` and `key` and a String `value` (else 422; malformed JSON gets 400). It does not guard against a direct request that overwrites a non-String translation or a whole namespace with a String; the admin UI only offers String translations.
|
|
175
|
+
|
|
176
|
+
## Import
|
|
177
|
+
|
|
178
|
+
Copy translations from your YAML files into the configured backend:
|
|
122
179
|
|
|
123
180
|
```
|
|
124
|
-
|
|
181
|
+
bin/rails autochthon:import
|
|
125
182
|
```
|
|
126
183
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
In your `app/assets/javascripts/application.js` file do:
|
|
184
|
+
Only specific locales:
|
|
130
185
|
|
|
131
|
-
```
|
|
132
|
-
|
|
186
|
+
```
|
|
187
|
+
LOCALES="en,fr" bin/rails autochthon:import
|
|
133
188
|
```
|
|
134
189
|
|
|
135
|
-
|
|
190
|
+
The import copies every value except Procs, including the non-String ones the admin UI hides, so apps whose only I18n backend is the database or Redis keep Rails' date and number data.
|
|
136
191
|
|
|
137
192
|
## Examples
|
|
138
193
|
|
|
139
|
-
*
|
|
194
|
+
* Rails application using the ActiveRecord backend: https://github.com/asok/autochthon_rails_example_app
|
|
140
195
|
|
|
141
196
|
## Development
|
|
142
197
|
|
|
143
|
-
|
|
198
|
+
```
|
|
199
|
+
bundle install
|
|
200
|
+
bundle exec rspec
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
The specs need a running Redis server (database 15, or set `TEST_REDIS_DB`) and Chrome for the browser feature specs (Cuprite). `RAILS_VERSION=main` runs them against Rails `main`.
|
|
144
204
|
|
|
145
|
-
|
|
205
|
+
`public/bundle.js` is a frozen build of the 2016 React UI in `web/`; its toolchain cannot be rebuilt, so treat the bundle as an opaque artifact. The admin UI is due to be rewritten in 1.1.
|
|
146
206
|
|
|
147
|
-
|
|
207
|
+
## Releasing
|
|
148
208
|
|
|
149
|
-
|
|
209
|
+
Maintainers release from `master` with RubyGems trusted publishing; no API key is stored.
|
|
150
210
|
|
|
151
|
-
`
|
|
211
|
+
1. Bump `Autochthon::VERSION` in `lib/autochthon/version.rb`, add the release date to its CHANGELOG heading (`## 1.0.0 (YYYY-MM-DD)`), and merge to `master`.
|
|
212
|
+
2. Tag that commit with `v` plus the version and push the tag:
|
|
213
|
+
|
|
214
|
+
```
|
|
215
|
+
git tag v1.0.0
|
|
216
|
+
git push origin v1.0.0
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
The [Release workflow](.github/workflows/release.yml) runs the specs on the tagged commit, checks the commit is on `master` and the tag equals `v` + `Autochthon::VERSION`, and publishes with `rubygems/release-gem` from the `release` environment.
|
|
152
220
|
|
|
153
221
|
## Contributing
|
|
154
222
|
|
|
155
223
|
Bug reports and pull requests are welcome on GitHub at https://github.com/asok/autochthon.
|
|
156
224
|
|
|
157
|
-
|
|
158
225
|
## License
|
|
159
226
|
|
|
160
227
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'autochthon/backend/fetching_all'
|
|
2
4
|
|
|
3
5
|
begin
|
|
@@ -10,24 +12,19 @@ begin
|
|
|
10
12
|
|
|
11
13
|
include FetchingAll
|
|
12
14
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
# i18n-active_record >= 1.0 reads its settings from `self.class.config`;
|
|
16
|
+
# share the gem's own configuration rather than keeping a separate one.
|
|
17
|
+
def self.config
|
|
18
|
+
I18n::Backend::ActiveRecord.config
|
|
17
19
|
end
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
t.boolean :is_proc, default: false
|
|
27
|
-
|
|
28
|
-
t.timestamps null: false
|
|
29
|
-
end
|
|
30
|
-
end
|
|
21
|
+
# Goes through Translation#value so booleans decode from their marker
|
|
22
|
+
# characters; Proc rows are skipped rather than evaluated.
|
|
23
|
+
def all_for_locale(locale)
|
|
24
|
+
I18n::Backend::ActiveRecord::Translation
|
|
25
|
+
.locale(locale)
|
|
26
|
+
.reject(&:is_proc)
|
|
27
|
+
.to_h { |translation| [translation.key, translation.value] }
|
|
31
28
|
end
|
|
32
29
|
end
|
|
33
30
|
end
|
|
@@ -1,20 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Autochthon
|
|
2
4
|
module Backend
|
|
3
5
|
module FetchingAll
|
|
4
6
|
include I18n::Backend::Flatten
|
|
5
7
|
|
|
8
|
+
# The editable translations: String leaves only.
|
|
6
9
|
def all(locales = available_locales)
|
|
7
|
-
locales.
|
|
10
|
+
leaves(locales).select { |translation| editable?(translation[:value]) }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Every storable leaf, whatever its type: what autochthon:import copies,
|
|
14
|
+
# since Rails needs data like date.day_names when the database or Redis
|
|
15
|
+
# backend is the app's only I18n backend. Intermediate Hashes are not
|
|
16
|
+
# leaves, and Procs are code that key-value backends cannot store.
|
|
17
|
+
def leaves(locales = available_locales)
|
|
18
|
+
locales.each_with_object([]) do |locale, out|
|
|
8
19
|
flatten_keys(all_for_locale(locale), false) do |key, value|
|
|
20
|
+
next if value.is_a?(Hash) || value.is_a?(Proc)
|
|
21
|
+
|
|
9
22
|
out << {key: key, value: value, locale: locale}
|
|
10
23
|
end
|
|
11
|
-
out
|
|
12
24
|
end
|
|
13
25
|
end
|
|
14
26
|
|
|
15
27
|
def all_for_locale(locale)
|
|
16
28
|
raise NotImplementedError
|
|
17
29
|
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
# Only Strings are editable text. Arrays, Symbols, Numerics, booleans and
|
|
34
|
+
# nil (e.g. date.day_names, number.format.precision) are data the admin
|
|
35
|
+
# UI would corrupt by saving it back as a String.
|
|
36
|
+
def editable?(value)
|
|
37
|
+
value.is_a?(String)
|
|
38
|
+
end
|
|
18
39
|
end
|
|
19
40
|
end
|
|
20
41
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
begin
|
|
2
4
|
require 'i18n/backend/redis'
|
|
3
5
|
|
|
@@ -6,11 +8,16 @@ begin
|
|
|
6
8
|
class Redis < I18n::Backend::Redis
|
|
7
9
|
include FetchingAll
|
|
8
10
|
|
|
11
|
+
# Reads the stored values directly: translate throws on nil and false
|
|
12
|
+
# values (redis-i18n treats them as missing).
|
|
9
13
|
def all_for_locale(locale)
|
|
10
|
-
store.keys("#{locale}.*")
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
keys = store.keys("#{locale}.*")
|
|
15
|
+
|
|
16
|
+
keys
|
|
17
|
+
.map { |key| key[(locale.size + 1)..] }
|
|
18
|
+
.zip(store.mget(*keys))
|
|
19
|
+
.to_h
|
|
20
|
+
|
|
14
21
|
end
|
|
15
22
|
end
|
|
16
23
|
end
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'autochthon/configuration'
|
|
1
4
|
require 'autochthon/backend/fetching_all'
|
|
2
5
|
|
|
3
6
|
module Autochthon
|
|
@@ -7,8 +10,23 @@ module Autochthon
|
|
|
7
10
|
|
|
8
11
|
include FetchingAll
|
|
9
12
|
|
|
13
|
+
PRODUCTION_MESSAGE =
|
|
14
|
+
"Autochthon::Backend::Simple keeps translations in memory, per process, " \
|
|
15
|
+
"so edits are lost on restart and not shared between processes. Use " \
|
|
16
|
+
"Autochthon::Backend::ActiveRecord or Autochthon::Backend::Redis in " \
|
|
17
|
+
"production, or pass allow_in_production: true to accept this " \
|
|
18
|
+
"(e.g. a demo host running with the production environment)."
|
|
19
|
+
|
|
20
|
+
def initialize(allow_in_production: false)
|
|
21
|
+
if Environment.production? && !allow_in_production
|
|
22
|
+
raise ConfigurationError, PRODUCTION_MESSAGE
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
super()
|
|
26
|
+
end
|
|
27
|
+
|
|
10
28
|
def all_for_locale(locale)
|
|
11
|
-
|
|
29
|
+
translations(do_init: true).fetch(locale.to_sym, {})
|
|
12
30
|
end
|
|
13
31
|
end
|
|
14
32
|
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Autochthon
|
|
4
|
+
class ConfigurationError < StandardError; end
|
|
5
|
+
|
|
6
|
+
# The environment the host app runs in: Rails.env when Rails is loaded,
|
|
7
|
+
# else RACK_ENV, defaulting to development.
|
|
8
|
+
module Environment
|
|
9
|
+
PERMISSIVE = %w[development test].freeze
|
|
10
|
+
|
|
11
|
+
def self.name
|
|
12
|
+
if defined?(::Rails) && ::Rails.respond_to?(:env)
|
|
13
|
+
::Rails.env.to_s
|
|
14
|
+
else
|
|
15
|
+
ENV["RACK_ENV"] || "development"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.production?
|
|
20
|
+
name == "production"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Development and test allow requests when no authorize hook is configured.
|
|
24
|
+
def self.permissive?
|
|
25
|
+
PERMISSIVE.include?(name)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
module Methods
|
|
30
|
+
attr_accessor :backend, :mount_point
|
|
31
|
+
|
|
32
|
+
# A callable receiving the request; a truthy result grants access.
|
|
33
|
+
attr_accessor :authorize
|
|
34
|
+
|
|
35
|
+
def authorized?(request)
|
|
36
|
+
return authorize.call(request) ? true : false if authorize
|
|
37
|
+
|
|
38
|
+
Environment.permissive?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def warn_if_unprotected
|
|
42
|
+
return if authorize || Environment.permissive?
|
|
43
|
+
|
|
44
|
+
message = "[autochthon] No Autochthon.authorize hook configured: every request " \
|
|
45
|
+
"will get 403 in the #{Environment.name} environment. Set " \
|
|
46
|
+
"Autochthon.authorize = ->(request) { ... } to grant access."
|
|
47
|
+
if defined?(::Rails) && ::Rails.respond_to?(:logger) && ::Rails.logger
|
|
48
|
+
::Rails.logger.warn(message)
|
|
49
|
+
else
|
|
50
|
+
Kernel.warn(message)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
extend Methods
|
|
56
|
+
end
|
data/lib/autochthon/engine.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Autochthon
|
|
2
4
|
class Engine < ::Rails::Engine
|
|
3
5
|
isolate_namespace Autochthon
|
|
@@ -5,5 +7,10 @@ module Autochthon
|
|
|
5
7
|
rake_tasks do
|
|
6
8
|
load 'autochthon/tasks/rails.rake'
|
|
7
9
|
end
|
|
10
|
+
|
|
11
|
+
# Runs after config/initializers, where Autochthon.authorize is set.
|
|
12
|
+
config.after_initialize do
|
|
13
|
+
Autochthon.warn_if_unprotected
|
|
14
|
+
end
|
|
8
15
|
end
|
|
9
16
|
end if defined?(::Rails)
|
|
@@ -1,26 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'autochthon/backend/active_record'
|
|
2
4
|
|
|
3
5
|
namespace :autochthon do
|
|
4
|
-
desc "Create translations table"
|
|
5
|
-
task create: :environment do
|
|
6
|
-
Autochthon::Backend::ActiveRecord::Migration.new.change
|
|
7
|
-
end
|
|
8
|
-
|
|
9
6
|
desc "Reads all translations from yml files and inserts them into db"
|
|
10
7
|
task import: :environment do
|
|
11
|
-
backend
|
|
8
|
+
# Only reads the YAML files, so the in-memory backend is fine in production.
|
|
9
|
+
backend = Autochthon::Backend::Simple.new(allow_in_production: true)
|
|
12
10
|
|
|
13
11
|
locales = (ENV['LOCALES'] || '').split(',')
|
|
14
12
|
locales = backend.available_locales if locales.empty?
|
|
15
13
|
|
|
16
14
|
fn = -> do
|
|
17
|
-
backend.
|
|
18
|
-
Autochthon.backend.store_translations(t[:locale], {t[:key] => t[:value]})
|
|
15
|
+
backend.leaves(locales).each do |t|
|
|
16
|
+
Autochthon.backend.store_translations(t[:locale], {t[:key] => t[:value]}, escape: false)
|
|
19
17
|
end
|
|
20
18
|
end
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
# Backend::ActiveRecord is only defined when i18n-active_record is installed.
|
|
21
|
+
if defined?(Autochthon::Backend::ActiveRecord) && Autochthon.backend.is_a?(Autochthon::Backend::ActiveRecord)
|
|
22
|
+
ActiveRecord::Base.transaction { fn.call }
|
|
24
23
|
else
|
|
25
24
|
fn.call
|
|
26
25
|
end
|
data/lib/autochthon/version.rb
CHANGED