preferred_locale 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +289 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +69 -0
- data/LICENSE.txt +21 -0
- data/README.md +66 -0
- data/Rakefile +12 -0
- data/lib/preferred_locale/auto_locale.rb +26 -0
- data/lib/preferred_locale/header_parser.rb +25 -0
- data/lib/preferred_locale/version.rb +5 -0
- data/lib/preferred_locale.rb +40 -0
- data/sig/preferred_locale.rbs +23 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 25d7d501311ec07eb4453107066175a7ff0d34493084cc3e8d0b38864147ff3e
|
4
|
+
data.tar.gz: 8bf555fc4424a836839253ba9b88c1f464d2be7d97d1d7c7f20b07ee03ac8f39
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b306fd65e2dfe7203705a4904b9ba5df4eb370695aa453ecb8685df8201ddc358736a200a515cd2afad41025a628c5f0bec7162087e1b8f499ad8203a5698ab1
|
7
|
+
data.tar.gz: 67a93005c500cc999f628a0b016ae9454b55bb8cf64c4cff65597cc9a7b08b24340ecd9336223bc61607362d3608f0c85b6e47d007094cd0dedeee2b6411c208
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,289 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
- rubocop-performance
|
4
|
+
AllCops:
|
5
|
+
NewCops: enable
|
6
|
+
DisplayCopNames: false
|
7
|
+
DisplayStyleGuide: true
|
8
|
+
StyleGuideCopsOnly: false
|
9
|
+
TargetRubyVersion: 2.7
|
10
|
+
Layout/ArgumentAlignment:
|
11
|
+
EnforcedStyle: with_fixed_indentation
|
12
|
+
Style/BlockDelimiters:
|
13
|
+
EnforcedStyle: braces_for_chaining
|
14
|
+
Style/CollectionMethods:
|
15
|
+
PreferredMethods:
|
16
|
+
collect: map
|
17
|
+
collect!: map!
|
18
|
+
inject: reduce
|
19
|
+
detect: find
|
20
|
+
find_all: select
|
21
|
+
Style/CommentAnnotation:
|
22
|
+
Enabled: true
|
23
|
+
Keywords:
|
24
|
+
- TODO
|
25
|
+
- FIXME
|
26
|
+
- OPTIMIZE
|
27
|
+
- HACK
|
28
|
+
- REVIEW
|
29
|
+
- DEPRECATED
|
30
|
+
Layout/DotPosition:
|
31
|
+
EnforcedStyle: leading
|
32
|
+
Layout/EmptyLineAfterGuardClause:
|
33
|
+
Enabled: false
|
34
|
+
Style/FormatString:
|
35
|
+
Enabled: true
|
36
|
+
EnforcedStyle: format
|
37
|
+
Style/FrozenStringLiteralComment:
|
38
|
+
SafeAutoCorrect: true
|
39
|
+
Enabled: true
|
40
|
+
Style/GlobalVars:
|
41
|
+
Enabled: false
|
42
|
+
Style/GuardClause:
|
43
|
+
Enabled: true
|
44
|
+
MinBodyLength: 3
|
45
|
+
Style/IfUnlessModifier:
|
46
|
+
Enabled: true
|
47
|
+
Layout/FirstArrayElementIndentation:
|
48
|
+
EnforcedStyle: consistent
|
49
|
+
Layout/FirstHashElementIndentation:
|
50
|
+
EnforcedStyle: consistent
|
51
|
+
Style/LambdaCall:
|
52
|
+
Enabled: true
|
53
|
+
Style/Next:
|
54
|
+
Enabled: true
|
55
|
+
MinBodyLength: 3
|
56
|
+
Style/NumericLiterals:
|
57
|
+
Enabled: true
|
58
|
+
MinDigits: 5
|
59
|
+
Style/PercentLiteralDelimiters:
|
60
|
+
Enabled: true
|
61
|
+
PreferredDelimiters:
|
62
|
+
'%': '[]'
|
63
|
+
'%i': '[]'
|
64
|
+
'%q': '[]'
|
65
|
+
'%Q': '[]'
|
66
|
+
'%r': '{}'
|
67
|
+
'%s': '()'
|
68
|
+
'%w': '[]'
|
69
|
+
'%W': '[]'
|
70
|
+
'%x': '[]'
|
71
|
+
Style/RaiseArgs:
|
72
|
+
Enabled: true
|
73
|
+
EnforcedStyle: exploded
|
74
|
+
Style/SingleLineMethods:
|
75
|
+
Enabled: true
|
76
|
+
AllowIfMethodIsEmpty: true
|
77
|
+
Style/StringLiterals:
|
78
|
+
EnforcedStyle: single_quotes
|
79
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
80
|
+
Enabled: true
|
81
|
+
EnforcedStyle: space
|
82
|
+
Layout/SpaceBeforeBlockBraces:
|
83
|
+
Enabled: true
|
84
|
+
EnforcedStyle: space
|
85
|
+
Layout/SpaceInsideBlockBraces:
|
86
|
+
Enabled: true
|
87
|
+
EnforcedStyle: space
|
88
|
+
EnforcedStyleForEmptyBraces: no_space
|
89
|
+
SpaceBeforeBlockParameters: true
|
90
|
+
Layout/SpaceInsideHashLiteralBraces:
|
91
|
+
Enabled: true
|
92
|
+
EnforcedStyle: space
|
93
|
+
EnforcedStyleForEmptyBraces: no_space
|
94
|
+
Style/SymbolProc:
|
95
|
+
Enabled: true
|
96
|
+
Layout/TrailingEmptyLines:
|
97
|
+
Enabled: true
|
98
|
+
EnforcedStyle: final_newline
|
99
|
+
Style/TrailingCommaInArguments:
|
100
|
+
Enabled: true
|
101
|
+
EnforcedStyleForMultiline: no_comma
|
102
|
+
Style/TrailingCommaInArrayLiteral:
|
103
|
+
Enabled: true
|
104
|
+
EnforcedStyleForMultiline: no_comma
|
105
|
+
Style/TrailingCommaInHashLiteral:
|
106
|
+
Enabled: true
|
107
|
+
EnforcedStyleForMultiline: no_comma
|
108
|
+
Style/TrivialAccessors:
|
109
|
+
Enabled: true
|
110
|
+
AllowDSLWriters: true
|
111
|
+
Naming/VariableName:
|
112
|
+
Enabled: true
|
113
|
+
EnforcedStyle: snake_case
|
114
|
+
Style/WordArray:
|
115
|
+
Enabled: true
|
116
|
+
MinSize: 2
|
117
|
+
Metrics/AbcSize:
|
118
|
+
Enabled: false
|
119
|
+
Metrics/BlockNesting:
|
120
|
+
Enabled: true
|
121
|
+
Max: 5
|
122
|
+
Metrics/BlockLength:
|
123
|
+
Enabled: false
|
124
|
+
Metrics/ClassLength:
|
125
|
+
Enabled: true
|
126
|
+
CountComments: false
|
127
|
+
Max: 500
|
128
|
+
Metrics/ModuleLength:
|
129
|
+
Enabled: true
|
130
|
+
CountComments: false
|
131
|
+
Max: 500
|
132
|
+
Metrics/CyclomaticComplexity:
|
133
|
+
Enabled: false
|
134
|
+
Layout/LineLength:
|
135
|
+
Enabled: true
|
136
|
+
Max: 100
|
137
|
+
AllowURI: true
|
138
|
+
IgnoreCopDirectives: true
|
139
|
+
Exclude:
|
140
|
+
- config/routes.rb
|
141
|
+
Metrics/MethodLength:
|
142
|
+
Enabled: true
|
143
|
+
CountComments: false
|
144
|
+
Max: 100
|
145
|
+
Metrics/ParameterLists:
|
146
|
+
Enabled: true
|
147
|
+
Max: 5
|
148
|
+
CountKeywordArgs: false
|
149
|
+
Metrics/PerceivedComplexity:
|
150
|
+
Enabled: false
|
151
|
+
Lint/AssignmentInCondition:
|
152
|
+
Enabled: true
|
153
|
+
AllowSafeAssignment: true
|
154
|
+
Style/InlineComment:
|
155
|
+
Enabled: false
|
156
|
+
Style/MethodCalledOnDoEndBlock:
|
157
|
+
Enabled: true
|
158
|
+
Style/SymbolArray:
|
159
|
+
Enabled: true
|
160
|
+
Naming/AccessorMethodName:
|
161
|
+
Enabled: true
|
162
|
+
Style/Alias:
|
163
|
+
Enabled: true
|
164
|
+
EnforcedStyle: prefer_alias_method
|
165
|
+
Style/ArrayJoin:
|
166
|
+
Enabled: true
|
167
|
+
Style/AsciiComments:
|
168
|
+
Enabled: false
|
169
|
+
Naming/AsciiIdentifiers:
|
170
|
+
Enabled: true
|
171
|
+
Style/Attr:
|
172
|
+
Enabled: true
|
173
|
+
Style/BlockComments:
|
174
|
+
Enabled: true
|
175
|
+
Style/ColonMethodCall:
|
176
|
+
Enabled: true
|
177
|
+
Style/Documentation:
|
178
|
+
Enabled: false
|
179
|
+
Style/EmptyLiteral:
|
180
|
+
Enabled: true
|
181
|
+
Style/EvenOdd:
|
182
|
+
Enabled: true
|
183
|
+
Lint/FlipFlop:
|
184
|
+
Enabled: true
|
185
|
+
Style/IfWithSemicolon:
|
186
|
+
Enabled: true
|
187
|
+
Style/Lambda:
|
188
|
+
EnforcedStyle: literal
|
189
|
+
# HACK: Until Rubocop is released with EnforcedStyle
|
190
|
+
Enabled: true
|
191
|
+
Layout/LeadingCommentSpace:
|
192
|
+
Enabled: true
|
193
|
+
Style/MultilineBlockChain:
|
194
|
+
Enabled: true
|
195
|
+
Style/MultilineTernaryOperator:
|
196
|
+
Enabled: true
|
197
|
+
Style/NegatedIf:
|
198
|
+
Enabled: true
|
199
|
+
Style/NestedTernaryOperator:
|
200
|
+
Enabled: true
|
201
|
+
Style/NilComparison:
|
202
|
+
Enabled: true
|
203
|
+
Style/Not:
|
204
|
+
Enabled: true
|
205
|
+
Style/OneLineConditional:
|
206
|
+
Enabled: true
|
207
|
+
Style/PerlBackrefs:
|
208
|
+
Enabled: true
|
209
|
+
Style/RedundantBegin:
|
210
|
+
Enabled: true
|
211
|
+
Style/RedundantSelf:
|
212
|
+
Enabled: true
|
213
|
+
Style/RescueModifier:
|
214
|
+
Enabled: true
|
215
|
+
Style/SelfAssignment:
|
216
|
+
Enabled: true
|
217
|
+
Style/ClassAndModuleChildren:
|
218
|
+
Enabled: true
|
219
|
+
Exclude:
|
220
|
+
- app/graphql/**/*
|
221
|
+
Style/HashEachMethods:
|
222
|
+
Enabled: true
|
223
|
+
Style/HashTransformKeys:
|
224
|
+
Enabled: true
|
225
|
+
Style/HashTransformValues:
|
226
|
+
Enabled: true
|
227
|
+
Layout/SpaceBeforeFirstArg:
|
228
|
+
Enabled: true
|
229
|
+
Layout/SpaceAfterColon:
|
230
|
+
Enabled: true
|
231
|
+
Layout/SpaceAfterComma:
|
232
|
+
Enabled: true
|
233
|
+
Layout/SpaceAroundKeyword:
|
234
|
+
Enabled: true
|
235
|
+
Layout/SpaceAfterMethodName:
|
236
|
+
Enabled: true
|
237
|
+
Layout/SpaceAfterNot:
|
238
|
+
Enabled: true
|
239
|
+
Layout/SpaceAfterSemicolon:
|
240
|
+
Enabled: true
|
241
|
+
Layout/SpaceBeforeComma:
|
242
|
+
Enabled: true
|
243
|
+
Layout/SpaceBeforeComment:
|
244
|
+
Enabled: true
|
245
|
+
Layout/SpaceBeforeSemicolon:
|
246
|
+
Enabled: true
|
247
|
+
Layout/SpaceAroundOperators:
|
248
|
+
Enabled: true
|
249
|
+
Layout/SpaceInsideParens:
|
250
|
+
Enabled: true
|
251
|
+
Layout/SpaceInsideRangeLiteral:
|
252
|
+
Enabled: true
|
253
|
+
Style/SpecialGlobalVars:
|
254
|
+
Enabled: true
|
255
|
+
Layout/TrailingWhitespace:
|
256
|
+
Enabled: true
|
257
|
+
Style/UnlessElse:
|
258
|
+
Enabled: true
|
259
|
+
Style/VariableInterpolation:
|
260
|
+
Enabled: true
|
261
|
+
Style/WhenThen:
|
262
|
+
Enabled: true
|
263
|
+
Lint/Debugger:
|
264
|
+
Enabled: true
|
265
|
+
Lint/DeprecatedClassMethods:
|
266
|
+
Enabled: true
|
267
|
+
Security/Eval:
|
268
|
+
Enabled: true
|
269
|
+
Lint/SuppressedException:
|
270
|
+
Enabled: true
|
271
|
+
Lint/LiteralInInterpolation:
|
272
|
+
Enabled: true
|
273
|
+
Lint/Loop:
|
274
|
+
Enabled: true
|
275
|
+
Lint/ParenthesesAsGroupedExpression:
|
276
|
+
Enabled: true
|
277
|
+
Lint/RequireParentheses:
|
278
|
+
Enabled: false
|
279
|
+
Lint/UnderscorePrefixedVariableName:
|
280
|
+
Enabled: true
|
281
|
+
Lint/UselessSetterCall:
|
282
|
+
Enabled: true
|
283
|
+
RSpec/ExampleLength:
|
284
|
+
Max: 20
|
285
|
+
RSpec/ImplicitSubject:
|
286
|
+
EnforcedStyle: single_statement_only
|
287
|
+
Enabled: true
|
288
|
+
RSpec/NamedSubject:
|
289
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
preferred_locale (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.5.0)
|
11
|
+
json (2.6.3)
|
12
|
+
parallel (1.23.0)
|
13
|
+
parser (3.2.2.1)
|
14
|
+
ast (~> 2.4.1)
|
15
|
+
rainbow (3.1.1)
|
16
|
+
rake (13.0.6)
|
17
|
+
regexp_parser (2.8.0)
|
18
|
+
rexml (3.2.5)
|
19
|
+
rspec (3.12.0)
|
20
|
+
rspec-core (~> 3.12.0)
|
21
|
+
rspec-expectations (~> 3.12.0)
|
22
|
+
rspec-mocks (~> 3.12.0)
|
23
|
+
rspec-core (3.12.2)
|
24
|
+
rspec-support (~> 3.12.0)
|
25
|
+
rspec-expectations (3.12.3)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.12.0)
|
28
|
+
rspec-mocks (3.12.5)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.12.0)
|
31
|
+
rspec-support (3.12.0)
|
32
|
+
rubocop (1.50.2)
|
33
|
+
json (~> 2.3)
|
34
|
+
parallel (~> 1.10)
|
35
|
+
parser (>= 3.2.0.0)
|
36
|
+
rainbow (>= 2.2.2, < 4.0)
|
37
|
+
regexp_parser (>= 1.8, < 3.0)
|
38
|
+
rexml (>= 3.2.5, < 4.0)
|
39
|
+
rubocop-ast (>= 1.28.0, < 2.0)
|
40
|
+
ruby-progressbar (~> 1.7)
|
41
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
42
|
+
rubocop-ast (1.28.1)
|
43
|
+
parser (>= 3.2.1.0)
|
44
|
+
rubocop-capybara (2.18.0)
|
45
|
+
rubocop (~> 1.41)
|
46
|
+
rubocop-performance (1.17.1)
|
47
|
+
rubocop (>= 1.7.0, < 2.0)
|
48
|
+
rubocop-ast (>= 0.4.0)
|
49
|
+
rubocop-rspec (2.21.0)
|
50
|
+
rubocop (~> 1.33)
|
51
|
+
rubocop-capybara (~> 2.17)
|
52
|
+
ruby-progressbar (1.13.0)
|
53
|
+
unicode-display_width (2.4.2)
|
54
|
+
|
55
|
+
PLATFORMS
|
56
|
+
ruby
|
57
|
+
x86_64-darwin-19
|
58
|
+
x86_64-linux
|
59
|
+
|
60
|
+
DEPENDENCIES
|
61
|
+
preferred_locale!
|
62
|
+
rake
|
63
|
+
rspec
|
64
|
+
rubocop
|
65
|
+
rubocop-performance
|
66
|
+
rubocop-rspec
|
67
|
+
|
68
|
+
BUNDLED WITH
|
69
|
+
2.3.13
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Emma Lejeck
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# PreferredLocale
|
2
|
+
|
3
|
+
A gem to help you find the best locales between what you support and what a user prefers. In
|
4
|
+
practice, this usually means finding the best locale between `I18n.available_locales` and the
|
5
|
+
`Accept-Language` header from the request, but it could also be used to find the best locale between
|
6
|
+
something in your database and a user's preferences.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Install the gem and add to the application's Gemfile by executing:
|
11
|
+
|
12
|
+
```shell
|
13
|
+
$ bundle add preferred_locale
|
14
|
+
```
|
15
|
+
|
16
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
17
|
+
|
18
|
+
```shell
|
19
|
+
$ gem install preferred_locale
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
To use PreferredLocale directly, you can create a new instance with your available locales:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
preferred_locale = PreferredLocale.new(available: ['en', 'fr', 'es'])
|
28
|
+
```
|
29
|
+
|
30
|
+
Then, you can find the available locales from a given list of locales from the user:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
preferred_locale.acceptable_for(['en-US', 'fr', 'ja-JP']) # => ['en', 'fr']
|
34
|
+
```
|
35
|
+
|
36
|
+
Or you can just get the best one:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
preferred_locale.preferred_for(['en-US', 'fr', 'ja-JP']) # => 'en'
|
40
|
+
```
|
41
|
+
|
42
|
+
### With Rails
|
43
|
+
|
44
|
+
PreferredLocale can be used with Rails by including the `PreferredLocale::AutoLocale` module in your
|
45
|
+
controller, which will automatically set the locale based on the `Accept-Language` header from the
|
46
|
+
request.
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run
|
51
|
+
the tests. You can also run `bin/console` for an interactive prompt that will allow you to
|
52
|
+
experiment.
|
53
|
+
|
54
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new
|
55
|
+
version, update the version number in `version.rb`, and then run `bundle exec rake release`, which
|
56
|
+
will create a git tag for the version, push git commits and the created tag, and push the `.gem`
|
57
|
+
file to [rubygems.org](https://rubygems.org).
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hummingbird-me/preferred_locale.
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the [MIT
|
66
|
+
License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
class PreferredLocale
|
6
|
+
module AutoLocale
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
prepend_before_action :set_locale
|
11
|
+
end
|
12
|
+
|
13
|
+
def set_locale
|
14
|
+
I18n.locale = preferred_locale || I18n.default_locale
|
15
|
+
end
|
16
|
+
|
17
|
+
def preferred_locale
|
18
|
+
acceptable = HeaderParser.new(request.env['HTTP_ACCEPT_LANGUAGE']).preferred_locales
|
19
|
+
PreferredLocale.new(
|
20
|
+
available: I18n.available_locales
|
21
|
+
).preferred_for(
|
22
|
+
locales: acceptable
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PreferredLocale
|
4
|
+
class HeaderParser
|
5
|
+
def initialize(header)
|
6
|
+
@header = header.to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
def raw_locales
|
10
|
+
@raw_locales ||= begin
|
11
|
+
values = @header.split(/,\s*/).map do |item|
|
12
|
+
locale, quality = item.split(';q=')
|
13
|
+
[locale, quality ? quality.to_f : 1.0]
|
14
|
+
end
|
15
|
+
values.sort { |(_, a), (_, b)| b <=> a }.filter_map(&:first)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def preferred_locales
|
20
|
+
raw_locales.filter do |locale|
|
21
|
+
locale.match(/^[-a-z0-9]+$/i)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'preferred_locale/version'
|
4
|
+
require_relative 'preferred_locale/header_parser'
|
5
|
+
require_relative 'preferred_locale/auto_locale' if defined?(Rails)
|
6
|
+
|
7
|
+
class PreferredLocale
|
8
|
+
def initialize(available: [])
|
9
|
+
# We need this to avoid overwriting explicit options with implicit ones
|
10
|
+
available_lower = available.map { |locale| locale.to_s.downcase }
|
11
|
+
|
12
|
+
# Create a map of "implicit" available locales to their "canonical" form
|
13
|
+
@available = available.each_with_object({}) do |locale, obj|
|
14
|
+
locale_str = locale.to_s
|
15
|
+
obj[locale_str.downcase] = locale
|
16
|
+
# Add the language without a country if it's not already in the list
|
17
|
+
language = locale_str.downcase.split('-')[0]
|
18
|
+
obj[language] = locale unless available_lower.include?(language)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def acceptable_for(locales: [])
|
23
|
+
# Build a candidate list including our implicit candidate locales
|
24
|
+
candidates = locales.flat_map do |locale|
|
25
|
+
[locale.to_s.downcase, locale.to_s.split('-')[0].downcase]
|
26
|
+
end
|
27
|
+
|
28
|
+
# Figure out which candidates are available
|
29
|
+
available = candidates.filter do |locale|
|
30
|
+
@available.key?(locale.downcase)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Convert to the canonical form and then remove duplicates
|
34
|
+
available.map { |locale| @available[locale.downcase] }.uniq
|
35
|
+
end
|
36
|
+
|
37
|
+
def preferred_for(locales: [])
|
38
|
+
acceptable_for(locales: locales)[0]
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class PreferredLocale
|
2
|
+
VERSION: String
|
3
|
+
|
4
|
+
def initialize: (available: Array[String]) -> void
|
5
|
+
|
6
|
+
def acceptable_for: (locales: Array[String]) -> Array[String]
|
7
|
+
|
8
|
+
def preferred_for: (locales: Array[String]) -> String | nil
|
9
|
+
|
10
|
+
class HeaderParser
|
11
|
+
def initialize: (header: String) -> void
|
12
|
+
|
13
|
+
def raw_locales: () -> Array[String]
|
14
|
+
|
15
|
+
def preferred_locales: () -> Array[String]
|
16
|
+
end
|
17
|
+
|
18
|
+
module AutoLocale
|
19
|
+
def preferred_locale: () -> String
|
20
|
+
|
21
|
+
def set_locale: () -> void
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: preferred_locale
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Emma Lejeck
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- nuck@kitsu.io
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/preferred_locale.rb
|
29
|
+
- lib/preferred_locale/auto_locale.rb
|
30
|
+
- lib/preferred_locale/header_parser.rb
|
31
|
+
- lib/preferred_locale/version.rb
|
32
|
+
- sig/preferred_locale.rbs
|
33
|
+
homepage: https://github.com/hummingbird-me/preferred_locale
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata:
|
37
|
+
homepage_uri: https://github.com/hummingbird-me/preferred_locale
|
38
|
+
source_code_uri: https://github.com/hummingbird-me/preferred_locale
|
39
|
+
changelog_uri: https://github.com/hummingbird-me/preferred_locale/blob/main/CHANGELOG.md
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.7.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.1.6
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Get the user's preferred locale from a list of available locales
|
59
|
+
test_files: []
|