preferred_locale 1.0.0 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 25d7d501311ec07eb4453107066175a7ff0d34493084cc3e8d0b38864147ff3e
4
- data.tar.gz: 8bf555fc4424a836839253ba9b88c1f464d2be7d97d1d7c7f20b07ee03ac8f39
3
+ metadata.gz: 9b3dd0a6d6efe6ee0a04861617f18a144ca0055416e8625f332927656d1af7dc
4
+ data.tar.gz: 19a440f91a0cc1c84a017e7fdb9c53d7e1031111229bc5d11daad613bcf4c71a
5
5
  SHA512:
6
- metadata.gz: b306fd65e2dfe7203705a4904b9ba5df4eb370695aa453ecb8685df8201ddc358736a200a515cd2afad41025a628c5f0bec7162087e1b8f499ad8203a5698ab1
7
- data.tar.gz: 67a93005c500cc999f628a0b016ae9454b55bb8cf64c4cff65597cc9a7b08b24340ecd9336223bc61607362d3608f0c85b6e47d007094cd0dedeee2b6411c208
6
+ metadata.gz: a03628fe5782f91dc4d0f451ddb9362a15494e5b71c6b69bd301e5f33283059ea7f30552f7dcde0365600eadcf261be2ba1fad2944f22d6c0aea58bab40f6cad
7
+ data.tar.gz: fa21679df9523936a3695e06dc37320bad283eda0acb1f1bb6f1351b1c2cd1f671b55fc1a7e4a493d6ccd08cb61a1986da91acfaa0a9503e28327306bf04d705
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [1.0.0] - 2023-05-05
3
+ ## [1.1.0](https://github.com/hummingbird-me/preferred_locale/releases/tag/v1.1.0) - 2023-05-06
4
+
5
+ - Improve documentation
6
+ - Don't add an implicit (language) fallback if the language is explicitly provided elsewhere in a
7
+ list. For example, if `en-US` and `en` are both in the list, don't add `en` as a fallback for
8
+ `en-US`.
9
+
10
+ ## [1.0.0](https://github.com/hummingbird-me/preferred_locale/releases/tag/v1.0.0) - 2023-05-05
4
11
 
5
12
  - Initial release
data/Gemfile CHANGED
@@ -10,3 +10,4 @@ gem 'rspec'
10
10
  gem 'rubocop'
11
11
  gem 'rubocop-performance'
12
12
  gem 'rubocop-rspec'
