rack-locale_memorable 0.3.0 → 0.3.2
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 +4 -4
- data/CHANGELOG.md +15 -2
- data/README.md +43 -4
- data/lib/rack/locale_memorable/response.rb +3 -3
- data/lib/rack/locale_memorable/version.rb +1 -1
- data/lib/rack/locale_memorable.rb +4 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ccb7d078d2dd4d582f7db663cf0436da123b94e6531f8d6de83a63c1eb03bbd
|
4
|
+
data.tar.gz: eb20d6db30ef828dfac9f27843ab9a249308c889835f912e61986d1f08f1edc9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b6212b7c2a58b6f110a22856ac52c8197600ccc9929f229147f8324d6342a15e5472ef45917fe11dfbdd2bf196877fd8b5e26f962cfa9668b95d7622b77cd8d
|
7
|
+
data.tar.gz: 6fdd32c5bad90120d759514971c20b2de1330fbc9774bbf218cd91a7781cfffb89f2a29f3da5547452b4ae1cede23d757d6b25ca345429cb7b5f81b6f98f0eae
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
-
## [0.
|
3
|
+
## [0.3.2] - 2022-09-28
|
4
4
|
|
5
|
-
|
5
|
+
### Fixed
|
6
|
+
|
7
|
+
- handle cookie expiry correctly https://github.com/nov/rack-locale_memorable/commit/886fed66da5513a4c0b6408a9ac7685932b0a034
|
8
|
+
|
9
|
+
## [0.3.1] - 2022-09-28
|
10
|
+
|
11
|
+
### Fixed
|
12
|
+
|
13
|
+
- fixed cookie key handling when remembering https://github.com/nov/rack-locale_memorable/commit/ab8c87bff449e762daf00dea5cef44d001176141
|
14
|
+
|
15
|
+
## [0.3.0] - 2022-09-28
|
16
|
+
|
17
|
+
- First stable release.
|
18
|
+
- Older versions are buggy, DO NOT USE THEM.
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Rack::LocaleMemorable
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Handle query params, cookie and HTTP_ACCEPT_LANGUAGE header to detect user-preffered locale, and remember it when necessary.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -16,7 +14,48 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
16
14
|
|
17
15
|
## Usage
|
18
16
|
|
19
|
-
|
17
|
+
```ruby
|
18
|
+
# in initializers/middlewares.rb etc.
|
19
|
+
Rails.application.configure do |config|
|
20
|
+
config.middleware.use Rack::LocaleMemorable
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
NOTE: If you're using devise, set `Rack::LocaleMemorable` before `Warden::Manager`, otherwise you see warden error messages in wrong locale.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
Rails.application.configure do |config|
|
28
|
+
config.middleware.insert_before Warden::Manager, Rack::LocaleMemorable
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
By default, this gem handles
|
33
|
+
|
34
|
+
* `params['locale']` as explicit locale
|
35
|
+
* `cookies['locale']` as remembered locale
|
36
|
+
* `headers['HTTP_ACCEPT_LANGUAGE']` as implicit locale
|
37
|
+
|
38
|
+
and when explicit locale is specified in the request, remember it in cookie.
|
39
|
+
|
40
|
+
You can customize those values
|
41
|
+
* params_key (`'locale'` by default)
|
42
|
+
* cookie_key (`'locale'` by default)
|
43
|
+
* secure_cookie (`true` by default)
|
44
|
+
* cookie_lifetime (`1.year` by default)
|
45
|
+
|
46
|
+
like below
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Rails.application.configure do |config|
|
50
|
+
config.middleware.use(
|
51
|
+
Rack::LocaleMemorable,
|
52
|
+
params_key: 'ui_locale',
|
53
|
+
cookie_key: 'ui_locale',
|
54
|
+
secure_cookie: Rails.env.production?,
|
55
|
+
cookie_lifetime: 3.months
|
56
|
+
)
|
57
|
+
end
|
58
|
+
```
|
20
59
|
|
21
60
|
## Development
|
22
61
|
|
@@ -3,10 +3,10 @@
|
|
3
3
|
module Rack
|
4
4
|
class LocaleMemorable
|
5
5
|
class Response < Rack::Response
|
6
|
-
def remember_locale(explicit_locale, secure_cookie:,
|
7
|
-
set_cookie
|
6
|
+
def remember_locale(explicit_locale, secure_cookie:, cookie_lifetime:, cookie_key:)
|
7
|
+
set_cookie cookie_key, {
|
8
8
|
value: explicit_locale,
|
9
|
-
expires:
|
9
|
+
expires: cookie_lifetime.from_now,
|
10
10
|
http_only: true,
|
11
11
|
secure: secure_cookie
|
12
12
|
}
|
@@ -7,12 +7,12 @@ require 'rack'
|
|
7
7
|
|
8
8
|
module Rack
|
9
9
|
class LocaleMemorable
|
10
|
-
def initialize(app, params_key: 'locale', cookie_key: 'locale', secure_cookie: true,
|
10
|
+
def initialize(app, params_key: 'locale', cookie_key: 'locale', secure_cookie: true, cookie_lifetime: 1.year)
|
11
11
|
@app = app
|
12
12
|
@params_key = params_key
|
13
13
|
@cookie_key = cookie_key
|
14
14
|
@secure_cookie = secure_cookie
|
15
|
-
@
|
15
|
+
@cookie_lifetime = cookie_lifetime
|
16
16
|
end
|
17
17
|
|
18
18
|
def call(env)
|
@@ -24,7 +24,8 @@ module Rack
|
|
24
24
|
response.remember_locale(
|
25
25
|
request.explicit_locale,
|
26
26
|
secure_cookie: @secure_cookie,
|
27
|
-
|
27
|
+
cookie_lifetime: @cookie_lifetime,
|
28
|
+
cookie_key: @cookie_key
|
28
29
|
)
|
29
30
|
end
|
30
31
|
response.finish
|