raindeer 1.1.0 → 1.1.1
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/lib/sites/locale.rb +23 -9
- data/lib/version.rb +1 -1
- 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: 2ba6913d288b3b72e6573e9c00a6102c972f623ae6e79a172157295b113242cb
|
|
4
|
+
data.tar.gz: b07dfc5faa90fe83cfd1ede92d66911c902cff1b86e603bd01aef42a1e334fa4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ca803703ebd518640094fb69a73794704a514d595183661269a8b72ba636e891c4f4d476b618b9c1e2a2a3b36f4ff2da032311e520f2884d130ac62b1b84b9e9
|
|
7
|
+
data.tar.gz: 6b4e08ab82d2c192a2f5f288bb4f42854b67cbf0d98da1081c4a9116dcaeeef6b8c48dc724ffd5eb583ca813ee56eae58e62488fc5fe598391fc86e12baf25fd
|
data/lib/sites/locale.rb
CHANGED
|
@@ -5,21 +5,28 @@ require_relative 'request_header'
|
|
|
5
5
|
module Rain
|
|
6
6
|
# The cookie a visitor's chosen language is kept in, like locale=en-US, as an app's language menu sets it.
|
|
7
7
|
LOCALE_COOKIE = 'locale'
|
|
8
|
+
# The cookie a visitor's chosen country is kept in, like country=US, as an app's country menu sets it.
|
|
9
|
+
REGION_COOKIE = 'country'
|
|
8
10
|
|
|
9
11
|
# Who a response is for: the language it's written in and the region its visitor is in, which can differ, like an
|
|
10
12
|
# American reading the Australian site. The language is the one the visitor chose, by their locale cookie, or else the
|
|
11
13
|
# site's when config.yaml sets it, or else the visitor's Accept-Language among the translations there are, or else the
|
|
12
|
-
# site's own. The region is the site's, or else the country
|
|
14
|
+
# site's own. The region is the site's, or else the country the visitor chose by their country cookie, or else the one
|
|
15
|
+
# a CDN like Cloudflare found them in, or else their language's, like US for en-US.
|
|
13
16
|
Locale = Data.define(:language, :region) do
|
|
14
17
|
def self.from_request(request, site:)
|
|
15
|
-
|
|
18
|
+
language = language_of(request, site:)
|
|
19
|
+
region = site.region || chosen_region(request) || cloudflare_country(request) || language[/-([A-Za-z]{2})\z/, 1]&.upcase
|
|
20
|
+
of(language, region)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.language_of(request, site:)
|
|
16
24
|
language = chosen_language(request, site:)
|
|
17
25
|
language ||= site.language if site.fixed_language?
|
|
18
|
-
return
|
|
26
|
+
return language if language
|
|
19
27
|
|
|
20
28
|
accept_language = RequestHeader.joined(request, 'accept-language')
|
|
21
|
-
|
|
22
|
-
of(language, region)
|
|
29
|
+
Translations.negotiate(accept_language: (accept_language unless accept_language&.strip&.empty?), default: site.language)
|
|
23
30
|
end
|
|
24
31
|
|
|
25
32
|
# A locale is a value, so each one is made once rather than for every request. There are only so many, as its
|
|
@@ -48,12 +55,19 @@ module Rain
|
|
|
48
55
|
pair&.delete_prefix("#{name}=")
|
|
49
56
|
end
|
|
50
57
|
|
|
51
|
-
# The
|
|
52
|
-
|
|
58
|
+
# The country a visitor chose, by their country cookie, or else nil.
|
|
59
|
+
def self.chosen_region(request)
|
|
60
|
+
country_code(cookie(request, REGION_COOKIE))
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The visitor's country by Cloudflare's CF-IPCountry header, or nil when there's none to go by.
|
|
53
64
|
def self.cloudflare_country(request)
|
|
54
|
-
|
|
55
|
-
|
|
65
|
+
country_code(RequestHeader.first(request, 'cf-ipcountry'))
|
|
66
|
+
end
|
|
56
67
|
|
|
68
|
+
# A two letter country code in capitals, or nil for anything else, like XX for a country Cloudflare can't place or
|
|
69
|
+
# T1 for a visitor using Tor.
|
|
70
|
+
def self.country_code(code)
|
|
57
71
|
code = code.to_s.upcase
|
|
58
72
|
code if code.match?(/\A[A-Z]{2}\z/) && !%w[XX T1].include?(code)
|
|
59
73
|
end
|
data/lib/version.rb
CHANGED