rack-user-locale 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +2 -1
  4. data/VERSION +1 -1
  5. data/lib/user_locale.rb +21 -17
  6. metadata +12 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39d2236bcd061b79e92a196c564fc2b3e09053ed063a272128cc81d29610d4f2
4
- data.tar.gz: cca5a0b5b88dd00cd575ee3ae811f41ce7d233a7ddf11be6d99fd450c80efdd7
3
+ metadata.gz: 9dc51ae0380e03d6cb6b2d97f76c35046ce797743542438d4034b325208f858e
4
+ data.tar.gz: 771dfb2612220a99c331d38b1814a1559f9fcacdb3e1ed415547cf0e4dd268c5
5
5
  SHA512:
6
- metadata.gz: '0533867d9f8ce4113b9c3b6cafea2bc016508b05755019d89b04a94baec798a12e74802ef5ec55a9546986ffb43f0cf4d4df509c778380b02ba386692b4095ae'
7
- data.tar.gz: 8558978f95d16347b585367c987e99e5c52f0bc6cc41f83d9f25b59f063f7daea18310254ce334a2dc04e98cdf779dd8643a87a379820b042d3ed44493febee6
6
+ metadata.gz: b1aa1b2a4b177f9d7b0df84ce5c7b64e170a47e999f35732ed89fb7c787ddb1fc3483710d217e2480af3be1765d412bcf4ca9aa1cf9650339ecf27e9394ad62f
7
+ data.tar.gz: d4ec84e5e50d6a1a99591a32eec18bd5f35641b7d1a163e113f8b5520202575fa804965d1658342bbf125379f609a8cfa49efb0caa5fcd66d4f0544fdab9a418
@@ -1,4 +1,4 @@
1
- Copyright 2020 Stuart Chinery
1
+ Copyright (c) 2020 Stuart Chinery
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Rack User Locale
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/rack-user-locale.png)](http://badge.fury.io/rb/rack-user-locale) [![Build Status](https://secure.travis-ci.org/schinery/rack-user-locale.png)](http://travis-ci.org/schinery/rack-user-locale)
3
+ [![Gem Version](https://badge.fury.io/rb/rack-user-locale.png)](http://badge.fury.io/rb/rack-user-locale)
4
+ [![Build Status](https://secure.travis-ci.org/schinery/rack-user-locale.png)](http://travis-ci.org/schinery/rack-user-locale)
4
5
 
5
6
  A Rack module for getting and setting a user's locale via a cookie or browser default language.
6
7
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.1.0
@@ -16,7 +16,7 @@ module Rack
16
16
 
17
17
  def call(env)
18
18
  @env = env
19
- set_i18n_locale
19
+ set_i18n
20
20
  app.call(env) && return unless request.get?
21
21
 
22
22
  @status, @headers, @body = app.call(env)
@@ -31,16 +31,7 @@ module Rack
31
31
  end
32
32
 
33
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)
34
+ @browser_locale ||= detect_browser_locale
44
35
  end
45
36
 
46
37
  def check_accepted?
@@ -55,12 +46,21 @@ module Rack
55
46
  @default_locale ||= I18n.default_locale
56
47
  end
57
48
 
58
- def http_accept_languages
59
- @http_accept_languages ||= env["HTTP_ACCEPT_LANGUAGE"]
49
+ def detect_browser_locale
50
+ return if http_accept_languages.nil?
51
+
52
+ if check_accepted?
53
+ weighted_langs.each do |lang|
54
+ l = accepted_locale(split_lang(lang.first).to_sym)
55
+ return l unless l.nil?
56
+ end
57
+ end
58
+
59
+ split_lang(weighted_langs.first.first)
60
60
  end
61
61
 
62
- def locale
63
- @locale ||= cookie_locale || browser_locale || default_locale
62
+ def http_accept_languages
63
+ @http_accept_languages ||= env["HTTP_ACCEPT_LANGUAGE"]
64
64
  end
65
65
 
66
66
  def request
@@ -71,8 +71,8 @@ module Rack
71
71
  @response ||= Rack::Response.new(body, status, headers)
72
72
  end
73
73
 
74
- def set_i18n_locale
75
- new_locale = check_accepted? ? accepted_locale(locale.to_sym, default_locale) : locale.to_sym
74
+ def set_i18n
75
+ new_locale = check_accepted? ? accepted_locale(user_locale.to_sym, default_locale) : user_locale.to_sym
76
76
  I18n.locale = env["rack.locale"] = new_locale
77
77
  end
78
78
 
@@ -95,6 +95,10 @@ module Rack
95
95
  lang.split("-").first
96
96
  end
97
97
 
98
+ def user_locale
99
+ @user_locale ||= cookie_locale || browser_locale || default_locale
100
+ end
101
+
98
102
  def weighted_langs
99
103
  @weighted_langs ||= split_http_accept_languages.sort { |a, b| b[1] <=> a[1] }
100
104
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-user-locale
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stuart Chinery
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-02 00:00:00.000000000 Z
11
+ date: 2020-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -16,56 +16,56 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.7.0
19
+ version: 1.8.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 1.7.0
26
+ version: 1.8.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 2.0.8
33
+ version: 2.1.2
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 2.0.8
40
+ version: 2.1.2
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: byebug
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '11.0'
47
+ version: '11.1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '11.0'
54
+ version: '11.1'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '5.13'
61
+ version: '5.14'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '5.13'
68
+ version: '5.14'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: minitest-fail-fast
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -142,14 +142,14 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: '0.78'
145
+ version: '0.79'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: '0.78'
152
+ version: '0.79'
153
153
  description: A Rack module for getting and setting a user's locale via a cookie or
154
154
  browser default language.
155
155
  email: