locale_detector 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Locale Detector
2
2
 
3
3
  This Rails gem makes use of the HTTP_ACCEPT_LANGUAGE http header sent by web browsers with every request, to set the `I18n.locale` setting.
4
- When this fails (for example if there is no such http header - as is the case for Google bot), it will try to determine the locale based on the toplevel domain suffix (so it will set 'de' for the `example.de` domain).
5
- When both fail, it will fall back to the `LocaleDetector.fallback_locale`, which is 'en' by default and can be overriden in `config/initializers/locale_detector.rb`.
4
+
5
+ When this fails (for example if there is no such http header - as is the case for Google bot), it will try to determine the locale based on the toplevel domain suffix (so it will set `'de'` for the `example.de` domain).
6
+
7
+ When both fail, it will fall back to `I18n.default_locale`, which should be set in application’s `config/application.rb` file (and is `'en'` by default).
6
8
 
7
9
  The HTTP_ACCEPT_LANGUAGE header parser is based on:
8
10
  https://github.com/iain/http_accept_language/blob/master/lib/http_accept_language.rb
@@ -13,6 +15,17 @@ https://github.com/iain/http_accept_language/blob/master/lib/http_accept_languag
13
15
  Rails 3.0+
14
16
 
15
17
 
18
+ ## Example
19
+
20
+ When this gem is installed and someone visits your application using a Polish version of Firefox the following http header will be sent:
21
+
22
+ ```
23
+ HTTP_ACCEPT_LANGUAGE: pl,en-us;q=0.7,en;q=0.3
24
+ ```
25
+
26
+ and the `I18n.locale` will be set to `'pl'`.
27
+
28
+
16
29
  ## Installation
17
30
 
18
31
  Install the gem
@@ -34,12 +47,5 @@ $ bundle
34
47
  ```
35
48
 
36
49
 
37
- You can optionally overwrite the default fallback locale by creating a `config/initializers/locale_detector.rb` file and set the `fallback_locale` to a new string, for example:
38
-
39
- ```ruby
40
- LocaleDetector.fallback_locale = 'pl'
41
- ```
42
-
43
-
44
50
 
45
51
  Copyright © 2011 Exvo.com Development BV, released under the MIT license
@@ -1,4 +1,3 @@
1
- require "locale_detector/fallback_locale"
2
1
  require "locale_detector/filter"
3
2
  require "locale_detector/version"
4
3
 
@@ -18,28 +18,100 @@ module LocaleDetector
18
18
  y.last.to_f <=> x.last.to_f
19
19
  end.first.first.gsub(/-[a-z]+$/i, '').downcase
20
20
  rescue # rescue (anything) from the malformed (or missing) accept language headers
21
+ country_to_language(request.host.split('.').last)
22
+ end
23
+ end
24
+
25
+ # a somewhat incomplete list of toplevel domain suffix to language code mappings
26
+ def country_to_language(country_code)
27
+
28
+ # sources:
29
+ # http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains
30
+ # http://www.w3.org/WAI/ER/IG/ert/iso639.htm
31
+ # http://msdn.microsoft.com/en-us/library/ms693062%28v=vs.85%29.aspx
32
+
33
+ # country_code => language_code
34
+ mappings = {
35
+ # English
36
+ :au => :en,
37
+ :ca => :en,
38
+ :eu => :en,
39
+ :ie => :en,
40
+ :nz => :en,
41
+ :sg => :en,
42
+ :uk => :en,
43
+ :us => :en,
44
+
45
+ # French
46
+ :cd => :fr,
47
+ :cg => :fr,
48
+ :cm => :fr,
49
+ :fr => :fr,
50
+ :mg => :fr,
21
51
 
22
- # parse request.host and try to set the locale based on toplevel domain suffix (for netbots mostly)
23
- suffix = request.host.split('.').last
24
-
25
- # somewhat incomplete list of more common toplevel domains
26
- if [:bg, :be, :cn, :cz, :da, :de, :es, :et, :fi, :fr, :gr, :hi, :hr, :hu, :is, :it, :jp, :ko, :lv, :lt,
27
- :mk, :nl, :no, :pl, :pt, :ro, :ru, :se, :sr, :sk, :sl, :tr, :vi, :ua].include?(suffix.to_sym)
28
- suffix
29
- # some common international/English domains
30
- elsif [:eu, :uk, :us].include?(suffix.to_sym)
31
- 'en'
32
- # Spanish speaking countries
33
- elsif [:ar, :cl, :co, :cu, :mx].include?(suffix.to_sym)
34
- 'es'
35
- # Portuguese speaking countries
36
- elsif suffix == 'br'
37
- 'pt'
38
- # fall back to fallback_locale for .com and all other domains ('en' by default)
39
- else
40
- LocaleDetector.fallback_locale
41
- end
52
+ # German
53
+ :at => :de,
54
+ :ch => :de,
55
+ :de => :de,
56
+ :li => :de,
57
+ :lu => :de,
58
+
59
+ # Portuguese
60
+ :ao => :pt,
61
+ :br => :pt,
62
+ :mz => :pt,
63
+ :pt => :pt,
64
+
65
+ # Spanish
66
+ :ar => :es,
67
+ :cl => :es,
68
+ :co => :es,
69
+ :cu => :es,
70
+ :es => :es,
71
+ :mx => :es,
72
+
73
+ # All other languages
74
+ :bg => :bg,
75
+ :by => :be,
76
+ :cn => :zh,
77
+ :cz => :cs,
78
+ :dk => :da,
79
+ :ee => :et,
80
+ :fi => :fi,
81
+ :gr => :el,
82
+ :hr => :hr,
83
+ :hu => :hu,
84
+ :il => :he,
85
+ :in => :hi,
86
+ :is => :is,
87
+ :it => :it,
88
+ :jp => :ja,
89
+ :kr => :ko,
90
+ :lt => :lt,
91
+ :lv => :lv,
92
+ :mn => :mn,
93
+ :nl => :nl,
94
+ :no => :no,
95
+ :pl => :pl,
96
+ :ro => :ro,
97
+ :rs => :sr,
98
+ :ru => :ru,
99
+ :se => :sv,
100
+ :si => :sl,
101
+ :sk => :sk,
102
+ :th => :th,
103
+ :tr => :tr,
104
+ :ua => :uk,
105
+ :vn => :vi,
106
+ }
107
+
108
+ if mappings.has_key?(country_code.to_sym)
109
+ mappings[country_code.to_sym].to_s
110
+ else
111
+ # fall back for all other missing mappings
112
+ I18n.default_locale.to_s
42
113
  end
114
+
43
115
  end
44
116
  end
45
117
  end
@@ -1,3 +1,3 @@
1
1
  module LocaleDetector
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
10
10
  s.email = ["pawel.goscicki@gmail.com"]
11
11
  s.homepage = "https://github.com/Exvo/locale_detector"
12
12
  s.summary = "Rails gem setting the I18n.locale based on user's browser language"
13
- s.description = "Parses HTTP_ACCEPT_LANGUAGE http header and sets the I18n.locale based on that"
13
+ s.description = "Parses HTTP_ACCEPT_LANGUAGE http header and sets the I18n.locale based on that, if it's missing it uses toplevel regional domain suffix or falls back to I18n.default_locale"
14
14
 
15
15
  s.files = `git ls-files`.split("\n")
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -16,7 +16,6 @@ describe LocaleDetector::Filter do
16
16
  end
17
17
 
18
18
  context "http header locale setting" do
19
-
20
19
  specify { set_locale(:language => 'pl').should eql('pl') }
21
20
 
22
21
  specify { set_locale(:language => 'pl-PL').should eql('pl') }
@@ -26,11 +25,9 @@ describe LocaleDetector::Filter do
26
25
  specify { set_locale(:language => 'lt,en-us;q=0.8,en;q=0.6,ru;q=0.4,pl;q=0.2').should eql('lt') }
27
26
 
28
27
  specify { set_locale(:language => 'pl-PL;q=0.1,en-us;q=0.7,en;q=0.3').should eql('en') }
29
-
30
28
  end
31
29
 
32
30
  context "host based locale setting" do
33
-
34
31
  specify { set_locale(:host => 'example.pl').should eql('pl') }
35
32
 
36
33
  specify { set_locale(:host => 'example.co.uk').should eql('en') }
@@ -39,8 +36,17 @@ describe LocaleDetector::Filter do
39
36
 
40
37
  specify { set_locale(:host => 'example.br').should eql('pt') }
41
38
 
42
- specify { set_locale(:host => 'example.com').should eql(LocaleDetector.fallback_locale) }
39
+ specify { set_locale(:host => 'example.jp').should eql('ja') }
40
+
41
+ specify { set_locale(:host => 'example.se').should eql('sv') }
42
+ end
43
+
44
+ context "default fallback" do
45
+ before do
46
+ I18n.default_locale = 'de'
47
+ end
43
48
 
49
+ specify { set_locale(:host => 'example.com').should eql('de') }
44
50
  end
45
51
 
46
52
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locale_detector
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Pawe\xC5\x82 Go\xC5\x9Bcicki"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-09 00:00:00 +02:00
18
+ date: 2011-08-11 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -112,7 +112,7 @@ dependencies:
112
112
  version: "2.6"
113
113
  type: :development
114
114
  version_requirements: *id006
115
- description: Parses HTTP_ACCEPT_LANGUAGE http header and sets the I18n.locale based on that
115
+ description: Parses HTTP_ACCEPT_LANGUAGE http header and sets the I18n.locale based on that, if it's missing it uses toplevel regional domain suffix or falls back to I18n.default_locale
116
116
  email:
117
117
  - pawel.goscicki@gmail.com
118
118
  executables: []
@@ -129,12 +129,10 @@ files:
129
129
  - README.md
130
130
  - Rakefile
131
131
  - lib/locale_detector.rb
132
- - lib/locale_detector/fallback_locale.rb
133
132
  - lib/locale_detector/filter.rb
134
133
  - lib/locale_detector/railtie.rb
135
134
  - lib/locale_detector/version.rb
136
135
  - locale_detector.gemspec
137
- - spec/locale_detector/fallback_locale_spec.rb
138
136
  - spec/locale_detector/filter_spec.rb
139
137
  - spec/spec_helper.rb
140
138
  has_rdoc: true
@@ -1,5 +0,0 @@
1
- module LocaleDetector
2
- @@fallback_locale = 'en'
3
-
4
- mattr_accessor :fallback_locale
5
- end
@@ -1,9 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe LocaleDetector do
4
-
5
- subject { LocaleDetector.fallback_locale }
6
-
7
- it { should_not be_empty }
8
-
9
- end