13
+ gem 'simplecov'
data/Gemfile.lock CHANGED
@@ -1,13 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- preferred_locale (1.0.0)
4
+ preferred_locale (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
10
  diff-lcs (1.5.0)
11
+ docile (1.4.0)
11
12
  json (2.6.3)
12
13
  parallel (1.23.0)
13
14
  parser (3.2.2.1)
@@ -50,6 +51,12 @@ GEM
50
51
  rubocop (~> 1.33)
51
52
  rubocop-capybara (~> 2.17)
52
53
  ruby-progressbar (1.13.0)
54
+ simplecov (0.22.0)
55
+ docile (~> 1.1)
56
+ simplecov-html (~> 0.11)
57
+ simplecov_json_formatter (~> 0.1)
58
+ simplecov-html (0.12.3)
59
+ simplecov_json_formatter (0.1.4)
53
60
  unicode-display_width (2.4.2)
54
61
 
55
62
  PLATFORMS
@@ -64,6 +71,7 @@ DEPENDENCIES
64
71
  rubocop
65
72
  rubocop-performance
66
73
  rubocop-rspec
74
+ simplecov
67
75
 
68
76
  BUNDLED WITH
69
77
  2.3.13
data/README.md CHANGED
@@ -5,6 +5,42 @@ practice, this usually means finding the best locale between `I18n.available_loc
5
5
  `Accept-Language` header from the request, but it could also be used to find the best locale between
6
6
  something in your database and a user's preferences.
7
7
 
8
+ This is similar to the [http_accept_language gem](https://github.com/iain/http_accept_language) and
9
+ the [preferred-locale JS library](https://github.com/wopian/preferred-locale) in that it attempts to
10
+ resolve mismatches in specificity (ie, `en-US` vs `en`), but differs slightly in algorithm and in
11
+ API design.
12
+
13
+ ## Algorithm
14
+
15
+ For available locales from the application:
16
+
17
+ 1. Start with available locales:
18
+ - `['en-US', 'pt-BR', 'ja-JP', 'pt']`
19
+ 2. Normalize them to lowercase mapped to the original values:
20
+ - `{'en-us' => 'en-US', 'pt-br' => 'pt-BR', 'ja-jp' => 'ja-JP', 'pt' => 'pt'}`
21
+ 3. Add "implicit" locales as fallbacks:
22
+ - `{'en-us' => 'en-US', 'en' => 'en-US', 'pt-br' => 'pt-BR', 'ja-jp' => 'ja-JP', 'ja' => 'ja-JP',
23
+ 'pt' => 'pt'}`
24
+ - Note that `en` and `ja` are added as fallbacks for `en-US` and `ja-JP` respectively, but *not*
25
+ for `pt-BR` because `pt` is already in the list later.
26
+
27
+ Now to match a user's preferred locales:
28
+
29
+ 1. Start with preferred locales:
30
+ - `['en-US', 'fr', 'ja-JP', 'en']`
31
+ 2. Normalize them to lowercase:
32
+ - `['en-us', 'fr', 'ja-jp', 'en']`
33
+ 3. Add "implicit" locales as fallbacks:
34
+ - `['en-us', 'fr', 'ja-jp', 'ja', 'en']`
35
+ - As above, note that `en` is *not* added as a fallback for `en-us` because it is already in the
36
+ list.
37
+ 4. Filter out locales that are not available:
38
+ - `['en-us', 'ja-jp', 'ja', 'en']`
39
+ 5. Convert to the canonical form
40
+ - `['en-US', 'ja-JP', 'ja-JP', 'en-US']`
41
+ 6. Remove duplicates
42
+ - `['en-US', 'ja-JP']`
43
+
8
44
  ## Installation
9
45
 
10
46
  Install the gem and add to the application's Gemfile by executing:
@@ -30,13 +66,13 @@ preferred_locale = PreferredLocale.new(available: ['en', 'fr', 'es'])
30
66
  Then, you can find the available locales from a given list of locales from the user:
31
67
 
32
68
  ```ruby
33
- preferred_locale.acceptable_for(['en-US', 'fr', 'ja-JP']) # => ['en', 'fr']
69
+ preferred_locale.acceptable_for(locales: ['en-US', 'fr', 'ja-JP']) # => ['en', 'fr']
34
70
  ```
35
71
 
36
72
  Or you can just get the best one:
37
73
 
38
74
  ```ruby
39
- preferred_locale.preferred_for(['en-US', 'fr', 'ja-JP']) # => 'en'
75
+ preferred_locale.preferred_for(locales: ['en-US', 'fr', 'ja-JP']) # => 'en'
40
76
  ```
41
77
 
42
78
  ### With Rails
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class PreferredLocale
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
@@ -15,14 +15,20 @@ class PreferredLocale
15
15
  obj[locale_str.downcase] = locale
16
16
  # Add the language without a country if it's not already in the list
17
17
  language = locale_str.downcase.split('-')[0]
18
- obj[language] = locale unless available_lower.include?(language)
18
+ obj[language] = locale unless available_lower.include?(language) || obj.key?(language)
19
19
  end
20
20
  end
21
21
 
22
22
  def acceptable_for(locales: [])
23
+ locales_lower = locales.map { |locale| locale.to_s.downcase }
23
24
  # 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]
25
+ candidates = locales_lower.flat_map do |locale|
26
+ language = locale.split('-')[0]
27
+ [
28
+ locale,
29
+ # Add the language without a country if it's not already in the list
30
+ (language unless locales_lower.include?(language))
31
+ ].compact
26
32
  end
27
33
 
28
34
  # Figure out which candidates are available
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: preferred_locale
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emma Lejeck
@@ -37,6 +37,7 @@ metadata:
37
37
  homepage_uri: https://github.com/hummingbird-me/preferred_locale
38
38
  source_code_uri: https://github.com/hummingbird-me/preferred_locale
39
39
  changelog_uri: https://github.com/hummingbird-me/preferred_locale/blob/main/CHANGELOG.md
40
+ rubygems_mfa_required: 'true'
40
41
  post_install_message:
41
42
  rdoc_options: []
42
43
  require_paths: