rack-user-locale 0.5.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/VERSION +1 -1
- data/lib/user_locale.rb +102 -0
- metadata +3 -73
- data/lib/user-locale.rb +0 -102
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a996b94aedc7a9d81e79b92a22ea8b89eaaefee9b4d757f8409ccdf951980cf
|
4
|
+
data.tar.gz: 94c2d1fa648b0921b9ad1cedf4a0b3c52fb371708745161bd6c3f80f4be93e50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7d017f4c32cbed718cb66c97d761f9a8cfc2b6fabe563b8bc4e37c54a53f4268764fe3652d7f328d165e2c96f3075fbe5c2695b0158ef5406b988746d07a86e
|
7
|
+
data.tar.gz: dc4303e47bf37925d0e739b560a32845cff61f5f5ad40a784c953896b0b26e6228a46dbbb21b47f74973c6bdf86badee137186495350b1bb7ffceb2ad681f57a
|
data/README.md
CHANGED
@@ -86,6 +86,6 @@ use Rack::UserLocale, cookie_name: "some-cookie-name"
|
|
86
86
|
|
87
87
|
## Copyright
|
88
88
|
|
89
|
-
Copyright (c)
|
89
|
+
Copyright (c) 2020 [Stuart Chinery](https://github.com/schinery) and [Dave Hrycyszyn](https://github.com/futurechimp).
|
90
90
|
|
91
91
|
See LICENSE.txt for further details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/lib/user_locale.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "i18n"
|
4
|
+
|
5
|
+
module Rack
|
6
|
+
class UserLocale
|
7
|
+
attr_reader :app, :body, :env, :headers, :options, :status
|
8
|
+
|
9
|
+
def initialize(app, options = {})
|
10
|
+
@app = app
|
11
|
+
@options = {
|
12
|
+
accepted_locales: [],
|
13
|
+
cookie_name: "user-locale"
|
14
|
+
}.merge(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
@env = env
|
19
|
+
set_i18n_locale
|
20
|
+
app.call(env) && return unless request.get?
|
21
|
+
|
22
|
+
@status, @headers, @body = app.call(env)
|
23
|
+
set_response_cookie
|
24
|
+
response.finish
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def accepted_locale(locale, other_locale = nil)
|
30
|
+
options[:accepted_locales].include?(locale) ? locale : other_locale
|
31
|
+
end
|
32
|
+
|
33
|
+
def browser_locale
|
34
|
+
return if http_accept_languages.nil?
|
35
|
+
|
36
|
+
if check_accepted?
|
37
|
+
weighted_langs.each do |lang|
|
38
|
+
l = accepted_locale(split_lang(lang.first).to_sym)
|
39
|
+
return l unless l.nil?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
split_lang(weighted_langs.first.first)
|
44
|
+
end
|
45
|
+
|
46
|
+
def check_accepted?
|
47
|
+
@check_accepted ||= options[:accepted_locales].count.positive?
|
48
|
+
end
|
49
|
+
|
50
|
+
def cookie_locale
|
51
|
+
@cookie_locale ||= request.cookies[options[:cookie_name]]
|
52
|
+
end
|
53
|
+
|
54
|
+
def default_locale
|
55
|
+
@default_locale ||= I18n.default_locale
|
56
|
+
end
|
57
|
+
|
58
|
+
def http_accept_languages
|
59
|
+
@http_accept_languages ||= env["HTTP_ACCEPT_LANGUAGE"]
|
60
|
+
end
|
61
|
+
|
62
|
+
def locale
|
63
|
+
@locale ||= cookie_locale || browser_locale || default_locale
|
64
|
+
end
|
65
|
+
|
66
|
+
def request
|
67
|
+
@request ||= Rack::Request.new(env)
|
68
|
+
end
|
69
|
+
|
70
|
+
def response
|
71
|
+
@response ||= Rack::Response.new(body, status, headers)
|
72
|
+
end
|
73
|
+
|
74
|
+
def set_i18n_locale
|
75
|
+
new_locale = check_accepted? ? accepted_locale(locale.to_sym, default_locale) : locale.to_sym
|
76
|
+
I18n.locale = env["rack.locale"] = new_locale
|
77
|
+
end
|
78
|
+
|
79
|
+
def set_response_cookie
|
80
|
+
return if cookie_locale == I18n.locale.to_s
|
81
|
+
|
82
|
+
response.set_cookie(options[:cookie_name], value: I18n.locale, path: "/")
|
83
|
+
end
|
84
|
+
|
85
|
+
def split_http_accept_languages
|
86
|
+
@split_http_accept_languages ||= http_accept_languages.split(",").map do |l|
|
87
|
+
l += ";q=1.0" unless l =~ /;q=\d+\.\d+$/
|
88
|
+
l.split(";q=")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def split_lang(lang)
|
93
|
+
return if lang.nil?
|
94
|
+
|
95
|
+
lang.split("-").first
|
96
|
+
end
|
97
|
+
|
98
|
+
def weighted_langs
|
99
|
+
@weighted_langs ||= split_http_accept_languages.sort { |a, b| b[1] <=> a[1] }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-user-locale
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stuart Chinery
|
@@ -38,76 +38,6 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 2.0.8
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: bundler
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '2.0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '2.0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: minitest
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '5.13'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '5.13'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: minitest-fail-fast
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0.1'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0.1'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: minitest-macos-notification
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0.2'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - "~>"
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0.2'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: minitest-reporters
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - "~>"
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '1.4'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - "~>"
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '1.4'
|
111
41
|
- !ruby/object:Gem::Dependency
|
112
42
|
name: rack-test
|
113
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -149,7 +79,7 @@ files:
|
|
149
79
|
- Rakefile
|
150
80
|
- VERSION
|
151
81
|
- lib/rack/user-locale/version.rb
|
152
|
-
- lib/
|
82
|
+
- lib/user_locale.rb
|
153
83
|
homepage: https://github.com/schinery/rack-user-locale
|
154
84
|
licenses:
|
155
85
|
- MIT
|
@@ -169,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
169
99
|
- !ruby/object:Gem::Version
|
170
100
|
version: '0'
|
171
101
|
requirements: []
|
172
|
-
rubygems_version: 3.
|
102
|
+
rubygems_version: 3.1.2
|
173
103
|
signing_key:
|
174
104
|
specification_version: 4
|
175
105
|
summary: A Rack module for getting and setting a user's locale
|
data/lib/user-locale.rb
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "i18n"
|
4
|
-
|
5
|
-
module Rack
|
6
|
-
class UserLocale
|
7
|
-
# TODO: Write notes
|
8
|
-
#
|
9
|
-
def initialize(app, options = {})
|
10
|
-
@app = app
|
11
|
-
@options = {
|
12
|
-
accepted_locales: [],
|
13
|
-
cookie_name: "user-locale"
|
14
|
-
}.merge(options)
|
15
|
-
end
|
16
|
-
|
17
|
-
# TODO: Write notes
|
18
|
-
#
|
19
|
-
def call(env)
|
20
|
-
@env = env
|
21
|
-
@request = Rack::Request.new(@env)
|
22
|
-
set_locale
|
23
|
-
|
24
|
-
@app.call(env) && return if @request.post? || @request.put? || @request.delete?
|
25
|
-
|
26
|
-
status, headers, body = @app.call(@env)
|
27
|
-
response = Rack::Response.new(body, status, headers)
|
28
|
-
response.set_cookie(@options[:cookie_name], value: I18n.locale, path: "/") if cookie_locale != I18n.locale.to_s
|
29
|
-
response.finish
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
# TODO: Write notes
|
35
|
-
#
|
36
|
-
def set_locale
|
37
|
-
new_locale = check_accepted? ? accepted_locale(locale.to_sym, default_locale) : locale.to_sym
|
38
|
-
I18n.locale = @env["rack.locale"] = new_locale
|
39
|
-
end
|
40
|
-
|
41
|
-
# TODO: Write notes
|
42
|
-
#
|
43
|
-
def accepted_locale(locale, other_locale = nil)
|
44
|
-
@options[:accepted_locales].include?(locale) ? locale : other_locale
|
45
|
-
end
|
46
|
-
|
47
|
-
# TODO: Write notes
|
48
|
-
#
|
49
|
-
def locale
|
50
|
-
cookie_locale || browser_locale || default_locale
|
51
|
-
end
|
52
|
-
|
53
|
-
# TODO: Write notes
|
54
|
-
#
|
55
|
-
def cookie_locale
|
56
|
-
@request.cookies[@options[:cookie_name]]
|
57
|
-
end
|
58
|
-
|
59
|
-
# TODO: Write notes
|
60
|
-
#
|
61
|
-
def browser_locale
|
62
|
-
accept_lang = @env["HTTP_ACCEPT_LANGUAGE"]
|
63
|
-
return if accept_lang.nil?
|
64
|
-
|
65
|
-
langs = accept_lang.split(",").map do |l|
|
66
|
-
l += ";q=1.0" unless l =~ /;q=\d+\.\d+$/
|
67
|
-
l.split(";q=")
|
68
|
-
end
|
69
|
-
|
70
|
-
langs.sort! { |a, b| b[1] <=> a[1] }
|
71
|
-
|
72
|
-
if check_accepted?
|
73
|
-
langs.each do |lang|
|
74
|
-
l = accepted_locale(split_lang(lang.first).to_sym)
|
75
|
-
return l unless l.nil?
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
split_lang(langs.first.first)
|
80
|
-
end
|
81
|
-
|
82
|
-
# TODO: Write notes
|
83
|
-
#
|
84
|
-
def split_lang(lang)
|
85
|
-
return if lang.nil?
|
86
|
-
|
87
|
-
lang.split("-").first
|
88
|
-
end
|
89
|
-
|
90
|
-
# TODO: Write notes
|
91
|
-
#
|
92
|
-
def default_locale
|
93
|
-
I18n.default_locale
|
94
|
-
end
|
95
|
-
|
96
|
-
# TODO: Write notes
|
97
|
-
#
|
98
|
-
def check_accepted?
|
99
|
-
@options[:accepted_locales].count.positive?
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|