rack-locale_memorable 0.2.2 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -2
- data/README.md +22 -4
- data/lib/rack/locale_memorable/request.rb +9 -3
- data/lib/rack/locale_memorable/response.rb +2 -2
- data/lib/rack/locale_memorable/version.rb +1 -1
- data/lib/rack/locale_memorable.rb +6 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd5bd6d13a8a85a10053362a85a1d5c3530348fadadb5f871f9990e40e36d59b
|
4
|
+
data.tar.gz: 6f9c459b2527f17fc153c43bd3d71b9ecfb5c6ae60402e142ecc0da8bde1afa2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ed6cc8acbfbe09b475fc80c90f155102f25916a28f279e7efadc28e2660a5fce8cd7896727c4b819906097009886112d10df9e7e807eefb1ffa10e798d2c1d2
|
7
|
+
data.tar.gz: f8304fa8aa3ed7b6f0fa102935828f5eeec3d0cae20bbd0bc8e5d10bc8fba3f37edbdd7879bfdc7f416f39ba8f43c08f2f242fa088661edf52e0b1c2afd6f595
|
data/CHANGELOG.md
CHANGED
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,27 @@ 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(
|
21
|
+
Rack::LocaleMemorable,
|
22
|
+
secure_cookie: Rails.env.production?
|
23
|
+
)
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
NOTE: If you're using devise, set `Rack::LocaleMemorable` before `Warden::Manager`, otherwise you see warden error messages in wrong locale.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Rails.application.configure do |config|
|
31
|
+
config.middleware.insert_before(
|
32
|
+
Warden::Manager,
|
33
|
+
Rack::LocaleMemorable,
|
34
|
+
secure_cookie: Rails.env.production?
|
35
|
+
)
|
36
|
+
end
|
37
|
+
```
|
20
38
|
|
21
39
|
## Development
|
22
40
|
|
@@ -3,7 +3,13 @@
|
|
3
3
|
module Rack
|
4
4
|
class LocaleMemorable
|
5
5
|
class Request < Rack::Request
|
6
|
-
attr_reader :explicit_locale
|
6
|
+
attr_reader :explicit_locale, :params_key, :cookie_key
|
7
|
+
|
8
|
+
def initialize(env, params_key:, cookie_key:)
|
9
|
+
super env
|
10
|
+
@params_key = params_key
|
11
|
+
@cookie_key = cookie_key
|
12
|
+
end
|
7
13
|
|
8
14
|
def detect_locale
|
9
15
|
(
|
@@ -16,11 +22,11 @@ module Rack
|
|
16
22
|
private
|
17
23
|
|
18
24
|
def from_params
|
19
|
-
@explicit_locale = primary_locale_from params[
|
25
|
+
@explicit_locale = primary_locale_from params[params_key]
|
20
26
|
end
|
21
27
|
|
22
28
|
def from_cookies
|
23
|
-
primary_locale_from cookies[
|
29
|
+
primary_locale_from cookies[cookie_key]
|
24
30
|
end
|
25
31
|
|
26
32
|
def from_headers
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module Rack
|
4
4
|
class LocaleMemorable
|
5
5
|
class Response < Rack::Response
|
6
|
-
def remember_locale(explicit_locale, secure_cookie:, cookie_expiry:)
|
7
|
-
set_cookie
|
6
|
+
def remember_locale(explicit_locale, secure_cookie:, cookie_expiry:, cookie_key:)
|
7
|
+
set_cookie cookie_key, {
|
8
8
|
value: explicit_locale,
|
9
9
|
expires: cookie_expiry,
|
10
10
|
http_only: true,
|
@@ -7,14 +7,16 @@ require 'rack'
|
|
7
7
|
|
8
8
|
module Rack
|
9
9
|
class LocaleMemorable
|
10
|
-
def initialize(app, secure_cookie: true, cookie_expiry: 1.year.from_now)
|
10
|
+
def initialize(app, params_key: 'locale', cookie_key: 'locale', secure_cookie: true, cookie_expiry: 1.year.from_now)
|
11
11
|
@app = app
|
12
|
+
@params_key = params_key
|
13
|
+
@cookie_key = cookie_key
|
12
14
|
@secure_cookie = secure_cookie
|
13
15
|
@cookie_expiry = cookie_expiry
|
14
16
|
end
|
15
17
|
|
16
18
|
def call(env)
|
17
|
-
request = Request.new(env)
|
19
|
+
request = Request.new(env, params_key: @params_key, cookie_key: @cookie_key)
|
18
20
|
I18n.with_locale(request.detect_locale) do
|
19
21
|
status, headers, body = @app.call(env)
|
20
22
|
response = Response.new body, status, headers
|
@@ -22,7 +24,8 @@ module Rack
|
|
22
24
|
response.remember_locale(
|
23
25
|
request.explicit_locale,
|
24
26
|
secure_cookie: @secure_cookie,
|
25
|
-
cookie_expiry: @cookie_expiry
|
27
|
+
cookie_expiry: @cookie_expiry,
|
28
|
+
cookie_key: @cookie_key
|
26
29
|
)
|
27
30
|
end
|
28
31
|
response.finish
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-locale_memorable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nov
|
@@ -163,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
163
|
- !ruby/object:Gem::Version
|
164
164
|
version: '0'
|
165
165
|
requirements: []
|
166
|
-
rubygems_version: 3.
|
166
|
+
rubygems_version: 3.1.6
|
167
167
|
signing_key:
|
168
168
|
specification_version: 4
|
169
169
|
summary: Remember locale in rack layer
|